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
=== modified file 'openerp/addons/base/migrations/7.0.1.3/post-migration.py'
--- openerp/addons/base/migrations/7.0.1.3/post-migration.py 2013-10-21 08:59:09 +0000
+++ openerp/addons/base/migrations/7.0.1.3/post-migration.py 2013-11-18 15:34:37 +0000
@@ -56,11 +56,69 @@
56 SET rml_footer = rml_footer156 SET rml_footer = rml_footer1
57 """)57 """)
5858
59def migrate_base_contact(cr):
60 """
61 Move entries of res_partner_contact into res_partner
62 """
63 cr.execute("SELECT * FROM ir_module_module WHERE name = 'base_contact' and state = 'to remove';")
64 if not cr.fetchall():
65 return
66 fields = [
67 'create_date',
68 'name',
69 'website',
70 'image',
71 'active',
72 'comment',
73 'title',
74 'phone',
75 'country_id',
76 'email',
77 'birthdate',
78 'lang',
79 'parent_id',
80 ]
81 # Add lang from lang_id
82 openupgrade.logged_query(cr, "ALTER TABLE res_partner_contact ADD COLUMN lang character varying(5);")
83 openupgrade.logged_query(cr, """
84 UPDATE res_partner_contact
85 SET lang = (SELECT code
86 FROM res_lang l
87 WHERE lang_id = l.id
88 LIMIT 1);""")
89 # Add parent_id
90 openupgrade.logged_query(cr, "ALTER TABLE res_partner_contact ADD COLUMN parent_id integer;")
91 openupgrade.logged_query(cr, """
92 UPDATE res_partner_contact
93 SET parent_id = (SELECT openupgrade_7_migrated_to_partner_id
94 FROM res_partner_address a
95 WHERE id = a.contact_id
96 LIMIT 1);""")
97 # Make sure fields exist
98 cr.execute(
99 "SELECT column_name "
100 "FROM information_schema.columns "
101 "WHERE table_name = 'res_partner_contact';")
102 available_fields = set(i[0] for i in cr.fetchall())
103 lost_fields = set(fields) - available_fields
104 if lost_fields:
105 openupgrade.logger.warning("""\
106The following columns are not present in the table of %s: %s.
107
108This can be the case if an additional module installed on your database changes the type of a
109regular column to a non-stored function or related field.
110""", 'res_partner_contact', ", ".join(lost_fields))
111 fields = list(available_fields.intersection(fields))
112 # Move data
113 openupgrade.logged_query(cr, """
114 INSERT INTO res_partner (%s)
115 SELECT %s
116 FROM res_partner_contact;""" % (", ".join(fields + ['customer', 'is_company']),
117 ", ".join(fields + ['TRUE', 'FALSE'])))
118
59def migrate_partner_address(cr, pool):119def migrate_partner_address(cr, pool):
60 """ res.partner.address is obsolete. Move existing data to120 """ res.partner.address is obsolete. Move existing data to
61 partner121 partner
62
63 TODO: break hard when base_contact is installed
64 """122 """
65 partner_obj = pool.get('res.partner')123 partner_obj = pool.get('res.partner')
66 cr.execute(124 cr.execute(
@@ -82,6 +140,22 @@
82 partner_found = []140 partner_found = []
83 processed_ids = []141 processed_ids = []
84142
143 # Make sure fields exist
144 cr.execute(
145 "SELECT column_name "
146 "FROM information_schema.columns "
147 "WHERE table_name = 'res_partner_address';")
148 available_fields = set(i[0] for i in cr.fetchall())
149 lost_fields = set(fields) - available_fields
150 if lost_fields:
151 openupgrade.logger.warning("""\
152The following columns are not present in the table of %s: %s.
153
154This can be the case if an additional module installed on your database changes the type of a
155regular column to a non-stored function or related field.
156""", 'res_partner_address', ", ".join(lost_fields))
157 fields = available_fields.intersection(fields)
158
85 def set_address_partner(address_id, partner_id):159 def set_address_partner(address_id, partner_id):
86 cr.execute(160 cr.execute(
87 "UPDATE res_partner_address "161 "UPDATE res_partner_address "
@@ -107,8 +181,9 @@
107 """181 """
108 Migrate addresses to partners, based on sql WHERE clause182 Migrate addresses to partners, based on sql WHERE clause
109 """183 """
110 cr.execute(184 openupgrade.logged_query(cr, "\n"
111 "SELECT " + ', '.join(fields) + " FROM res_partner_address "185 "SELECT " + ', '.join(fields) + "\n"
186 "FROM res_partner_address\n"
112 "WHERE " + whereclause, args or ())187 "WHERE " + whereclause, args or ())
113 for row in cr.fetchall():188 for row in cr.fetchall():
114 row_cleaned = [val or False for val in row]189 row_cleaned = [val or False for val in row]
@@ -222,6 +297,7 @@
222 migrate_ir_translation(cr)297 migrate_ir_translation(cr)
223 migrate_company(cr)298 migrate_company(cr)
224 migrate_partner_address(cr, pool)299 migrate_partner_address(cr, pool)
300 migrate_base_contact(cr)
225 update_users_partner(cr, pool)301 update_users_partner(cr, pool)
226 reset_currency_companies(cr, pool)302 reset_currency_companies(cr, pool)
227 migrate_res_company_logo(cr, pool)303 migrate_res_company_logo(cr, pool)
228304
=== modified file 'openerp/addons/base/migrations/7.0.1.3/pre-migration.py'
--- openerp/addons/base/migrations/7.0.1.3/pre-migration.py 2013-10-25 19:54:27 +0000
+++ openerp/addons/base/migrations/7.0.1.3/pre-migration.py 2013-11-18 15:34:37 +0000
@@ -63,6 +63,19 @@
63 cr,63 cr,
64 "UPDATE ir_module_module SET demo = false")64 "UPDATE ir_module_module SET demo = false")
6565
66def rename_base_contact_columns(cr):
67 """
68 Rename columns only if res_partner_contact is installed
69 """
70 cr.execute("SELECT * FROM information_schema.tables WHERE table_name = 'res_partner_contact';")
71 if cr.fetchall():
72 openupgrade.rename_columns(cr, {
73 'res_partner_contact': [
74 ('photo', 'image'),
75 ('mobile', 'phone'),
76 ]
77 })
78
66def migrate_ir_attachment(cr):79def migrate_ir_attachment(cr):
67 # Data is now stored in db_datas column and datas is a function field80 # Data is now stored in db_datas column and datas is a function field
68 # like in the document module in 6.1. If the db_datas column already81 # like in the document module in 6.1. If the db_datas column already
@@ -175,12 +188,13 @@
175def remove_obsolete_modules(cr):188def remove_obsolete_modules(cr):
176 obsolete_modules = (189 obsolete_modules = (
177 'base_tools',190 'base_tools',
191 'base_contact',
178 )192 )
179 cr.execute(193 cr.execute(
180 """194 """
181 UPDATE ir_module_module195 UPDATE ir_module_module
182 SET state = 'to remove'196 SET state = 'to remove'
183 WHERE name in %s197 WHERE name in %s AND state <> 'uninstalled'
184 """, (obsolete_modules,))198 """, (obsolete_modules,))
185199
186@openupgrade.migrate()200@openupgrade.migrate()
@@ -192,6 +206,7 @@
192 )206 )
193 openupgrade.drop_columns(cr, [('ir_actions_todo', 'action_id')])207 openupgrade.drop_columns(cr, [('ir_actions_todo', 'action_id')])
194 openupgrade.rename_columns(cr, column_renames)208 openupgrade.rename_columns(cr, column_renames)
209 rename_base_contact_columns(cr)
195 openupgrade.rename_xmlids(cr, xmlid_renames)210 openupgrade.rename_xmlids(cr, xmlid_renames)
196 openupgrade.rename_models(cr, model_renames)211 openupgrade.rename_models(cr, model_renames)
197 migrate_ir_attachment(cr)212 migrate_ir_attachment(cr)

Subscribers

People subscribed via source and target branches