← Back to team overview

credativ team mailing list archive

[Merge] lp:~therp-nl/openupgrade-server/7.0-base into lp:openupgrade-server

 

Stefan Rijnhart (Therp) has proposed merging lp:~therp-nl/openupgrade-server/7.0-base into lp:openupgrade-server.

Commit message:
[ADD] Migration scripts for 'base' module

Requested reviews:
  OpenUpgrade Committers (openupgrade-committers)

For more details, see:
https://code.launchpad.net/~therp-nl/openupgrade-server/7.0-base/+merge/150086

This branch contains the preliminary migration scripts for the base module. They have been roughly tested on a 6.1 database with demo data installed.

For testing purposes, please make sure to have the following branches merged as well:

lp:~therp-nl/openupgrade-server/7.0-do_not_raise_premature_view_errors
lp:~therp-nl/openupgrade-server/7.0-lp1131653_workaround
-- 
https://code.launchpad.net/~therp-nl/openupgrade-server/7.0-base/+merge/150086
Your team OpenUpgrade Committers is requested to review the proposed merge of lp:~therp-nl/openupgrade-server/7.0-base into lp:openupgrade-server.
=== added file 'openerp/addons/base/migrations/7.0.1.3/post-migration.py'
--- openerp/addons/base/migrations/7.0.1.3/post-migration.py	1970-01-01 00:00:00 +0000
+++ openerp/addons/base/migrations/7.0.1.3/post-migration.py	2013-02-22 16:10:26 +0000
@@ -0,0 +1,185 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    This migration script copyright (C) 2012-2013 Therp BV (<http://therp.nl>)
+#
+#    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/>.
+#
+##############################################################################
+
+from openupgrade import openupgrade
+from openerp import pooler, SUPERUSER_ID
+
+force_defaults = {
+    'ir.mail_server': [('active', True)],
+    'ir.model.access': [('active', True)],
+    'ir.rule': [('active', True)],
+    'res.company': [('custom_footer', True)],
+    # We'll have to adapt the default for is_company in specific
+    # modules. For instance, a migration script for hr
+    # could reset is_company for partners associated with
+    # employees
+    'res.partner': [('is_company', True)],
+}
+
+def migrate_ir_translation(cr):
+    openupgrade.logged_query(
+        cr,
+        """ UPDATE ir_translation
+            SET state = 'translated'
+            WHERE length(value) > 0;
+        """)
+    openupgrade.logged_query(
+        cr,
+        """ UPDATE ir_translation
+            SET state = 'to_translate'
+            WHERE state is NULL;
+        """)
+
+def migrate_company(cr):
+    """
+    Copy char value to new text column
+    """
+    cr.execute(
+        """ UPDATE res_company
+            SET rml_footer = rml_footer1
+        """)
+
+def migrate_partner_address(cr, pool):
+    """ res.partner.address is obsolete. Move existing data to
+    partner
+
+    TODO: break hard when base_contact is installed
+    """
+    partner_obj = pool.get('res.partner')
+    cr.execute(
+        "ALTER TABLE res_partner_address "
+        "ADD column openupgrade_7_migrated_to_partner_id "
+        " INTEGER")
+    cr.execute(
+        "ALTER TABLE res_partner_address ADD FOREIGN KEY "
+        "(openupgrade_7_migrated_to_partner_id) "
+        "REFERENCES res_partner ON DELETE SET NULL")
+    fields = [
+        'id', 'birthdate', 'city', 'country_id', 'email', 'fax', 'function',
+        'mobile', 'phone', 'state_id', 'street', 'street2', 'type', 'zip',
+        'partner_id', 'name',
+        ]
+    partner_found = []
+    processed_ids = []
+
+    def create_partner(address_id, vals, defaults):
+        """
+        Create a partner from an address. Update the vals
+        with the defaults only if the keys do not occur
+        already in vals. Register the created partner_id
+        on the obsolete address table
+        """
+        for key in defaults:
+            if key not in vals:
+                vals[key] = defaults[key]
+
+        partner_id = partner_obj.create(cr, SUPERUSER_ID, vals)
+        cr.execute(
+            "UPDATE res_partner_address "
+            "SET openupgrade_7_migrated_to_partner_id = %s "
+            "WHERE id = %s",
+            (partner_id, address_id))
+
+    def process_address_type(cr, whereclause, args=None):
+        """
+        Migrate addresses to partners, based on sql WHERE clause
+        """
+        cr.execute(
+            "SELECT " + ', '.join(fields) + " FROM res_partner_address "
+            "WHERE " + whereclause, args or ())
+        for row in cr.fetchall():
+            row_cleaned = [val or False for val in row]
+            address = dict(zip(fields, row_cleaned))
+            partner_vals = address.copy()
+            partner_defaults = {
+                # list of values that we should not overwrite
+                # in existing partners
+                'customer': False,
+                'is_company': address['type'] != 'contact',
+                'type': address['type'],
+                'name': address['name'] or '/',
+                }
+            for f in ['name', 'id', 'type', 'partner_id']:
+                del partner_vals[f]
+            if not address['partner_id']:
+                # Dangling addresses, create with not is_company,
+                # not supplier and not customer
+                create_partner(address['id'], partner_vals, partner_defaults)
+            else:
+                if address['partner_id'] not in partner_found:
+                    # Main partner address
+                    partner_obj.write(
+                        cr, SUPERUSER_ID, address['partner_id'], partner_vals)
+                    partner_found.append(address['partner_id'])
+                else:
+                    # any following address for an existing partner
+                    partner_vals.update({
+                            'is_company': False,
+                            'parent_id': address['partner_id']})
+                    create_partner(
+                        address['id'], partner_vals, partner_defaults)
+            processed_ids.append(address['id'])
+
+    # Process all addresses, default type first 
+    process_address_type(cr, "type = 'default'")
+    process_address_type(cr, "type IS NULL OR type = ''")
+    process_address_type(cr, "id NOT IN %s", (tuple(processed_ids),))
+
+def update_users_partner(cr, pool):
+    """ 
+    Now that the fields exist on the model, finish
+    the work of create_users_partner() in the pre script
+    """
+    partner_obj = pool.get('res.partner')
+    # Effectively remove excess partner for user admin
+    # Maybe better to exclude user_root while creating?
+    cr.execute(
+        "SELECT openupgrade_7_created_partner_id "
+        "FROM res_users "
+        "WHERE openupgrade_7_created_partner_id IS NOT NULL "
+        "AND openupgrade_7_created_partner_id != partner_id")
+    partner_obj.unlink(
+        cr, SUPERUSER_ID,
+        [row[1] for row in cr.fetchall()])
+    cr.execute(
+        # Can't use orm as these fields to not appear on the model
+        # anymore
+        "SELECT id, openupgrade_7_created_partner_id, context_lang, "
+        "context_tz, " + openupgrade.get_legacy_name('user_email') + " "
+        "FROM res_users "
+        "WHERE openupgrade_7_created_partner_id IS NOT NULL")
+    for row in cr.fetchall():
+        partner_vals = {
+            'user_ids': [(4, row[0])],
+            'lang': row[2] or False,
+            'tz': row[3] or False,
+            'email': row[4] or False,
+            }
+        partner_obj.write(cr, SUPERUSER_ID, row[1], partner_vals)
+
+@openupgrade.migrate()
+def migrate(cr, version):
+    pool = pooler.get_pool(cr.dbname)
+    openupgrade.set_defaults(cr, pool, force_defaults, force=True)
+    migrate_ir_translation(cr)
+    migrate_company(cr)
+    migrate_partner_address(cr, pool)
+    update_users_partner(cr, pool)

=== added file 'openerp/addons/base/migrations/7.0.1.3/pre-migration.py'
--- openerp/addons/base/migrations/7.0.1.3/pre-migration.py	1970-01-01 00:00:00 +0000
+++ openerp/addons/base/migrations/7.0.1.3/pre-migration.py	2013-02-22 16:10:26 +0000
@@ -0,0 +1,146 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    This migration script copyright (C) 2012 Therp BV (<http://therp.nl>)
+#
+#    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/>.
+#
+##############################################################################
+
+# Maybe: investigate impact of 'model' field on ir.translation
+# Ignored: removal of integer_big which openerp 6.1 claims is currently unused
+
+from openupgrade import openupgrade
+
+module_namespec = [
+    # This is a list of tuples (old module name, new module name)
+    ('account_coda', 'l10n_be_coda'),
+    ('base_crypt', 'auth_crypt'),
+    ('mrp_subproduct', 'mrp_byproduct'),
+    ('users_ldap', 'auth_ldap'),
+    ('wiki', 'document_page'),
+]
+
+column_renames = {
+    # login_date: orm can map timestamps to date
+    'res_users': [
+        ('date', 'login_date'),
+        ('user_email', openupgrade.get_legacy_name('user_email')),
+        ]
+}
+
+xmlid_renames = []
+
+def migrate_ir_attachment(cr):
+    # Data is now stored in db_datas column
+    # and datas is a function field like in the document module
+    if not openupgrade.column_exists(cr, 'ir_attachment', 'db_datas'):
+        openupgrade.rename_columns(
+            cr, {'ir_attachment': [('datas', 'db_datas')]})
+
+def update_base_sql(cr):
+    """
+    Inject snippets of 
+    openerp/addons/base/base.sql
+    """
+    cr.execute("""
+CREATE TABLE ir_model_constraint (
+    id serial NOT NULL,
+    create_uid integer,
+    create_date timestamp without time zone,
+    write_date timestamp without time zone,
+    write_uid integer,
+    date_init timestamp without time zone,
+    date_update timestamp without time zone,
+    module integer NOT NULL references ir_module_module on delete restrict,
+    model integer NOT NULL references ir_model on delete restrict,
+    type character varying(1) NOT NULL,
+    name character varying(128) NOT NULL
+);
+CREATE TABLE ir_model_relation (
+    id serial NOT NULL,
+    create_uid integer,
+    create_date timestamp without time zone,
+    write_date timestamp without time zone,
+    write_uid integer,
+    date_init timestamp without time zone,
+    date_update timestamp without time zone,
+    module integer NOT NULL references ir_module_module on delete restrict,
+    model integer NOT NULL references ir_model on delete restrict,
+    name character varying(128) NOT NULL
+);  
+""")
+
+def create_users_partner(cr):
+    """
+    Users now have an inherits on res.partner.
+    Transferred fields include lang, tz and email
+    but these fields do not exist on the partner table
+    at this point. We'll pick this up in the post
+    script.
+
+    If other modules define defaults on the partner
+    model, their migration scripts should put them
+    into place for these entries.
+
+    If other modules set additional columns to
+    required, the following will break. We may
+    want to have a look at disabling triggers
+    at that point,
+    """
+    if not openupgrade.column_exists(
+        cr, 'res_users', 'partner_id'):
+        cr.execute(
+            "ALTER TABLE res_users "
+            "ADD column partner_id "
+            " INTEGER")
+        cr.execute(
+            "ALTER TABLE res_users ADD FOREIGN KEY "
+            "(partner_id) "
+            "REFERENCES res_partner ON DELETE SET NULL")
+    cr.execute(
+        "ALTER TABLE res_users "
+        "ADD column openupgrade_7_created_partner_id "
+        " INTEGER")
+    cr.execute(
+        "ALTER TABLE res_users ADD FOREIGN KEY "
+        "(openupgrade_7_created_partner_id) "
+        "REFERENCES res_partner ON DELETE SET NULL")
+    cr.execute(
+        "SELECT id, name, active FROM res_users "
+        "WHERE partner_id IS NULL")
+    for row in cr.fetchall():
+        cr.execute(
+            "INSERT INTO res_partner "
+            "(name, active) "
+            "VALUES(%s,%s) RETURNING id", row[1:])
+        partner_id = cr.fetchone()[0]
+        cr.execute(
+            "UPDATE res_users "
+            "SET partner_id = %s, "
+            "openupgrade_7_created_partner_id = %s "
+            "WHERE id = %s", (partner_id, partner_id, row[0]))
+
+@openupgrade.migrate()
+def migrate(cr, version):
+    update_base_sql(cr)
+    openupgrade.update_module_names(
+        cr, module_namespec
+        )
+    openupgrade.drop_columns(cr, [('ir_actions_todo', 'action_id')])
+    openupgrade.rename_columns(cr, column_renames)
+    openupgrade.rename_xmlids(cr, xmlid_renames)
+    migrate_ir_attachment(cr)
+    create_users_partner(cr)

=== added file 'openerp/addons/base/migrations/7.0.1.3/user_notes.txt'
--- openerp/addons/base/migrations/7.0.1.3/user_notes.txt	1970-01-01 00:00:00 +0000
+++ openerp/addons/base/migrations/7.0.1.3/user_notes.txt	2013-02-22 16:10:26 +0000
@@ -0,0 +1,9 @@
+Addresses are migrated to partners. For further reference, a new column openupgrade_7_migrated_to_partner_id will contain the newly created partner that originates from the entry in the old res_partner_address_table.
+
+Technical changes in the table layout of users and attachments are processed.
+
+Partner events are dropped
+
+Mail servers and access rules can now be deactivated.
+
+Users are now automatically a partner. This script creates partners for existing users.


Follow ups