Merge lp:~savoirfairelinux-openerp/openerp-mgmtsystem/openupgrade70 into lp:openerp-mgmtsystem

Status: Merged
Merged at revision: 97
Proposed branch: lp:~savoirfairelinux-openerp/openerp-mgmtsystem/openupgrade70
Merge into: lp:openerp-mgmtsystem
Diff against target: 561 lines (+479/-25)
8 files modified
document_page_environment_manual/migrations/7.0.1.0/pre-migration.py (+51/-0)
document_page_environmental_aspect/migrations/7.0.1.0/post-migration.py (+61/-0)
document_page_procedure/migrations/7.0.1.0/post-migration.py (+62/-0)
document_page_quality_manual/migrations/7.0.1.0/pre-migration.py (+51/-0)
document_page_work_instructions/migrations/7.0.1.0/post-migration.py (+61/-0)
mgmtsystem_action/migrations/7.0.1.0/post-migration.py (+82/-0)
mgmtsystem_action/migrations/7.0.1.0/pre-migration.py (+52/-0)
mgmtsystem_nonconformity/migrations/7.0.1.0/post-migration.py (+59/-25)
To merge this branch: bzr merge lp:~savoirfairelinux-openerp/openerp-mgmtsystem/openupgrade70
Reviewer Review Type Date Requested Status
Guewen Baconnier @ Camptocamp code review 2013-11-18 Approve on 2014-03-21
Stefan Rijnhart (Opener) 2014-01-16 Approve on 2014-03-15
Maxime Chambreuil (http://www.savoirfairelinux.com) code review 2013-10-25 Approve on 2013-11-08
Review via email: mp+192730@code.launchpad.net

Description of the change

This patch adds openupgrade migration scripts which should migrate mgmtsystem documents, actions and nonconformities properly.

To post a comment you must log in.

Just to say that I see no need to import from openupgrade here. Try to do without. You will lose some logging, but at the same time retain compatibility with parties who migrate using the OPW contract. When in doubt, copy the applied functions from openupgrade into your scripts.

Do you mean this line?
from openupgrade.openupgrade import logged_query

I could just replace logged_query with openupgrade.logged_query in that case.

Well, every line refering to OpenUpgrade actually. You will want to keep the migration of openupgrade_legacy_7_0_stage_id, but you would need to check if the field exists.

95. By Sandy Carter (http://www.savoirfairelinux.com) on 2013-10-31

[IMP] OpenUpgrade: mgmt_action: more states handled and legacy name fetched properly

96. By Sandy Carter (http://www.savoirfairelinux.com) on 2013-11-07

[FIX] Migration scripts no longer fully dependent on openupgrade

Adapted scripts to work identically with or without openupgrade.

review: Approve (code review)

While you make the method to generate the legacy column name available without OpenUpgrade, you should check if the column exists around l.411. If the database is migrated otherwise, there will be no such column.

What is the use of checking if the openupgrade import is available if you define the functions yourself anyway as a fallback?

review: Needs Fixing
97. By Sandy Carter (http://www.savoirfairelinux.com) on 2014-03-14

Check that column exists

98. By Sandy Carter (http://www.savoirfairelinux.com) on 2014-03-14

Removed redundant import from openupgrade

Thanks!

review: Approve
review: Approve (code review)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'document_page_environment_manual/migrations'
2=== added directory 'document_page_environment_manual/migrations/7.0.1.0'
3=== added file 'document_page_environment_manual/migrations/7.0.1.0/pre-migration.py'
4--- document_page_environment_manual/migrations/7.0.1.0/pre-migration.py 1970-01-01 00:00:00 +0000
5+++ document_page_environment_manual/migrations/7.0.1.0/pre-migration.py 2014-03-14 15:52:13 +0000
6@@ -0,0 +1,51 @@
7+# -*- encoding: utf-8 -*-
8+##############################################################################
9+#
10+# OpenERP, Open Source Management Solution
11+# This module copyright (C) 2013 Savoir-faire Linux
12+# (<http://www.savoirfairelinux.com>).
13+#
14+# This program is free software: you can redistribute it and/or modify
15+# it under the terms of the GNU Affero General Public License as
16+# published by the Free Software Foundation, either version 3 of the
17+# License, or (at your option) any later version.
18+#
19+# This program is distributed in the hope that it will be useful,
20+# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+# GNU Affero General Public License for more details.
23+#
24+# You should have received a copy of the GNU Affero General Public License
25+# along with this program. If not, see <http://www.gnu.org/licenses/>.
26+#
27+##############################################################################
28+
29+import logging
30+logger = logging.getLogger('upgrade')
31+
32+def logged_query(cr, query, args=None):
33+ if args is None:
34+ args = []
35+ res = cr.execute(query, args)
36+ logger.debug('Running %s', query % tuple(args))
37+ logger.debug('%s rows affected', cr.rowcount)
38+ return cr.rowcount
39+
40+
41+def pre_migrate_environment_manual_category(cr, version):
42+ logged_query(cr, """\
43+UPDATE document_page
44+SET parent_id = (SELECT id FROM document_page WHERE name = 'Manuals' LIMIT 1),
45+ name = name || ' (' || %s || ')'
46+WHERE parent_id = (SELECT id FROM document_page WHERE name = 'Environment Manual' AND type = 'category')
47+ AND type = 'content';""", [version])
48+ logged_query(cr, """\
49+UPDATE document_page
50+SET name = name || ' (' || %s || ')'
51+WHERE name = 'Environment Manual' AND type = 'category';""", [version])
52+
53+
54+def migrate(cr, version):
55+ pre_migrate_environment_manual_category(cr, version)
56+
57+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
58
59=== added directory 'document_page_environmental_aspect/migrations'
60=== added directory 'document_page_environmental_aspect/migrations/7.0.1.0'
61=== added file 'document_page_environmental_aspect/migrations/7.0.1.0/post-migration.py'
62--- document_page_environmental_aspect/migrations/7.0.1.0/post-migration.py 1970-01-01 00:00:00 +0000
63+++ document_page_environmental_aspect/migrations/7.0.1.0/post-migration.py 2014-03-14 15:52:13 +0000
64@@ -0,0 +1,61 @@
65+# -*- encoding: utf-8 -*-
66+##############################################################################
67+#
68+# OpenERP, Open Source Management Solution
69+# This module copyright (C) 2013 Savoir-faire Linux
70+# (<http://www.savoirfairelinux.com>).
71+#
72+# This program is free software: you can redistribute it and/or modify
73+# it under the terms of the GNU Affero General Public License as
74+# published by the Free Software Foundation, either version 3 of the
75+# License, or (at your option) any later version.
76+#
77+# This program is distributed in the hope that it will be useful,
78+# but WITHOUT ANY WARRANTY; without even the implied warranty of
79+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80+# GNU Affero General Public License for more details.
81+#
82+# You should have received a copy of the GNU Affero General Public License
83+# along with this program. If not, see <http://www.gnu.org/licenses/>.
84+#
85+##############################################################################
86+
87+import logging
88+logger = logging.getLogger('upgrade')
89+
90+def logged_query(cr, query, args=None):
91+ if args is None:
92+ args = []
93+ res = cr.execute(query, args)
94+ logger.debug('Running %s', query % tuple(args))
95+ logger.debug('%s rows affected', cr.rowcount)
96+ return cr.rowcount
97+
98+
99+def post_migrate_environmental_aspect_category(cr, version):
100+ logged_query(cr, """\
101+UPDATE document_page
102+SET parent_id = (SELECT id FROM document_page
103+ WHERE name = 'Environmental Aspect' AND type = 'category'
104+ ORDER BY id DESC
105+ LIMIT 1),
106+ name = name || ' (' || %s || ')'
107+WHERE parent_id = (SELECT id FROM document_page
108+ WHERE name = 'Environmental Aspect' AND type = 'category'
109+ ORDER BY id ASC
110+ LIMIT 1)
111+ AND type = 'content';""", [version])
112+ logged_query(cr, """\
113+UPDATE document_page
114+SET name = name || ' (' || %s || ')'
115+WHERE id = (SELECT id FROM document_page
116+ WHERE name = 'Environmental Aspect' AND type = 'category'
117+ ORDER BY id ASC
118+ LIMIT 1)
119+ AND type = 'category';""", [version])
120+
121+
122+def migrate(cr, version):
123+ post_migrate_environmental_aspect_category(cr, version)
124+
125+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
126
127=== added directory 'document_page_procedure/migrations'
128=== added directory 'document_page_procedure/migrations/7.0.1.0'
129=== added file 'document_page_procedure/migrations/7.0.1.0/post-migration.py'
130--- document_page_procedure/migrations/7.0.1.0/post-migration.py 1970-01-01 00:00:00 +0000
131+++ document_page_procedure/migrations/7.0.1.0/post-migration.py 2014-03-14 15:52:13 +0000
132@@ -0,0 +1,62 @@
133+# -*- encoding: utf-8 -*-
134+##############################################################################
135+#
136+# OpenERP, Open Source Management Solution
137+# This module copyright (C) 2013 Savoir-faire Linux
138+# (<http://www.savoirfairelinux.com>).
139+#
140+# This program is free software: you can redistribute it and/or modify
141+# it under the terms of the GNU Affero General Public License as
142+# published by the Free Software Foundation, either version 3 of the
143+# License, or (at your option) any later version.
144+#
145+# This program is distributed in the hope that it will be useful,
146+# but WITHOUT ANY WARRANTY; without even the implied warranty of
147+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148+# GNU Affero General Public License for more details.
149+#
150+# You should have received a copy of the GNU Affero General Public License
151+# along with this program. If not, see <http://www.gnu.org/licenses/>.
152+#
153+##############################################################################
154+
155+import logging
156+logger = logging.getLogger('upgrade')
157+
158+def logged_query(cr, query, args=None):
159+ if args is None:
160+ args = []
161+ res = cr.execute(query, args)
162+ logger.debug('Running %s', query % tuple(args))
163+ logger.debug('%s rows affected', cr.rowcount)
164+ return cr.rowcount
165+
166+
167+def post_migrate_category(cr, version, category):
168+ logged_query(cr, """\
169+UPDATE document_page
170+SET parent_id = (SELECT id FROM document_page
171+ WHERE name = %s AND type = 'category'
172+ ORDER BY id DESC
173+ LIMIT 1),
174+ name = name || ' (' || %s || ')'
175+WHERE parent_id = (SELECT id FROM document_page
176+ WHERE name = %s AND type = 'category'
177+ ORDER BY id ASC
178+ LIMIT 1)
179+ AND type = 'content';""", (category, version, category))
180+ logged_query(cr, """\
181+UPDATE document_page
182+SET name = name || ' (' || %s || ')'
183+WHERE id = (SELECT id FROM document_page
184+ WHERE name = %s AND type = 'category'
185+ ORDER BY id ASC
186+ LIMIT 1)
187+ AND type = 'category';""", (version, category))
188+
189+
190+def migrate(cr, version):
191+ post_migrate_category(cr, version, 'Procedure')
192+ post_migrate_category(cr, version, 'Work Instructions')
193+
194+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
195
196=== added directory 'document_page_quality_manual/migrations'
197=== added directory 'document_page_quality_manual/migrations/7.0.1.0'
198=== added file 'document_page_quality_manual/migrations/7.0.1.0/pre-migration.py'
199--- document_page_quality_manual/migrations/7.0.1.0/pre-migration.py 1970-01-01 00:00:00 +0000
200+++ document_page_quality_manual/migrations/7.0.1.0/pre-migration.py 2014-03-14 15:52:13 +0000
201@@ -0,0 +1,51 @@
202+# -*- encoding: utf-8 -*-
203+##############################################################################
204+#
205+# OpenERP, Open Source Management Solution
206+# This module copyright (C) 2013 Savoir-faire Linux
207+# (<http://www.savoirfairelinux.com>).
208+#
209+# This program is free software: you can redistribute it and/or modify
210+# it under the terms of the GNU Affero General Public License as
211+# published by the Free Software Foundation, either version 3 of the
212+# License, or (at your option) any later version.
213+#
214+# This program is distributed in the hope that it will be useful,
215+# but WITHOUT ANY WARRANTY; without even the implied warranty of
216+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
217+# GNU Affero General Public License for more details.
218+#
219+# You should have received a copy of the GNU Affero General Public License
220+# along with this program. If not, see <http://www.gnu.org/licenses/>.
221+#
222+##############################################################################
223+
224+import logging
225+logger = logging.getLogger('upgrade')
226+
227+def logged_query(cr, query, args=None):
228+ if args is None:
229+ args = []
230+ res = cr.execute(query, args)
231+ logger.debug('Running %s', query % tuple(args))
232+ logger.debug('%s rows affected', cr.rowcount)
233+ return cr.rowcount
234+
235+
236+def pre_migrate_quality_manual_category(cr, version):
237+ logged_query(cr, """\
238+UPDATE document_page
239+SET parent_id = (SELECT id FROM document_page WHERE name = 'Manuals' LIMIT 1),
240+ name = name || ' (' || %s || ')'
241+WHERE parent_id = (SELECT id FROM document_page WHERE name = 'Quality Manual' AND type = 'category')
242+ AND type = 'content';""", [version])
243+ logged_query(cr, """\
244+UPDATE document_page
245+SET name = name || ' (' || %s || ')'
246+WHERE name = 'Quality Manual' AND type = 'category';""", [version])
247+
248+
249+def migrate(cr, version):
250+ pre_migrate_quality_manual_category(cr, version)
251+
252+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
253
254=== added directory 'document_page_work_instructions/migrations'
255=== added directory 'document_page_work_instructions/migrations/7.0.1.0'
256=== added file 'document_page_work_instructions/migrations/7.0.1.0/post-migration.py'
257--- document_page_work_instructions/migrations/7.0.1.0/post-migration.py 1970-01-01 00:00:00 +0000
258+++ document_page_work_instructions/migrations/7.0.1.0/post-migration.py 2014-03-14 15:52:13 +0000
259@@ -0,0 +1,61 @@
260+# -*- encoding: utf-8 -*-
261+##############################################################################
262+#
263+# OpenERP, Open Source Management Solution
264+# This module copyright (C) 2013 Savoir-faire Linux
265+# (<http://www.savoirfairelinux.com>).
266+#
267+# This program is free software: you can redistribute it and/or modify
268+# it under the terms of the GNU Affero General Public License as
269+# published by the Free Software Foundation, either version 3 of the
270+# License, or (at your option) any later version.
271+#
272+# This program is distributed in the hope that it will be useful,
273+# but WITHOUT ANY WARRANTY; without even the implied warranty of
274+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
275+# GNU Affero General Public License for more details.
276+#
277+# You should have received a copy of the GNU Affero General Public License
278+# along with this program. If not, see <http://www.gnu.org/licenses/>.
279+#
280+##############################################################################
281+
282+import logging
283+logger = logging.getLogger('upgrade')
284+
285+def logged_query(cr, query, args=None):
286+ if args is None:
287+ args = []
288+ res = cr.execute(query, args)
289+ logger.debug('Running %s', query % tuple(args))
290+ logger.debug('%s rows affected', cr.rowcount)
291+ return cr.rowcount
292+
293+
294+def post_migrate_category(cr, version, category):
295+ logged_query(cr, """\
296+UPDATE document_page
297+SET parent_id = (SELECT id FROM document_page
298+ WHERE name = %s AND type = 'category'
299+ ORDER BY id DESC
300+ LIMIT 1),
301+ name = name || ' (' || %s || ')'
302+WHERE parent_id = (SELECT id FROM document_page
303+ WHERE name = %s AND type = 'category'
304+ ORDER BY id ASC
305+ LIMIT 1)
306+ AND type = 'content';""", (category, version, category))
307+ logged_query(cr, """\
308+UPDATE document_page
309+SET name = name || ' (' || %s || ')'
310+WHERE id = (SELECT id FROM document_page
311+ WHERE name = %s AND type = 'category'
312+ ORDER BY id ASC
313+ LIMIT 1)
314+ AND type = 'category';""", (version, category))
315+
316+
317+def migrate(cr, version):
318+ post_migrate_category(cr, version, 'Work Instructions')
319+
320+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
321
322=== added directory 'mgmtsystem_action/migrations/7.0.1.0'
323=== added file 'mgmtsystem_action/migrations/7.0.1.0/post-migration.py'
324--- mgmtsystem_action/migrations/7.0.1.0/post-migration.py 1970-01-01 00:00:00 +0000
325+++ mgmtsystem_action/migrations/7.0.1.0/post-migration.py 2014-03-14 15:52:13 +0000
326@@ -0,0 +1,82 @@
327+# -*- encoding: utf-8 -*-
328+##############################################################################
329+#
330+# OpenERP, Open Source Management Solution
331+# This module copyright (C) 2013 Savoir-faire Linux
332+# (<http://www.savoirfairelinux.com>).
333+#
334+# This program is free software: you can redistribute it and/or modify
335+# it under the terms of the GNU Affero General Public License as
336+# published by the Free Software Foundation, either version 3 of the
337+# License, or (at your option) any later version.
338+#
339+# This program is distributed in the hope that it will be useful,
340+# but WITHOUT ANY WARRANTY; without even the implied warranty of
341+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
342+# GNU Affero General Public License for more details.
343+#
344+# You should have received a copy of the GNU Affero General Public License
345+# along with this program. If not, see <http://www.gnu.org/licenses/>.
346+#
347+##############################################################################
348+
349+from openerp import release
350+import logging
351+logger = logging.getLogger('upgrade')
352+
353+def get_legacy_name(original_name):
354+ return 'legacy_' + ('_').join(
355+ map(str, release.version_info[0:2])) + '_' + original_name
356+
357+def logged_query(cr, query, args=None):
358+ if args is None:
359+ args = []
360+ cr.execute(query, args)
361+ logger.debug('Running %s', query % tuple(args))
362+ logger.debug('%s rows affected', cr.rowcount)
363+ return cr.rowcount
364+
365+
366+def migrate_stage_id(cr):
367+ stage_states = [
368+ ('draft', 'New'),
369+ ("open", "Accepted as Claim"),
370+ ("open", "Actions Defined"),
371+ ("open", "Qualification"),
372+ ("open", "Proposition"),
373+ ("open", "Negotiation"),
374+ ("done", "Actions Done"),
375+ ("done", "Won"),
376+ ("done", "Dead"),
377+ ("cancel", "Won't fix"),
378+ ("cancel", "Lost"),
379+ ]
380+ logged_query(cr, """
381+ UPDATE mgmtsystem_action
382+ SET stage_id = NULL""")
383+ legacy_stage_name = get_legacy_name("stage_id")
384+ # Check if the column exists
385+ logged_query(cr, """
386+ SELECT column_name
387+ FROM information_schema.columns
388+ WHERE table_name = 'mgmtsystem_action'
389+ AND column_name = %s""", legacy_stage_name)
390+ if not cr.fetchall():
391+ return
392+ query = """
393+ UPDATE mgmtsystem_action
394+ SET stage_id = (SELECT id
395+ FROM crm_claim_stage
396+ WHERE state = %%s
397+ LIMIT 1)
398+ WHERE %s IN (SELECT id
399+ FROM crm_case_stage
400+ WHERE name = %%s)""" % legacy_stage_name
401+ for i in stage_states:
402+ logged_query(cr, query, i)
403+
404+
405+def migrate(cr, version):
406+ migrate_stage_id(cr)
407+
408+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
409
410=== added file 'mgmtsystem_action/migrations/7.0.1.0/pre-migration.py'
411--- mgmtsystem_action/migrations/7.0.1.0/pre-migration.py 1970-01-01 00:00:00 +0000
412+++ mgmtsystem_action/migrations/7.0.1.0/pre-migration.py 2014-03-14 15:52:13 +0000
413@@ -0,0 +1,52 @@
414+# -*- encoding: utf-8 -*-
415+##############################################################################
416+#
417+# OpenERP, Open Source Management Solution
418+# This module copyright (C) 2013 Savoir-faire Linux
419+# (<http://www.savoirfairelinux.com>).
420+#
421+# This program is free software: you can redistribute it and/or modify
422+# it under the terms of the GNU Affero General Public License as
423+# published by the Free Software Foundation, either version 3 of the
424+# License, or (at your option) any later version.
425+#
426+# This program is distributed in the hope that it will be useful,
427+# but WITHOUT ANY WARRANTY; without even the implied warranty of
428+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
429+# GNU Affero General Public License for more details.
430+#
431+# You should have received a copy of the GNU Affero General Public License
432+# along with this program. If not, see <http://www.gnu.org/licenses/>.
433+#
434+##############################################################################
435+
436+from openerp import release
437+import logging
438+logger = logging.getLogger('upgrade')
439+
440+def get_legacy_name(original_name):
441+ return 'legacy_' + ('_').join(
442+ map(str, release.version_info[0:2])) + '_' + original_name
443+
444+def rename_columns(cr, column_spec):
445+ for table in column_spec.keys():
446+ for (old, new) in column_spec[table]:
447+ if new is None:
448+ new = get_legacy_name(old)
449+ logger.info("table %s, column %s: renaming to %s",
450+ table, old, new)
451+ cr.execute('ALTER TABLE "%s" RENAME "%s" TO "%s"' % (table, old, new,))
452+ cr.execute('DROP INDEX IF EXISTS "%s_%s_index"' % (table, old))
453+
454+
455+column_renames = {
456+ 'mgmtsystem_action': [
457+ ('stage_id', None)
458+ ]
459+}
460+
461+
462+def migrate(cr, version):
463+ rename_columns(cr, column_renames)
464+
465+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
466
467=== modified file 'mgmtsystem_nonconformity/migrations/7.0.1.0/post-migration.py'
468--- mgmtsystem_nonconformity/migrations/7.0.1.0/post-migration.py 2013-09-17 22:08:14 +0000
469+++ mgmtsystem_nonconformity/migrations/7.0.1.0/post-migration.py 2014-03-14 15:52:13 +0000
470@@ -20,32 +20,66 @@
471 ##############################################################################
472
473 import logging
474-
475 logger = logging.getLogger('upgrade')
476
477+def logged_query(cr, query, args=None):
478+ if args is None:
479+ args = []
480+ res = cr.execute(query, args)
481+ logger.debug('Running %s', query % tuple(args))
482+ logger.debug('%s rows affected', cr.rowcount)
483+ return cr.rowcount
484+
485+
486+def migrate_nonconformity_action_ids(cr, column_names):
487+ logged_query(cr, """
488+ SELECT COUNT(*)
489+ FROM mgmtsystem_nonconformity_action_rel""")
490+ if cr.fetchone()[0] > 0:
491+ logger.warning(
492+ "Attempt to migrate nonconformity action IDs failed: migration was already done.")
493+ return
494+ logger.info(
495+ "Moving nonconformity/action relations to mgmtsystem_nonconformity_action_rel")
496+ logged_query(cr, """
497+ SELECT column_name
498+ FROM information_schema.columns
499+ WHERE table_name = 'mgmtsystem_nonconformity'""")
500+ action_fields = ['preventive_action_id', 'immediate_action_id', 'corrective_action_id']
501+ available_fields = [i for i in action_fields if i in column_names]
502+ for action_field in available_fields:
503+ logged_query(cr, """
504+ INSERT INTO mgmtsystem_nonconformity_action_rel (nonconformity_id, action_id)
505+ (SELECT id, %s action_id FROM mgmtsystem_nonconformity
506+ WHERE %s IS NOT NULL);""" % (action_field, action_field))
507+
508+
509+def concatenate_action_comments(cr, column_names):
510+ logger.info("Concatenating action comments into evaluation_comments")
511+ action_fields = ['effectiveness_preventive', 'effectiveness_immediate', 'effectiveness_corrective']
512+ concatenation = " || ' ' || ".join([i for i in action_fields if i in column_names])
513+ if concatenation:
514+ logged_query(cr, """
515+ UPDATE mgmtsystem_nonconformity
516+ SET evaluation_comments = %s
517+ WHERE evaluation_comments IS NULL;""" % concatenation)
518+
519+
520+def update_state_flags(cr):
521+ logger.info("Updating state flags")
522+ for i in [('open', 'o'), ('done', 'c')]:
523+ logged_query(cr, """
524+ UPDATE mgmtsystem_nonconformity
525+ SET state = %s
526+ WHERE state = %s;""", i)
527+
528
529 def migrate(cr, version):
530- logger.info("Migrating mgmtsystem_nonconformity from version %s", version)
531- cr.execute("select count(*) from mgmtsystem_nonconformity_action_rel")
532- rowcount = cr.fetchone()[0]
533- if rowcount == 0:
534- logger.info("Moving nonconformity/action relations to mgmtsystem_nonconformity_action_rel")
535- for action_field in ('preventive_action_id', 'immediate_action_id', 'corrective_action_id'):
536- cr.execute("insert into mgmtsystem_nonconformity_action_rel"
537- "(nonconformity_id, action_id) "
538- "(SELECT id, %s FROM "
539- "mgmtsystem_nonconformity "
540- "WHERE %s IS NOT NULL )" % (action_field, action_field))
541- else:
542- logger.warning("Attempt to migrate nonconformity action IDs failed: migration was already done.")
543-
544- logger.info("Concatening action comments into evaluation_comments")
545- cr.execute("update mgmtsystem_nonconformity set evaluation_comments = "
546- "effectiveness_preventive || ' ' || effectiveness_immediate || ' ' || effectiveness_corrective "
547- "where evaluation_comments is null")
548-
549- logger.info("Updating state flags")
550- cr.execute("update mgmtsystem_nonconformity set state = 'open' where state = 'o'")
551- cr.execute("update mgmtsystem_nonconformity set state = 'done' where state = 'c'")
552-
553- logger.info("mgmtsystem_nonconformity update... done!")
554+ logged_query(cr, """
555+ SELECT column_name
556+ FROM information_schema.columns
557+ WHERE table_name = 'mgmtsystem_nonconformity'""")
558+ column_names = (i[0] for i in cr.fetchall())
559+ migrate_nonconformity_action_ids(cr, column_names)
560+ concatenate_action_comments(cr, column_names)
561+ update_state_flags(cr)