From b8a9a33e6f36ba40b4b7f15cc57c1cd166d0ea39 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Mon, 27 May 2024 14:37:01 +0200 Subject: [PATCH] [OU-ADD] intrastat_base: Migration scripts Handle the change from boolean to selection on `instrastat` field in account.fiscal.position. TT49367 --- .../migrations/16.0.1.0.0/post-migration.py | 28 +++++++++++++++++++ .../migrations/16.0.1.0.0/pre-migration.py | 10 +++++++ 2 files changed, 38 insertions(+) create mode 100644 intrastat_base/migrations/16.0.1.0.0/post-migration.py create mode 100644 intrastat_base/migrations/16.0.1.0.0/pre-migration.py diff --git a/intrastat_base/migrations/16.0.1.0.0/post-migration.py b/intrastat_base/migrations/16.0.1.0.0/post-migration.py new file mode 100644 index 00000000..ef44d455 --- /dev/null +++ b/intrastat_base/migrations/16.0.1.0.0/post-migration.py @@ -0,0 +1,28 @@ +# Copyright 2024 Tecnativa - Pedro M. Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openupgradelib import openupgrade + + +@openupgrade.migrate() +def migrate(env, version): + # Set proper values for new selection field + old_column = openupgrade.get_legacy_name("intrastat") + openupgrade.logged_query( + env.cr, + f""" + UPDATE account_fiscal_position + SET intrastat = CASE + WHEN NOT {old_column} THEN 'no' + ELSE 'b2b' END + """, + ) + # Propagate proper values to the related field + openupgrade.logged_query( + env.cr, + """ + UPDATE account_move am + SET intrastat_fiscal_position = afp.intrastat + FROM account_fiscal_position afp + WHERE afp.id = am.fiscal_position_id + """, + ) diff --git a/intrastat_base/migrations/16.0.1.0.0/pre-migration.py b/intrastat_base/migrations/16.0.1.0.0/pre-migration.py new file mode 100644 index 00000000..03bc4a84 --- /dev/null +++ b/intrastat_base/migrations/16.0.1.0.0/pre-migration.py @@ -0,0 +1,10 @@ +# Copyright 2024 Tecnativa - Pedro M. Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openupgradelib import openupgrade + + +@openupgrade.migrate() +def migrate(env, version): + openupgrade.rename_columns( + env.cr, {"account_fiscal_position": [("intrastat", None)]} + )