← Back to team overview

credativ team mailing list archive

lp:~therp-nl/openupgrade-server/three-functions-for-addons-migration into lp:openupgrade-server

 

Stefan Rijnhart (Therp) has proposed merging lp:~therp-nl/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/~therp-nl/openupgrade-server/three-functions-for-addons-migration/+merge/175790

Supersedes https://code.launchpad.net/~sylvain-legal/openupgrade-server/three-functions-for-addons-migration/+merge/174668 and https://code.launchpad.net/~therp-nl/openupgrade-server/7.0-API-logging/+merge/175132

-- 
https://code.launchpad.net/~therp-nl/openupgrade-server/three-functions-for-addons-migration/+merge/175790
Your team OpenUpgrade Committers is requested to review the proposed merge of lp:~therp-nl/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-19 10:16:42 +0000
@@ -202,6 +202,49 @@
         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 dictionary 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 wasn't used.
+                # Just a loss of functionality
+                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.
+                message(
+                    cr, old_module,
+                    "Field '%s' has moved to module "
+                    "'%s' that is not installed: "
+                    "There was %s different values in this field.",
+                    field['field'], 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
@@ -331,6 +374,15 @@
     return 'openupgrade_legacy_'+('_').join(
         map(str, release.version_info[0:2]))+'_'+original_name
 
+def message(cr, module, message, *args, **kwargs):
+    """
+    Log handler for non-critical notifications about the upgrade.
+    To be extended with logging to a table for reporting purposes.
+    """
+    argslist = list(args)
+    argslist.insert(0, module)
+    logger.warn("Module %s: " + message, *argslist, **kwargs)
+
 def migrate():
     """
     This is the decorator for the migrate() function

=== 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-19 10:16:42 +0000
@@ -0,0 +1,63 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+# OpenERP, Open Source Management Solution
+# This module copyright (C) 2013 Sylvain LE GAL
+#                       (C) 2013 Therp BV
+#
+# 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.
+
+from openerp.openupgrade import openupgrade
+from openerp import SUPERUSER_ID
+
+def set_partner_id_from_partner_address_id(
+        cr, pool, model_name, partner_field, address_field, table=None):
+    """
+    Set the new partner_id on any table with migrated contact ids
+
+    :param model_name: the model name of the target table
+    :param partner_field: the column in the target model's table \
+                          that will store the new partner when found
+    :param address_field: the legacy field in the model's table \
+                    that contains the old address in the model's table
+    :param table: override the target model's table name in case it was renamed               
+    :returns: nothing
+    """
+    model = pool.get(model_name)
+    table = table or model._table
+    # Cannot use cursor's string substitution for table names
+    cr.execute("""
+        SELECT target.id, address.openupgrade_7_migrated_to_partner_id
+        FROM %s as target,
+             res_partner_address as address
+        WHERE address.id = target.%s""" % (table, address_field))
+    for row in cr.fetchall():
+        model.write(cr, row[0], SUPERUSER_ID, {partner_field: row[1]})
+    
+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