[FIX] website_mail_group: restore missing snippet icon
[odoo/odoo.git] / history / migrate / 4.0.0-4.2.0 / pre.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 __author__ = 'Cédric Krier, <ced@tinyerp.com>'
23 __version__ = '0.1.0'
24
25 import psycopg
26 import optparse
27 import ConfigParser
28
29 # -----
30
31 parser = optparse.OptionParser(version="Tiny ERP server migration script " + __version__)
32
33 parser.add_option("-c", "--config", dest="config", help="specify path to Tiny ERP config file")
34
35 group = optparse.OptionGroup(parser, "Database related options")
36 group.add_option("--db_host", dest="db_host", help="specify the database host") 
37 group.add_option("--db_port", dest="db_port", help="specify the database port") 
38 group.add_option("-d", "--database", dest="db_name", help="specify the database name")
39 group.add_option("-r", "--db_user", dest="db_user", help="specify the database user name")
40 group.add_option("-w", "--db_password", dest="db_password", help="specify the database password") 
41 parser.add_option_group(group)
42
43 options = optparse.Values()
44 options.db_name = 'terp' # default value
45 parser.parse_args(values=options)
46
47 if hasattr(options, 'config'):
48     configparser = ConfigParser.ConfigParser()
49     configparser.read([options.config])
50     for name, value in configparser.items('options'):
51         if not (hasattr(options, name) and getattr(options, name)):
52             if value in ('true', 'True'):
53                 value = True
54             if value in ('false', 'False'):
55                 value = False
56             setattr(options, name, value)
57
58 # -----
59
60 host = hasattr(options, 'db_host') and "host=%s" % options.db_host or ''
61 port = hasattr(options, 'db_port') and "port=%s" % options.db_port or ''
62 name = "dbname=%s" % options.db_name
63 user = hasattr(options, 'db_user') and "user=%s" % options.db_user or ''
64 password = hasattr(options, 'db_password') and "password=%s" % options.db_password or ''
65
66 db = psycopg.connect('%s %s %s %s %s' % (host, port, name, user, password), serialize=0)
67 cr = db.cursor()
68
69 # ------------------------ #
70 # change currency rounding #
71 # ------------------------ #
72
73 cr.execute("""SELECT c.relname,a.attname,a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,t.typname,CASE WHEN a.attlen=-1 THEN a.atttypmod-4 ELSE a.attlen END as size FROM pg_class c,pg_attribute a,pg_type t WHERE c.relname='res_currency' AND a.attname='rounding' AND c.oid=a.attrelid AND a.atttypid=t.oid""")
74 res = cr.dictfetchall()
75 if res[0]['typname'] != 'numeric':
76     for line in (
77         "ALTER TABLE res_currency RENAME rounding TO rounding_bak",
78         "ALTER TABLE res_currency ADD rounding NUMERIC(12,6)",
79         "UPDATE res_currency SET rounding = power(10, - rounding_bak)",
80         "ALTER TABLE res_currency DROP rounding_bak",
81         ):
82         cr.execute(line)
83 cr.commit()
84
85 # ----------------------------- #
86 # drop constraint on ir_ui_view #
87 # ----------------------------- #
88
89 cr.execute('SELECT conname FROM pg_constraint where conname = \'ir_ui_view_type\'')
90 if cr.fetchall():
91     cr.execute('ALTER TABLE ir_ui_view DROP CONSTRAINT ir_ui_view_type')
92 cr.commit()
93
94 # ------------------------ #
95 # update res.partner.bank  #
96 # ------------------------ #
97
98 cr.execute('SELECT a.attname FROM pg_class c, pg_attribute a WHERE c.relname = \'res_partner_bank\' AND a.attname = \'iban\' AND c.oid = a.attrelid')
99 if cr.fetchall():
100     cr.execute('ALTER TABLE res_partner_bank RENAME iban TO acc_number')
101 cr.commit()
102
103 # ------------------------------------------- #
104 # Add perm_id to ir_model and ir_model_fields #
105 # ------------------------------------------- #
106
107 cr.execute('SELECT a.attname FROM pg_class c, pg_attribute a WHERE c.relname = \'ir_model\' AND a.attname = \'perm_id\' AND c.oid = a.attrelid')
108 if not cr.fetchall():
109     cr.execute("ALTER TABLE ir_model ADD perm_id int references perm on delete set null")
110 cr.commit()
111
112 cr.execute('SELECT a.attname FROM pg_class c, pg_attribute a WHERE c.relname = \'ir_model_fields\' AND a.attname = \'perm_id\' AND c.oid = a.attrelid')
113 if not cr.fetchall():
114     cr.execute("ALTER TABLE ir_model_fields ADD perm_id int references perm on delete set null")
115 cr.commit()
116
117
118 # --------------------------------- #
119 # remove name for all ir_act_window #
120 # --------------------------------- #
121
122 cr.execute("UPDATE ir_act_window SET name = ''")
123 cr.commit()
124
125 # ------------------------------------------------------------------------ #
126 # Create a "allow none" default access to keep the behaviour of the system #
127 # ------------------------------------------------------------------------ #
128
129 cr.execute('SELECT model_id FROM ir_model_access')
130 res= cr.fetchall()
131 for r in res:
132     cr.execute('SELECT id FROM ir_model_access WHERE model_id = %d AND group_id IS NULL', (r[0],))
133     if not cr.fetchall():
134         cr.execute("INSERT into ir_model_access (name,model_id,group_id) VALUES ('Auto-generated access by migration',%d,NULL)",(r[0],))
135 cr.commit()
136
137 # ------------------------------------------------- #
138 # Drop view report_account_analytic_line_to_invoice #
139 # ------------------------------------------------- #
140
141 cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_account_analytic_line_to_invoice\'')
142 if cr.fetchall():
143     cr.execute('DROP VIEW report_account_analytic_line_to_invoice')
144 cr.commit()
145
146 # --------------------------- #
147 # Drop state from hr_employee #
148 # --------------------------- #
149
150 cr.execute('SELECT * FROM pg_class c, pg_attribute a WHERE c.relname=\'hr_employee\' AND a.attname=\'state\' AND c.oid=a.attrelid')
151 if cr.fetchall():
152     cr.execute('ALTER TABLE hr_employee DROP state')
153 cr.commit()
154
155 # ------------ #
156 # Add timezone #
157 # ------------ #
158
159 cr.execute('SELECT id FROM ir_values where model=\'res.users\' AND key=\'meta\' AND name=\'tz\'')
160 if not cr.fetchall():
161     import pytz, pickle
162     meta = pickle.dumps({'type':'selection', 'string':'Timezone', 'selection': [(x, x) for x in pytz.all_timezones]})
163     value = pickle.dumps(False)
164     cr.execute('INSERT INTO ir_values (name, key, model, meta, key2, object, value) VALUES (\'tz\', \'meta\', \'res.users\', %s, \'tz\', %s, %s)', (meta,False, value))
165 cr.commit()
166
167 # ------------------------- #
168 # change product_uom factor #
169 # ------------------------- #
170
171 cr.execute('SELECT a.attname FROM pg_class c, pg_attribute a, pg_type t WHERE c.relname = \'product_uom\' AND a.attname = \'factor\' AND c.oid = a.attrelid AND a.atttypid = t.oid AND t.typname = \'float8\'')
172 if cr.fetchall():
173     cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_account_analytic_planning_stat_account\'')
174     if cr.fetchall():
175         cr.execute('DROP VIEW report_account_analytic_planning_stat_account')
176     cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_account_analytic_planning_stat\'')
177     if cr.fetchall():
178         cr.execute('DROP VIEW report_account_analytic_planning_stat')
179     cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_account_analytic_planning_stat_user\'')
180     if cr.fetchall():
181         cr.execute('DROP VIEW report_account_analytic_planning_stat_user')
182     cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_purchase_order_product\'')
183     if cr.fetchall():
184         cr.execute('DROP VIEW report_purchase_order_product')
185     cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_purchase_order_category\'')
186     if cr.fetchall():
187         cr.execute('DROP VIEW report_purchase_order_category')
188     cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_sale_order_product\'')
189     if cr.fetchall():
190         cr.execute('DROP VIEW report_sale_order_product')
191     cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_sale_order_category\'')
192     if cr.fetchall():
193         cr.execute('DROP VIEW report_sale_order_category')
194     cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_hr_timesheet_invoice_journal\'')
195     if cr.fetchall():
196         cr.execute('DROP VIEW report_hr_timesheet_invoice_journal')
197
198     cr.execute('ALTER TABLE product_uom RENAME COLUMN factor to temp_column')
199     cr.execute('ALTER TABLE product_uom ADD COLUMN factor NUMERIC(12,6)')
200     cr.execute('UPDATE product_uom SET factor = temp_column')
201     cr.execute('ALTER TABLE product_uom ALTER factor SET NOT NULL')
202     cr.execute('ALTER TABLE product_uom DROP COLUMN temp_column')
203 cr.commit()
204
205
206 # ------------------------------------------------- #
207 # Drop name_uniq constraint on stock_production_lot #
208 # ------------------------------------------------- #
209
210 cr.execute('SELECT conname FROM pg_constraint where conname = \'stock_production_lot_name_uniq\'')
211 if cr.fetchall():
212     cr.execute('ALTER TABLE stock_production_lot DROP CONSTRAINT stock_production_lot_name_uniq')
213 cr.commit()
214
215 # ------------------------------------ #
216 # Put country/state code in upper case #
217 # ------------------------------------ #
218
219 cr.execute('UPDATE res_country SET code = UPPER(code)')
220 cr.execute('UPDATE res_country_state SET code = UPPER(code)')
221 cr.commit()
222
223 # --------------------------------------------- #
224 # Add primary key on tables inherits ir_actions #
225 # --------------------------------------------- #
226
227 cr.execute('SELECT indexname FROm pg_indexes WHERE indexname = \'ir_act_report_xml_pkey\' and tablename = \'ir_act_report_xml\'')
228 if not cr.fetchall():
229     cr.execute('ALTER TABLE ir_act_report_xml ADD PRIMARY KEY (id)')
230 cr.execute('SELECT indexname FROm pg_indexes WHERE indexname = \'ir_act_report_custom_pkey\' and tablename = \'ir_act_report_custom\'')
231 if not cr.fetchall():
232     cr.execute('ALTER TABLE ir_act_report_custom ADD PRIMARY KEY (id)')
233 cr.execute('SELECT indexname FROm pg_indexes WHERE indexname = \'ir_act_group_pkey\' and tablename = \'ir_act_group\'')
234 if not cr.fetchall():
235     cr.execute('ALTER TABLE ir_act_group ADD PRIMARY KEY (id)')
236 cr.execute('SELECT indexname FROm pg_indexes WHERE indexname = \'ir_act_execute_pkey\' and tablename = \'ir_act_execute\'')
237 if not cr.fetchall():
238     cr.execute('ALTER TABLE ir_act_execute ADD PRIMARY KEY (id)')
239 cr.execute('SELECT indexname FROm pg_indexes WHERE indexname = \'ir_act_wizard_pkey\' and tablename = \'ir_act_wizard\'')
240 if not cr.fetchall():
241     cr.execute('ALTER TABLE ir_act_wizard ADD PRIMARY KEY (id)')
242 cr.commit()
243
244 cr.close
245
246 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
247