← Back to team overview

credativ team mailing list archive

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

 

Sylvain LE GAL (GRAP) has proposed merging lp:~sylvain-legal/openupgrade-server/three-functions-for-addons-migration into lp:openupgrade-server.

Requested reviews:
  OpenUpgrade Committers (openupgrade-committers)

For more details, see:
https://code.launchpad.net/~sylvain-legal/openupgrade-server/three-functions-for-addons-migration/+merge/174668

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)
    # [...]
-- 
https://code.launchpad.net/~sylvain-legal/openupgrade-server/three-functions-for-addons-migration/+merge/174668
Your team OpenUpgrade Committers is requested to review the proposed merge of lp:~sylvain-legal/openupgrade-server/three-functions-for-addons-migration into lp:openupgrade-server.
=== modified file 'openerp/openupgrade/openupgrade.py'
--- openerp/openupgrade/openupgrade.py	2013-06-11 08:09:44 +0000
+++ openerp/openupgrade/openupgrade.py	2013-07-15 08:41:25 +0000
@@ -202,6 +202,44 @@
         cr,
         "DELETE FROM wkf WHERE osv = %s", (model,))
 
+def warn_possible_dataloss(cr, pool, old_module, fields):
+    """
+    Use that function in the following case : 
+    if a field of a model was moved from a 'A' module to a 'B' module. 
+    ('B' depend on 'A'), 
+    This function will test if 'B' is installed. 
+    If not, count the number of different value and possibly warn the user.
+    Use orm, so call from the post script.
+    
+    :param old_module: name of the old module
+    :param fields: list of dictionnary with the following keys :
+        'table' : name of the table where the field is.
+        'field' : name of the field that are moving.
+        'new_module' : name of the new module
+    """
+    module_obj = pool.get('ir.module.module')
+    for field in fields: 
+        module_ids = module_obj.search(cr, SUPERUSER_ID, [
+                ('name', '=', field['new_module']),
+                ('state', 'in', ['installed', 'to upgrade', 'to install'])
+            ])
+        if not module_ids: 
+            cr.execute("SELECT count(*) FROM (SELECT %s from %s group by %s) as tmp" \
+                %(field['field'], field['table'], field['field']))
+            row = cr.fetchone()
+            if row[0] == 1: 
+                # not a problem, that field was'nt used. Just a loss of fonctionnality
+                logger.info("'%s' in module '%s' has moved in module " \
+                    "'%s' that is not installed : " \
+                    "Users'll loose fonctionnalities" \
+                    %(field['field'], old_module, field['new_module']))
+            else: 
+                # there is data loss after the migration.
+                logger.warning("'%s' in module '%s' has moved in module " \
+                    "'%s' that is not installed : " \
+                    "There was %s differentes values in this field." \
+                    %(field['field'], old_module, field['new_module'], row[0]))
+
 def set_defaults(cr, pool, default_spec, force=False):
     """
     Set default value. Useful for fields that are newly required. Uses orm, so

=== added file 'openerp/openupgrade/openupgrade_70.py'
--- openerp/openupgrade/openupgrade_70.py	1970-01-01 00:00:00 +0000
+++ openerp/openupgrade/openupgrade_70.py	2013-07-15 08:41:25 +0000
@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+# OpenERP, Open Source Management Solution
+# This migration script copyright (C) Georges Abitbol
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+# This module provides simple tools for openupgrade migration, specific for the
+# 6.1 -> 7.0.
+
+def get_partner_id_from_partner_address_id(cr, partner_address_id):
+    """
+        Get the new partner_id from old partner_address_id.
+        :param partner_address_id : res_partner_address previously used.
+    """
+    cr.execute("""
+        SELECT openupgrade_7_migrated_to_partner_id 
+        FROM res_partner_address
+        WHERE id=%s""" \
+        %(partner_address_id))
+    return cr.fetchone()[0]
+    
+def get_partner_id_from_user_id(cr, user_id):
+    """
+        Get the new partner_id from user_id.
+        :param user_id : user previously used.
+    """
+    cr.execute("""
+        SELECT partner_id 
+        FROM res_users 
+        WHERE id=%s""" \
+        %(user_id))
+    return cr.fetchone()[0]


Follow ups