30f1e31a7390c6dcad04a75232065d92658e1be0
[odoo/odoo.git] / doc / migrate / 3.4.0-4.0.0 / pre.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 # change some columns types #
77 # ------------------------- #
78
79 def change_column(cr, table, column, new_type, copy):
80         commands = [
81                 "ALTER TABLE %s RENAME COLUMN %s TO temp_column" % (table, column),
82                 "ALTER TABLE %s ADD COLUMN %s %s" % (table, column, new_type),
83                 "ALTER TABLE %s DROP COLUMN temp_column" % table
84         ]
85         if copy:
86                 commands.insert(
87                         2, 
88                         "UPDATE %s SET %s=temp_column::%s" % (table, column, new_type))
89
90         for command in commands:
91                 cr.execute(command)
92
93 change_column(cr, 'crm_case', 'date_closed', 'timestamp', True)
94 cr.commit()
95
96 # -------------------- #
97 # add module if needed #
98 # -------------------- #
99
100 cr.execute("SELECT name FROM ir_module_module")
101 if not cr.rowcount:
102         for module in ('base', 'marketing', 'subscription', 'account', 'base_partner_relation', 'audittrail', 'account_followup', 'product', 'hr', 'l10n_simple', 'crm', 'stock', 'hr_timesheet', 'purchase', 'report_purchase', 'mrp', 'sale', 'report_sale', 'delivery', 'project', 'sale_crm', 'hr_timesheet_project', 'scrum', 'report_project'):
103                 cr.execute("INSERT INTO ir_module_module (name, state) VALUES ('%s', 'installed')" % module)
104         cr.commit()
105
106 # --------------- #
107 # remove old menu #
108 # --------------- #
109
110 while True:
111         cr.execute("select id from ir_ui_menu where id not in (select parent_id from ir_ui_menu where parent_id is not null) and id not in (select res_id from ir_model_data where model='ir.ui.menu')")
112         if not cr.rowcount:
113                 break
114         cr.execute("delete from ir_ui_menu where id not in (select parent_id from ir_ui_menu where parent_id is not null) and id not in (select res_id from ir_model_data where model='ir.ui.menu')")
115 cr.commit()
116
117 # ----------------------------------------------------- #
118 # add some fields (which cannot be added automatically) #
119 # ----------------------------------------------------- #
120
121 for line in (
122                 "ALTER TABLE ir_module_module ADD demo BOOLEAN",
123                 "ALTER TABLE ir_module_module SET demo DEFAULT False",
124                 "DELETE FROM ir_values WHERE VALUE LIKE '%,False'",
125                 """UPDATE ir_ui_view set arch='<?xml version="1.0"?><tree string="Menu" toolbar="1"><field icon="icon" name="name"/></tree>' where name='ir.ui.menu.tree' and type='tree' and field_parent='child_id'""",
126         ):
127         cr.execute(line)
128
129 cr.commit()
130 cr.close()