Merge lp:~savoirfairelinux-openerp/openupgrade-server/base_contact into lp:openupgrade-server

Status: Merged
Merged at revision: 4642
Proposed branch: lp:~savoirfairelinux-openerp/openupgrade-server/base_contact
Merge into: lp:openupgrade-server
Diff against target: 165 lines (+96/-5)
2 files modified
openerp/addons/base/migrations/7.0.1.3/post-migration.py (+80/-4)
openerp/addons/base/migrations/7.0.1.3/pre-migration.py (+16/-1)
To merge this branch: bzr merge lp:~savoirfairelinux-openerp/openupgrade-server/base_contact
Reviewer Review Type Date Requested Status
Pedro Manuel Baeza code review Approve
Holger Brunn (Therp) code review Approve
Stefan Rijnhart (Opener) Approve
Review via email: mp+194762@code.launchpad.net

Description of the change

Marks base_contact module to be removed.
Moves entries from res_partner_contact to res_partner.

To post a comment you must log in.
Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

Thanks! Looks alright to me, in principle. Two questions:

l.51 and l.78: why do you need to check if the fields exist? Seems to me this could mask potential problems in the current or future version of the code.
l.12: would it not be safer if you checked the status of the entry for 'base_contact' in the ir_module_module table? The table could theoretically belong to another module, or contain deprecated information after a deinstallation of the base_contact module.

review: Needs Information
Revision history for this message
Sandy Carter (http://www.savoirfairelinux.com) (sandy-carter) wrote :

> l.51 and l.78: why do you need to check if the fields exist? Seems to me this
> could mask potential problems in the current or future version of the code.

For l.78, I put that in place to avoid an error I had while testing relating to the 'mobile' column, which is not present on a bare system with only base_contact installed. I duplicated that safeguard in l.51 to avoid future similar errors.

Steps to reproduce:
OpenERP 6.1 with new database, install base_contact
Run OpenUpgrade

2013-11-14 16:33:28,336 9796 ERROR upgrade openerp: Failed to initialize database `upgrade`.
Traceback (most recent call last):
...
  File "base/migrations/7.0.1.3/post-migration.py", line 282, in migrate
  File "base/migrations/7.0.1.3/post-migration.py", line 211, in migrate_partner_address
  File "base/migrations/7.0.1.3/post-migration.py", line 170, in process_address_type
  File "parts/openerp/openerp/sql_db.py", line 161, in wrapper
    return f(self, *args, **kwargs)
  File "parts/openerp/openerp/sql_db.py", line 226, in execute
    res = self._obj.execute(query, params)
ProgrammingError: column "mobile" does not exist
LIGNE 1 : ...irthdate, city, country_id, email, fax, function, mobile, ph...
                                                               ^

> l.12: would it not be safer if you checked the status of the entry for
> 'base_contact' in the ir_module_module table? The table could theoretically
> belong to another module, or contain deprecated information after a
> deinstallation of the base_contact module.
It would. It's fixed now.

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

Thanks for adapting the check for base_contact!

I see now how the mobile field can go AWOL. Base_contact changes the field type to an unstored related field, upon which the ORM drops the column (I never liked it doing that). So your code makes good sense.

I'd like it though, if you could report on the missing fields in a logged warning. Would you mind doing so? Apart from that, this looks really good.

review: Needs Fixing
Revision history for this message
Sandy Carter (http://www.savoirfairelinux.com) (sandy-carter) wrote :

Stefan,
Thank you for your review.
This last commit should log as a warning any fields missing.

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

Thanks! Is there a special reason that you log the lost fields in l.194, which is called inside a loop? I'd prefer to log in line 82, right after determining the value of lost_fields. And maybe make the message a little less alarming and a more explaining, like:

'The following columns are not present in the table of %s: %s. This can be the case if an additional module installed on your database changes the type of a regular column to a non-stored function or related field.'

Given the nature of the message, I expect this one to pop up on the mailing list and having a more verbose message saves me the trouble of having to think ;-)

review: Needs Fixing
Revision history for this message
Sandy Carter (http://www.savoirfairelinux.com) (sandy-carter) wrote :

FYI, I found a merge proposal to bring base_contact into v7.0
This could affect this MP, even make it obsolete.

https://code.launchpad.net/~openerp-dev/openobject-addons/7.0-base-contact-xal/+merge/192129

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

@sandy Yes I was aware of this branch but I did not know it was submitted for review. Let's see where that goes.

I'll approve this proposal as per your latest changes (thanks!) but set it to 'Work in progress' until you decide which way you want to go.

review: Approve
Revision history for this message
Sandy Carter (http://www.savoirfairelinux.com) (sandy-carter) wrote :

@Stefan, I feel like the 7.0 base_contact will not be merged for a long time.
If we offer this intermediary step for users upgrading it will be more useful than stalling.

Once the 7.0 base_contact merge is accepted, we just have to take the scripts out.

I think we should merge it.

Revision history for this message
Holger Brunn (Therp) (hbrunn) :
review: Approve (code review)
Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

I see this OK, so as Sandy suggested, we should procede with the merge.

review: Approve (code review)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openerp/addons/base/migrations/7.0.1.3/post-migration.py'
2--- openerp/addons/base/migrations/7.0.1.3/post-migration.py 2013-10-21 08:59:09 +0000
3+++ openerp/addons/base/migrations/7.0.1.3/post-migration.py 2013-11-18 15:34:37 +0000
4@@ -56,11 +56,69 @@
5 SET rml_footer = rml_footer1
6 """)
7
8+def migrate_base_contact(cr):
9+ """
10+ Move entries of res_partner_contact into res_partner
11+ """
12+ cr.execute("SELECT * FROM ir_module_module WHERE name = 'base_contact' and state = 'to remove';")
13+ if not cr.fetchall():
14+ return
15+ fields = [
16+ 'create_date',
17+ 'name',
18+ 'website',
19+ 'image',
20+ 'active',
21+ 'comment',
22+ 'title',
23+ 'phone',
24+ 'country_id',
25+ 'email',
26+ 'birthdate',
27+ 'lang',
28+ 'parent_id',
29+ ]
30+ # Add lang from lang_id
31+ openupgrade.logged_query(cr, "ALTER TABLE res_partner_contact ADD COLUMN lang character varying(5);")
32+ openupgrade.logged_query(cr, """
33+ UPDATE res_partner_contact
34+ SET lang = (SELECT code
35+ FROM res_lang l
36+ WHERE lang_id = l.id
37+ LIMIT 1);""")
38+ # Add parent_id
39+ openupgrade.logged_query(cr, "ALTER TABLE res_partner_contact ADD COLUMN parent_id integer;")
40+ openupgrade.logged_query(cr, """
41+ UPDATE res_partner_contact
42+ SET parent_id = (SELECT openupgrade_7_migrated_to_partner_id
43+ FROM res_partner_address a
44+ WHERE id = a.contact_id
45+ LIMIT 1);""")
46+ # Make sure fields exist
47+ cr.execute(
48+ "SELECT column_name "
49+ "FROM information_schema.columns "
50+ "WHERE table_name = 'res_partner_contact';")
51+ available_fields = set(i[0] for i in cr.fetchall())
52+ lost_fields = set(fields) - available_fields
53+ if lost_fields:
54+ openupgrade.logger.warning("""\
55+The following columns are not present in the table of %s: %s.
56+
57+This can be the case if an additional module installed on your database changes the type of a
58+regular column to a non-stored function or related field.
59+""", 'res_partner_contact', ", ".join(lost_fields))
60+ fields = list(available_fields.intersection(fields))
61+ # Move data
62+ openupgrade.logged_query(cr, """
63+ INSERT INTO res_partner (%s)
64+ SELECT %s
65+ FROM res_partner_contact;""" % (", ".join(fields + ['customer', 'is_company']),
66+ ", ".join(fields + ['TRUE', 'FALSE'])))
67+
68 def migrate_partner_address(cr, pool):
69 """ res.partner.address is obsolete. Move existing data to
70 partner
71-
72- TODO: break hard when base_contact is installed
73 """
74 partner_obj = pool.get('res.partner')
75 cr.execute(
76@@ -82,6 +140,22 @@
77 partner_found = []
78 processed_ids = []
79
80+ # Make sure fields exist
81+ cr.execute(
82+ "SELECT column_name "
83+ "FROM information_schema.columns "
84+ "WHERE table_name = 'res_partner_address';")
85+ available_fields = set(i[0] for i in cr.fetchall())
86+ lost_fields = set(fields) - available_fields
87+ if lost_fields:
88+ openupgrade.logger.warning("""\
89+The following columns are not present in the table of %s: %s.
90+
91+This can be the case if an additional module installed on your database changes the type of a
92+regular column to a non-stored function or related field.
93+""", 'res_partner_address', ", ".join(lost_fields))
94+ fields = available_fields.intersection(fields)
95+
96 def set_address_partner(address_id, partner_id):
97 cr.execute(
98 "UPDATE res_partner_address "
99@@ -107,8 +181,9 @@
100 """
101 Migrate addresses to partners, based on sql WHERE clause
102 """
103- cr.execute(
104- "SELECT " + ', '.join(fields) + " FROM res_partner_address "
105+ openupgrade.logged_query(cr, "\n"
106+ "SELECT " + ', '.join(fields) + "\n"
107+ "FROM res_partner_address\n"
108 "WHERE " + whereclause, args or ())
109 for row in cr.fetchall():
110 row_cleaned = [val or False for val in row]
111@@ -222,6 +297,7 @@
112 migrate_ir_translation(cr)
113 migrate_company(cr)
114 migrate_partner_address(cr, pool)
115+ migrate_base_contact(cr)
116 update_users_partner(cr, pool)
117 reset_currency_companies(cr, pool)
118 migrate_res_company_logo(cr, pool)
119
120=== modified file 'openerp/addons/base/migrations/7.0.1.3/pre-migration.py'
121--- openerp/addons/base/migrations/7.0.1.3/pre-migration.py 2013-10-25 19:54:27 +0000
122+++ openerp/addons/base/migrations/7.0.1.3/pre-migration.py 2013-11-18 15:34:37 +0000
123@@ -63,6 +63,19 @@
124 cr,
125 "UPDATE ir_module_module SET demo = false")
126
127+def rename_base_contact_columns(cr):
128+ """
129+ Rename columns only if res_partner_contact is installed
130+ """
131+ cr.execute("SELECT * FROM information_schema.tables WHERE table_name = 'res_partner_contact';")
132+ if cr.fetchall():
133+ openupgrade.rename_columns(cr, {
134+ 'res_partner_contact': [
135+ ('photo', 'image'),
136+ ('mobile', 'phone'),
137+ ]
138+ })
139+
140 def migrate_ir_attachment(cr):
141 # Data is now stored in db_datas column and datas is a function field
142 # like in the document module in 6.1. If the db_datas column already
143@@ -175,12 +188,13 @@
144 def remove_obsolete_modules(cr):
145 obsolete_modules = (
146 'base_tools',
147+ 'base_contact',
148 )
149 cr.execute(
150 """
151 UPDATE ir_module_module
152 SET state = 'to remove'
153- WHERE name in %s
154+ WHERE name in %s AND state <> 'uninstalled'
155 """, (obsolete_modules,))
156
157 @openupgrade.migrate()
158@@ -192,6 +206,7 @@
159 )
160 openupgrade.drop_columns(cr, [('ir_actions_todo', 'action_id')])
161 openupgrade.rename_columns(cr, column_renames)
162+ rename_base_contact_columns(cr)
163 openupgrade.rename_xmlids(cr, xmlid_renames)
164 openupgrade.rename_models(cr, model_renames)
165 migrate_ir_attachment(cr)

Subscribers

People subscribed via source and target branches