migration: add insert timezone in ir_value
[odoo/odoo.git] / doc / migrate / 3.3.0-3.4.0 / post.py
1 ##############################################################################
2 #
3 # Copyright (c) 2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsability of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # garantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 2
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25 #
26 ##############################################################################
27
28 __author__ = 'Gaetan de Menten, <ged@tiny.be>'
29 __version__ = '0.1.0'
30
31 import psycopg
32 import optparse
33 import ConfigParser
34
35 # -----
36
37 parser = optparse.OptionParser(version="Tiny ERP server migration script " + __version__)
38
39 parser.add_option("-c", "--config", dest="config", help="specify path to Tiny ERP config file")
40
41 group = optparse.OptionGroup(parser, "Database related options")
42 group.add_option("--db_host", dest="db_host", help="specify the database host") 
43 group.add_option("--db_port", dest="db_port", help="specify the database port") 
44 group.add_option("-d", "--database", dest="db_name", help="specify the database name")
45 group.add_option("-r", "--db_user", dest="db_user", help="specify the database user name")
46 group.add_option("-w", "--db_password", dest="db_password", help="specify the database password") 
47 parser.add_option_group(group)
48
49 options = optparse.Values()
50 options.db_name = 'terp' # default value
51 parser.parse_args(values=options)
52
53 if hasattr(options, 'config'):
54         configparser = ConfigParser.ConfigParser()
55         configparser.read([options.config])
56         for name, value in configparser.items('options'):
57                 if not (hasattr(options, name) and getattr(options, name)):
58                         if value in ('true', 'True'):
59                                 value = True
60                         if value in ('false', 'False'):
61                                 value = False
62                         setattr(options, name, value)
63
64 # -----
65
66 host = hasattr(options, 'db_host') and "host=%s" % options.db_host or ''
67 port = hasattr(options, 'db_port') and "port=%s" % options.db_port or ''
68 name = "dbname=%s" % options.db_name
69 user = hasattr(options, 'db_user') and "user=%s" % options.db_user or ''
70 password = hasattr(options, 'db_password') and "password=%s" % options.db_password or ''
71
72 db = psycopg.connect('%s %s %s %s %s' % (host, port, name, user, password), serialize=0)
73 cr = db.cursor()
74
75 # ------------------------------------------- #
76 # convert partner payment terms to properties #
77 # ------------------------------------------- #
78
79 # setup
80
81 cr.execute("select id from ir_model_fields where name='property_payment_term' and model='res.partner'")
82 fields_id = cr.fetchone()[0]
83
84 cr.execute("select company_id from res_users where company_id is not null limit 1")
85 company_id = cr.fetchone()[0]
86
87 # get partners
88 cr.execute("SELECT c.relname FROM pg_class c, pg_attribute a WHERE c.relname='res_partner' AND a.attname='payment_term' AND c.oid=a.attrelid")
89 partners=[]
90 drop_payment_term=False
91 if cr.rowcount:
92         drop_payment_term=True
93         cr.execute("select id, payment_term from res_partner where payment_term is not null")
94         partners = cr.dictfetchall()
95
96 # loop over them
97
98 for partner in partners:
99         value = 'account.payment.term,%d' % partner['payment_term']
100         res_id = 'res.partner,%d' % partner['id']
101         cr.execute(
102                 "insert into ir_property(name, value, res_id, company_id, fields_id) "\
103                 "values(%s, %s, %s, %d, %d)", 
104                 ('property_payment_term', value, res_id, company_id, fields_id))
105
106 # remove the field
107 if drop_payment_term:
108         cr.execute("alter table res_partner drop column payment_term")
109 cr.execute("delete from ir_model_fields where model = 'res.partner' and name = 'payment_term'")
110
111 cr.commit()
112
113 # ------------------------ #
114 # remove duplicate reports #
115 # ------------------------ #
116
117 cr.execute("select model, report_name from ir_act_report_xml group by model, report_name having count(*)>1")
118 reports_wh_duplicates = cr.dictfetchall()
119
120 cr.execute("select res_id from ir_model_data where model='ir.actions.report.xml'")
121 registered_reports = cr.fetchall()
122 reg_reports_ids = ','.join([str(id) for (id,) in registered_reports])
123
124 for report in reports_wh_duplicates:
125         cr.execute("select id from ir_act_report_xml where model=%s and report_name=%s and id not in ("+reg_reports_ids+")", (report['model'], report['report_name']))
126         (id,) = cr.fetchone()
127         cr.execute("delete from ir_act_report_xml where id=%d", (id,))
128         cr.execute("delete from ir_values where value='ir.actions.report.xml,%d'", (id,))
129
130 cr.commit()
131
132 # ------------------------------------- #
133 # remove duplicate workflow transitions #
134 # ------------------------------------- #
135
136 # this removes all transitions which are not registered in ir_model_data
137
138 cr.execute("delete from wkf_transition where id not in (select res_id from ir_model_data where model='workflow.transition')")
139 cr.commit()
140
141 # -------------------------------- #
142 # remove bad "default" menu action #
143 # -------------------------------- #
144
145 cr.execute("delete from ir_values where key='action' and model='ir.ui.menu' and res_id is null")
146 cr.commit()
147
148 cr.close()
149