-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIG] maintenance_equipment_sequence: Migration to 15.0
- Loading branch information
1 parent
53d29db
commit aacc1d3
Showing
6 changed files
with
119 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
maintenance_equipment_sequence/migrations/15.0.1.0.0/pre-migration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Copyright 2021 ForgeFlow S.L. (https://forgeflow.com) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
|
||
def migrate(cr, version): | ||
""" | ||
Fields 'code' and 'serial_no' of model 'maintenance.equipment' fulfill the | ||
same function, which is to have an internal identifier of an equipment. | ||
Therefore, as a solution we will use only one column. | ||
Field 'serial_no' is the one in odoo core and the one supposedly used for | ||
identifying our equipment. | ||
Based on this assumption, this migration will save on 'serial_no' column | ||
all the original values plus all the 'code' values that are null in | ||
'serial_no' original column. And after this, it will delete 'code' column. | ||
POSSIBLE ISSUE: | ||
Considering that this migration may not work correctly to everyone, | ||
because it's giving priority to 'serial_no' before 'code', we will create | ||
two new columns 'old_code' and 'old_serial_no' with the values of the | ||
original columns. | ||
ISSUE: If you identify your equipments with the 'code' field and you prefer to | ||
save it's values rather than 'serial_no' values. We provide you the following | ||
query: | ||
SQL STATEMENT: | ||
UPDATE maintenance_equipment | ||
SET serial_no = old_code | ||
WHERE old_code IS NOT NULL; | ||
""" | ||
|
||
if not version: | ||
return | ||
|
||
cr.execute( | ||
""" | ||
SELECT count(attname) FROM pg_attribute | ||
WHERE attrelid = (SELECT oid FROM pg_class | ||
WHERE relname = 'maintenance_equipment') | ||
AND attname = 'code'""" | ||
) | ||
|
||
if not cr.fetchone()[0] == 1: | ||
return | ||
|
||
cr.execute( | ||
""" | ||
ALTER TABLE maintenance_equipment ADD COLUMN "old_serial_no" varchar; | ||
UPDATE maintenance_equipment SET old_serial_no = serial_no;""" | ||
) | ||
|
||
cr.execute( | ||
""" | ||
ALTER TABLE maintenance_equipment ADD COLUMN "old_code" varchar; | ||
UPDATE maintenance_equipment SET old_code = code""" | ||
) | ||
|
||
cr.execute( | ||
""" | ||
UPDATE maintenance_equipment | ||
SET code = serial_no | ||
WHERE serial_no IS NOT NULL""" | ||
) | ||
|
||
cr.execute( | ||
""" | ||
WITH cte AS | ||
( | ||
SELECT ROW_NUMBER() OVER(PARTITION BY code ORDER BY code,id ) AS rno, code, id | ||
FROM maintenance_equipment | ||
) | ||
UPDATE maintenance_equipment me | ||
SET code = me.code || '_' || cte.rno | ||
FROM cte | ||
WHERE cte.rno>1 AND cte.id = me.id""" | ||
) | ||
|
||
cr.execute( | ||
""" | ||
UPDATE maintenance_equipment | ||
SET serial_no = code | ||
WHERE code IS NOT NULL;""" | ||
) | ||
|
||
cr.execute( | ||
""" | ||
ALTER TABLE maintenance_equipment | ||
DROP COLUMN code;""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
* Adria Gil Sorribes <adria.gil@forgeflow.com> | ||
* Núria Martín Xifré <nuria.martin@forgeflow.com> | ||
* Bernat Puig Font <bernat.puig@forgeflow.com> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters