Merge lp:~therp-nl/banking-addons/6.0interactive-support_v5_migration into lp:~therp-nl/banking-addons/6.0-interactive-matching-and-debit-orders

Proposed by Stefan Rijnhart (Opener)
Status: Merged
Merged at revision: 113
Proposed branch: lp:~therp-nl/banking-addons/6.0interactive-support_v5_migration
Merge into: lp:~therp-nl/banking-addons/6.0-interactive-matching-and-debit-orders
Diff against target: 112 lines (+95/-1)
2 files modified
account_banking/__openerp__.py (+1/-1)
account_banking/migrations/0.1.111/pre-move_payment_type.py (+94/-0)
To merge this branch: bzr merge lp:~therp-nl/banking-addons/6.0interactive-support_v5_migration
Reviewer Review Type Date Requested Status
Guewen Baconnier @ Camptocamp (community) Approve
Therp Pending
Review via email: mp+111744@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

Seems fine to me, well done for the neatness and clarity.

About the iteration on dict:
97 + for table in column_spec.keys():

We should avoid to use dict.keys() because it will build a copy of the list of keys before the iteration, while dict.iterkeys() will never build it.

You can even write
  for table in column_spec:
which is a shorthand for column_spec.iterkeys()

Anyway, here there is only a few keys so it won't really change.

It can be merged.

review: Approve
Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

Is the target branch correct?
As you put Therp in the reviewers, I do not merge until a Therp reviewer has approved the proposal.

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

Hi Guewen,

thank you for your comments. The target branch was an intrusive rewrite during 6.0. The migration script was developed for the stable branch but it applied equally to this branch.

Merged it myself as you approved it already.

Thanks
Stefan.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'account_banking/__openerp__.py'
2--- account_banking/__openerp__.py 2012-02-01 15:36:09 +0000
3+++ account_banking/__openerp__.py 2012-06-24 14:44:19 +0000
4@@ -30,7 +30,7 @@
5 ##############################################################################
6 {
7 'name': 'Account Banking',
8- 'version': '0.1.106',
9+ 'version': '0.1.111',
10 'license': 'GPL-3',
11 'author': 'Banking addons community',
12 'website': 'https://launchpad.net/banking-addons',
13
14=== added directory 'account_banking/migrations/0.1.111'
15=== added file 'account_banking/migrations/0.1.111/pre-move_payment_type.py'
16--- account_banking/migrations/0.1.111/pre-move_payment_type.py 1970-01-01 00:00:00 +0000
17+++ account_banking/migrations/0.1.111/pre-move_payment_type.py 2012-06-24 14:44:19 +0000
18@@ -0,0 +1,94 @@
19+# -*- coding: utf-8 -*-
20+##############################################################################
21+#
22+# OpenERP, Open Source Management Solution
23+# This module copyright (C) 2011-2012 Therp BV (<http://therp.nl>)
24+#
25+# This program is free software: you can redistribute it and/or modify
26+# it under the terms of the GNU Affero General Public License as
27+# published by the Free Software Foundation, either version 3 of the
28+# License, or (at your option) any later version.
29+#
30+# This program is distributed in the hope that it will be useful,
31+# but WITHOUT ANY WARRANTY; without even the implied warranty of
32+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33+# GNU Affero General Public License for more details.
34+#
35+# You should have received a copy of the GNU Affero General Public License
36+# along with this program. If not, see <http://www.gnu.org/licenses/>.
37+#
38+##############################################################################
39+
40+import logging
41+logger = logging.getLogger('Migration: account_banking')
42+
43+# methods from openupgrade. Need a proper python library
44+def table_exists(cr, table):
45+ """ Check whether a certain table or view exists """
46+ cr.execute(
47+ 'SELECT count(relname) FROM pg_class WHERE relname = %s',
48+ (table,))
49+ return cr.fetchone()[0] == 1
50+
51+def rename_models(cr, model_spec):
52+ """
53+ Rename models. Typically called in the pre script.
54+ :param model_spec: a list of tuples (old model name, new model name).
55+
56+ Use case: if a model changes name, but still implements equivalent
57+ functionality you will want to update references in for instance
58+ relation fields.
59+
60+ """
61+ for (old, new) in model_spec:
62+ logger.info("model %s: renaming to %s",
63+ old, new)
64+ cr.execute('UPDATE ir_model_fields SET relation = %s '
65+ 'WHERE relation = %s', (new, old,))
66+ cr.execute('UPDATE ir_model_data SET model = %s '
67+ 'WHERE model = %s', (new, old,))
68+
69+def rename_tables(cr, table_spec):
70+ """
71+ Rename tables. Typically called in the pre script.
72+ This function also renames the id sequence if it exists and if it is
73+ not modified in the same run.
74+
75+ :param table_spec: a list of tuples (old table name, new table name).
76+
77+ """
78+ # Append id sequences
79+ to_rename = [x[0] for x in table_spec]
80+ for old, new in list(table_spec):
81+ if (table_exists(cr, old + '_id_seq') and
82+ old + '_id_seq' not in to_rename):
83+ table_spec.append((old + '_id_seq', new + '_id_seq'))
84+ for (old, new) in table_spec:
85+ logger.info("table %s: renaming to %s",
86+ old, new)
87+ cr.execute('ALTER TABLE "%s" RENAME TO "%s"' % (old, new,))
88+
89+def rename_columns(cr, column_spec):
90+ """
91+ Rename table columns. Typically called in the pre script.
92+
93+ :param column_spec: a hash with table keys, with lists of tuples as values. \
94+ Tuples consist of (old_name, new_name).
95+
96+ """
97+ for table in column_spec.keys():
98+ for (old, new) in column_spec[table]:
99+ logger.info("table %s, column %s: renaming to %s",
100+ table, old, new)
101+ cr.execute('ALTER TABLE "%s" RENAME "%s" TO "%s"' % (table, old, new,))
102+
103+def migrate(cr, version):
104+ """
105+ Preserve references to Payment Type resources after renaming to
106+ Payment Mode Type
107+ """
108+ if version and version.startswith('5.0.'):
109+ rename_models(cr, [['payment.type', 'payment.mode.type']])
110+ rename_tables(cr, [['payment_type', 'payment_mode_type']])
111+ rename_columns(cr, {'account_bank_statement_line': [
112+ ('reconcile_id', 'legacy_bank_statement_reconcile_id')]})

Subscribers

People subscribed via source and target branches

to all changes: