1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 __author__ = 'Cédric Krier, <ced@tinyerp.com>'
31 parser = optparse.OptionParser(version="Tiny ERP server migration script " + __version__)
33 parser.add_option("-c", "--config", dest="config", help="specify path to Tiny ERP config file")
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)
43 options = optparse.Values()
44 options.db_name = 'terp' # default value
45 parser.parse_args(values=options)
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'):
54 if value in ('false', 'False'):
56 setattr(options, name, value)
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 ''
66 db = psycopg.connect('%s %s %s %s %s' % (host, port, name, user, password), serialize=0)
69 # ------------------------ #
70 # change currency rounding #
71 # ------------------------ #
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':
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",
85 # ----------------------------- #
86 # drop constraint on ir_ui_view #
87 # ----------------------------- #
89 cr.execute('SELECT conname FROM pg_constraint where conname = \'ir_ui_view_type\'')
91 cr.execute('ALTER TABLE ir_ui_view DROP CONSTRAINT ir_ui_view_type')
94 # ------------------------ #
95 # update res.partner.bank #
96 # ------------------------ #
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')
100 cr.execute('ALTER TABLE res_partner_bank RENAME iban TO acc_number')
103 # ------------------------------------------- #
104 # Add perm_id to ir_model and ir_model_fields #
105 # ------------------------------------------- #
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")
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")
118 # --------------------------------- #
119 # remove name for all ir_act_window #
120 # --------------------------------- #
122 cr.execute("UPDATE ir_act_window SET name = ''")
125 # ------------------------------------------------------------------------ #
126 # Create a "allow none" default access to keep the behaviour of the system #
127 # ------------------------------------------------------------------------ #
129 cr.execute('SELECT model_id FROM ir_model_access')
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],))
137 # ------------------------------------------------- #
138 # Drop view report_account_analytic_line_to_invoice #
139 # ------------------------------------------------- #
141 cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_account_analytic_line_to_invoice\'')
143 cr.execute('DROP VIEW report_account_analytic_line_to_invoice')
146 # --------------------------- #
147 # Drop state from hr_employee #
148 # --------------------------- #
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')
152 cr.execute('ALTER TABLE hr_employee DROP state')
159 cr.execute('SELECT id FROM ir_values where model=\'res.users\' AND key=\'meta\' AND name=\'tz\'')
160 if not cr.fetchall():
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))
167 # ------------------------- #
168 # change product_uom factor #
169 # ------------------------- #
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\'')
173 cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_account_analytic_planning_stat_account\'')
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\'')
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\'')
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\'')
184 cr.execute('DROP VIEW report_purchase_order_product')
185 cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_purchase_order_category\'')
187 cr.execute('DROP VIEW report_purchase_order_category')
188 cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_sale_order_product\'')
190 cr.execute('DROP VIEW report_sale_order_product')
191 cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_sale_order_category\'')
193 cr.execute('DROP VIEW report_sale_order_category')
194 cr.execute('SELECT viewname FROM pg_views WHERE viewname = \'report_hr_timesheet_invoice_journal\'')
196 cr.execute('DROP VIEW report_hr_timesheet_invoice_journal')
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')
206 # ------------------------------------------------- #
207 # Drop name_uniq constraint on stock_production_lot #
208 # ------------------------------------------------- #
210 cr.execute('SELECT conname FROM pg_constraint where conname = \'stock_production_lot_name_uniq\'')
212 cr.execute('ALTER TABLE stock_production_lot DROP CONSTRAINT stock_production_lot_name_uniq')
215 # ------------------------------------ #
216 # Put country/state code in upper case #
217 # ------------------------------------ #
219 cr.execute('UPDATE res_country SET code = UPPER(code)')
220 cr.execute('UPDATE res_country_state SET code = UPPER(code)')
223 # --------------------------------------------- #
224 # Add primary key on tables inherits ir_actions #
225 # --------------------------------------------- #
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)')
246 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: