Merge lp:~sylvain-legal/openupgrade-server/three-functions-for-addons-migration into lp:openupgrade-server

Proposed by Sylvain LE GAL (GRAP)
Status: Rejected
Rejected by: Holger Brunn (Therp)
Proposed branch: lp:~sylvain-legal/openupgrade-server/three-functions-for-addons-migration
Merge into: lp:openupgrade-server
Diff against target: 100 lines (+85/-0)
2 files modified
openerp/openupgrade/openupgrade.py (+38/-0)
openerp/openupgrade/openupgrade_70.py (+47/-0)
To merge this branch: bzr merge lp:~sylvain-legal/openupgrade-server/three-functions-for-addons-migration
Reviewer Review Type Date Requested Status
Stefan Rijnhart (Opener) Needs Fixing
Review via email: mp+174668@code.launchpad.net

Description of the change

1/ Add a file openupgrade_70.py containing specific function for the migration of modules from xx -> 7.0.
def get_partner_id_from_partner_address_id()
def get_partner_id_from_user_id()

2/ Add a function warn_possible_dataloss() in openupgrade.py to inform the user who realize the migration that field(s) moved from a installed module to an uninstalled module. (and so there is possible data loss)

Typical use :
openupgrade-addons / product / ... / post-migration.py

# [...]
possible_dataloss_fields = [
    {'table' : 'product_template', 'field' : 'loc_case', 'new_module' : 'stock',},
    {'table' : 'product_template', 'field' : 'purchase_ok', 'new_module' : 'purchase',},
# [...]
]

@openupgrade.migrate()
def migrate(cr, version):
    # [...]
    openupgrade.warn_possible_dataloss(cr, pool, 'product', possible_dataloss_fields)
    # [...]

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

Again, the question of having copyright assigned to George Arbitbol pops up.

warn_possible_dataloss: Can you tell me the exact use case for this function? I noticed that in 7.0, a number of functionalies have been split off to separate 'glue' modules. For example, there is now sale_stock for functionality that was first in the sale and stock modules. However, this module will be installed automatically if sale and stock are both installed, which will be the case if you have the sale module installed in 6.1 as it still depended on stock.

The other methods seem very useful to me. I have three minor comments for them.

l.58: I think mentioning the year is useful for copyright assignments.

    # This module copyright (C) 2013 Georges Abitbol

l.87: you are not supposed to perform variable substitution inline when using cr.execute() (unless you are substituting table names as there is no other way). The following should work:

WHERE id=%s""",
(partner_address_id,))

Same for l. 99.

review: Needs Fixing
4619. By Sylvain LE GAL (GRAP)

[REF] change the call params of cr.execute() function.

Revision history for this message
Sylvain LE GAL (GRAP) (sylvain-legal) wrote :

Hi Stefan,

1. First sorry for the copyright. I just didn't have idea what to write because for me there is no one Copyright but many. for exemple I write something, you review it and fix something else. My point of view is that is a community copyright. But ok.
--> updated. (rev 4619)

2. For the warn_possible_dataloss. For me the use case is the following :
- I provide a 6.1 database with 'product' module installed but without 'purchase'.
- The users fill the field 'purchase_ok' because it's available. (some product values to True, other to False)
- I realize the migration to 7.0.
- I clean up the DB after. (deleting unused field, like you said in your comment https://answers.launchpad.net/openupgrade-server/+question/230898)
- There is a Data Loss.

The warn_possible_dataloss is usefull for that. After the migration, reading log, (it's just warning), maybe i'll install 'purchase' before cleaning up the DB. (It's a choice)

I agree to say that it's a kind of theorical problem because using 'purchase_ok' without 'purchase' module installed is not very usefull but it's possible problem.

3. about cr.execute() call, thank you.
--> updated. (rev 4619)

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

Thank you for your explanation (and your technical changes which look good). I personally still think that it should suffice for the administrator to review the list of columns to be removed once we have the cleanup module ready. In a sense, removing each of those columns constitutes a potential data loss, but then again, if a model column is dangling after the upgrade, it is by definition not used in the (ORM) system.

I actually traced your use case to the invoicing part of OpenERP, as in 6.1 there still is a filter on purchasable products in the case of a supplier invoice even if the purchase module is not installed. That has now been dropped, see lp:1171794. So for this particular use case, when only invoicing is used, the data loss is indeed theoretic. Even if one decides to install the purchase module for the sake of managing this field, it is still not applied when using invoicing without purchase orders.

We could consider putting up hints in the cleanup module if we have information available that a column has moved to a different module, but even that could lead to a false sense of security in case of columns that moved between modules in a way that is not yet detected by the analysis process. But with a sensible disclaimer, that I would find useful.

So far for my two cents, what do other people think?

Revision history for this message
Holger Brunn (Therp) (hbrunn) wrote :

Lacking the cleanup module, I think this function is very useful. But shouldn't it be able to figure out by itself where the field come from now?

I'd much prefer actually to have the cleanup module and a function that allows us to add messages to the administrator with a severity on a per module, model and field level.

How about the compromise: Write this function first, what it currently does is write into the log, and some time in the future to the cleanup-module's messages table?

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

Holger, thanks. Let's then keep the warn_possible_dataloss method and canalize the interesting logs that it generates.

I think we should work this one out one day but until then, I have a simplistic starting point that will at least allow us to put messages in the database when we are ready for that.

https://code.launchpad.net/~therp-nl/openupgrade-server/7.0-API-logging/+merge/175132

Holger, if you can live with this for now, we can go ahead with this proposal, only requiring a change in l.41 of the diff (I think l.35 is fine in an INFO log but I would not bother OpenERP administrators with such messages in the polished upgrade report, by which I am ironically demonstrating the need for levelled logging like you suggest).

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

Sylvain, as you have indicated that you are on holidays now, I took the liberty to refactor your contribution here: https://code.launchpad.net/~therp-nl/openupgrade-server/three-functions-for-addons-migration/+merge/175790

Revision history for this message
Holger Brunn (Therp) (hbrunn) wrote :

I'll set this one to superseded and discuss the code in Stefan's MP

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openerp/openupgrade/openupgrade.py'
2--- openerp/openupgrade/openupgrade.py 2013-06-11 08:09:44 +0000
3+++ openerp/openupgrade/openupgrade.py 2013-07-15 10:42:33 +0000
4@@ -202,6 +202,44 @@
5 cr,
6 "DELETE FROM wkf WHERE osv = %s", (model,))
7
8+def warn_possible_dataloss(cr, pool, old_module, fields):
9+ """
10+ Use that function in the following case :
11+ if a field of a model was moved from a 'A' module to a 'B' module.
12+ ('B' depend on 'A'),
13+ This function will test if 'B' is installed.
14+ If not, count the number of different value and possibly warn the user.
15+ Use orm, so call from the post script.
16+
17+ :param old_module: name of the old module
18+ :param fields: list of dictionnary with the following keys :
19+ 'table' : name of the table where the field is.
20+ 'field' : name of the field that are moving.
21+ 'new_module' : name of the new module
22+ """
23+ module_obj = pool.get('ir.module.module')
24+ for field in fields:
25+ module_ids = module_obj.search(cr, SUPERUSER_ID, [
26+ ('name', '=', field['new_module']),
27+ ('state', 'in', ['installed', 'to upgrade', 'to install'])
28+ ])
29+ if not module_ids:
30+ cr.execute("SELECT count(*) FROM (SELECT %s from %s group by %s) as tmp" \
31+ %(field['field'], field['table'], field['field']))
32+ row = cr.fetchone()
33+ if row[0] == 1:
34+ # not a problem, that field was'nt used. Just a loss of fonctionnality
35+ logger.info("'%s' in module '%s' has moved in module " \
36+ "'%s' that is not installed : " \
37+ "Users'll loose fonctionnalities" \
38+ %(field['field'], old_module, field['new_module']))
39+ else:
40+ # there is data loss after the migration.
41+ logger.warning("'%s' in module '%s' has moved in module " \
42+ "'%s' that is not installed : " \
43+ "There was %s differentes values in this field." \
44+ %(field['field'], old_module, field['new_module'], row[0]))
45+
46 def set_defaults(cr, pool, default_spec, force=False):
47 """
48 Set default value. Useful for fields that are newly required. Uses orm, so
49
50=== added file 'openerp/openupgrade/openupgrade_70.py'
51--- openerp/openupgrade/openupgrade_70.py 1970-01-01 00:00:00 +0000
52+++ openerp/openupgrade/openupgrade_70.py 2013-07-15 10:42:33 +0000
53@@ -0,0 +1,47 @@
54+# -*- coding: utf-8 -*-
55+##############################################################################
56+#
57+# OpenERP, Open Source Management Solution
58+# This migration script copyright (C) 2013-today Sylvain LE GAL
59+#
60+# This program is free software: you can redistribute it and/or modify
61+# it under the terms of the GNU Affero General Public License as
62+# published by the Free Software Foundation, either version 3 of the
63+# License, or (at your option) any later version.
64+#
65+# This program is distributed in the hope that it will be useful,
66+# but WITHOUT ANY WARRANTY; without even the implied warranty of
67+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
68+# GNU Affero General Public License for more details.
69+#
70+# You should have received a copy of the GNU Affero General Public License
71+# along with this program. If not, see <http://www.gnu.org/licenses/>.
72+#
73+##############################################################################
74+
75+# This module provides simple tools for openupgrade migration, specific for the
76+# 6.1 -> 7.0.
77+
78+def get_partner_id_from_partner_address_id(cr, partner_address_id):
79+ """
80+ Get the new partner_id from old partner_address_id.
81+ :param partner_address_id : res_partner_address previously used.
82+ """
83+ cr.execute("""
84+ SELECT openupgrade_7_migrated_to_partner_id
85+ FROM res_partner_address
86+ WHERE id=%s""",
87+ (partner_address_id,))
88+ return cr.fetchone()[0]
89+
90+def get_partner_id_from_user_id(cr, user_id):
91+ """
92+ Get the new partner_id from user_id.
93+ :param user_id : user previously used.
94+ """
95+ cr.execute("""
96+ SELECT partner_id
97+ FROM res_users
98+ WHERE id=%s""",
99+ (user_id,))
100+ return cr.fetchone()[0]

Subscribers

People subscribed via source and target branches