Skip to content

Commit

Permalink
Merge PR OCA#3635 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by sergiocorato
  • Loading branch information
OCA-git-bot committed Oct 30, 2023
2 parents b50925b + f8bc09e commit 3c3ce34
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
from openupgradelib import openupgrade
from psycopg2 import sql


def migrate(cr, installed_version):
openupgrade.load_data(
cr, "l10n_it_intrastat_statement", "migrations/13.0.1.0.0/noupdate_changes.xml"
)

to_be_updated = (
("account_intrastat_statement_sale_section1", "invoice_id"),
("account_intrastat_statement_sale_section2", "invoice_id"),
("account_intrastat_statement_sale_section3", "invoice_id"),
("account_intrastat_statement_sale_section4", "invoice_id"),
("account_intrastat_statement_purchase_section1", "invoice_id"),
("account_intrastat_statement_purchase_section2", "invoice_id"),
("account_intrastat_statement_purchase_section3", "invoice_id"),
("account_intrastat_statement_purchase_section4", "invoice_id"),
)

if openupgrade.table_exists(cr, "account_invoice"):
for table, column in to_be_updated:
openupgrade.logged_query(
cr,
sql.SQL(
"""UPDATE {table} t
SET {column} = ai.move_id
FROM account_invoice ai
WHERE t.{column} = ai.id and ai.move_id is NOT NULL"""
).format(
table=sql.Identifier(table),
column=sql.Identifier(column),
),
)
48 changes: 48 additions & 0 deletions l10n_it_intrastat_statement/migrations/13.0.1.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from openupgradelib import openupgrade
from psycopg2 import sql


def migrate(cr, installed_version):
drop_sql = sql.SQL("ALTER TABLE {} DROP CONSTRAINT {}")
to_be_updated = (
("account_intrastat_statement_sale_section1", "invoice_id"),
("account_intrastat_statement_sale_section2", "invoice_id"),
("account_intrastat_statement_sale_section3", "invoice_id"),
("account_intrastat_statement_sale_section4", "invoice_id"),
("account_intrastat_statement_purchase_section1", "invoice_id"),
("account_intrastat_statement_purchase_section2", "invoice_id"),
("account_intrastat_statement_purchase_section3", "invoice_id"),
("account_intrastat_statement_purchase_section4", "invoice_id"),
)

if openupgrade.table_exists(cr, "account_invoice"):
for table, column in to_be_updated:
cr.execute(
"""
SELECT constraint_name
FROM information_schema.table_constraints
WHERE constraint_type = 'FOREIGN KEY' AND table_name = %s
AND constraint_name like %s
""",
(table, "%%%s%%" % "invoice_id"),
)
for constraint in (row[0] for row in cr.fetchall()):
openupgrade.logged_query(
cr,
drop_sql.format(
sql.Identifier(table),
sql.Identifier(constraint),
),
)
openupgrade.logged_query(
cr,
sql.SQL(
"""UPDATE {table} t
SET {column} = ai.move_id
FROM account_invoice ai
WHERE t.{column} = ai.id and ai.move_id is NOT NULL"""
).format(
table=sql.Identifier(table),
column=sql.Identifier(column),
),
)

0 comments on commit 3c3ce34

Please sign in to comment.