From 98db1f717a8bb53decf3828e757e2d42587231b6 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Sun, 24 Jun 2012 16:32:32 +0200 Subject: [PATCH] [IMP] Support migration of V5.0 payment types [IMP] Migration 5.0: also rename the old reconcile_id column on statement lines --- account_banking/__terp__.py | 2 +- .../0.1.78/pre-move_payment_type.py | 94 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 account_banking/migrations/0.1.78/pre-move_payment_type.py diff --git a/account_banking/__terp__.py b/account_banking/__terp__.py index b00d161a2..55a33a267 100644 --- a/account_banking/__terp__.py +++ b/account_banking/__terp__.py @@ -25,7 +25,7 @@ ############################################################################## { 'name': 'Account Banking', - 'version': '0.1.62', + 'version': '0.1.78', 'license': 'GPL-3', 'author': 'EduSense BV', 'website': 'http://www.edusense.nl', diff --git a/account_banking/migrations/0.1.78/pre-move_payment_type.py b/account_banking/migrations/0.1.78/pre-move_payment_type.py new file mode 100644 index 000000000..a72c0bb61 --- /dev/null +++ b/account_banking/migrations/0.1.78/pre-move_payment_type.py @@ -0,0 +1,94 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2011-2012 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 . +# +############################################################################## + +import logging +logger = logging.getLogger('Migration: account_banking') + +# methods from openupgrade. Need a proper python library +def table_exists(cr, table): + """ Check whether a certain table or view exists """ + cr.execute( + 'SELECT count(relname) FROM pg_class WHERE relname = %s', + (table,)) + return cr.fetchone()[0] == 1 + +def rename_models(cr, model_spec): + """ + Rename models. Typically called in the pre script. + :param model_spec: a list of tuples (old model name, new model name). + + Use case: if a model changes name, but still implements equivalent + functionality you will want to update references in for instance + relation fields. + + """ + for (old, new) in model_spec: + logger.info("model %s: renaming to %s", + old, new) + cr.execute('UPDATE ir_model_fields SET relation = %s ' + 'WHERE relation = %s', (new, old,)) + cr.execute('UPDATE ir_model_data SET model = %s ' + 'WHERE model = %s', (new, old,)) + +def rename_tables(cr, table_spec): + """ + Rename tables. Typically called in the pre script. + This function also renames the id sequence if it exists and if it is + not modified in the same run. + + :param table_spec: a list of tuples (old table name, new table name). + + """ + # Append id sequences + to_rename = [x[0] for x in table_spec] + for old, new in list(table_spec): + if (table_exists(cr, old + '_id_seq') and + old + '_id_seq' not in to_rename): + table_spec.append((old + '_id_seq', new + '_id_seq')) + for (old, new) in table_spec: + logger.info("table %s: renaming to %s", + old, new) + cr.execute('ALTER TABLE "%s" RENAME TO "%s"' % (old, new,)) + +def rename_columns(cr, column_spec): + """ + Rename table columns. Typically called in the pre script. + + :param column_spec: a hash with table keys, with lists of tuples as values. \ + Tuples consist of (old_name, new_name). + + """ + for table in column_spec.keys(): + for (old, new) in column_spec[table]: + logger.info("table %s, column %s: renaming to %s", + table, old, new) + cr.execute('ALTER TABLE "%s" RENAME "%s" TO "%s"' % (table, old, new,)) + +def migrate(cr, version): + """ + Preserve references to Payment Type resources after renaming to + Payment Mode Type + """ + if version and version.startswith('5.0.'): + rename_models(cr, [['payment.type', 'payment.mode.type']]) + rename_tables(cr, [['payment_type', 'payment_mode_type']]) + rename_columns(cr, {'account_bank_statement_line': [ + ('reconcile_id', 'legacy_bank_statement_reconcile_id')]})