Merge lp:~therp-nl/banking-addons/6.0-support_v5_migration into lp:banking-addons/6.0

Proposed by Stefan Rijnhart (Opener)
Status: Merged
Merged at revision: 79
Proposed branch: lp:~therp-nl/banking-addons/6.0-support_v5_migration
Merge into: lp:banking-addons/6.0
Diff against target: 113 lines (+95/-1)
2 files modified
account_banking/__terp__.py (+1/-1)
account_banking/migrations/0.1.78/pre-move_payment_type.py (+94/-0)
To merge this branch: bzr merge lp:~therp-nl/banking-addons/6.0-support_v5_migration
Reviewer Review Type Date Requested Status
Banking Addons Core Editors Pending
Review via email: mp+111743@code.launchpad.net
To post a comment you must log in.

Preview Diff

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

Subscribers

People subscribed via source and target branches