From f8bc09eab0ccd41ee7a2dd734440f6b83ae8c90d Mon Sep 17 00:00:00 2001 From: sergiocorato Date: Tue, 3 Oct 2023 19:05:05 +0200 Subject: [PATCH] [FIX] move invoice_id script to pre-migration --- .../migrations/13.0.1.0.0/post-migration.py | 27 ----------- .../migrations/13.0.1.0.0/pre-migration.py | 48 +++++++++++++++++++ 2 files changed, 48 insertions(+), 27 deletions(-) create mode 100644 l10n_it_intrastat_statement/migrations/13.0.1.0.0/pre-migration.py diff --git a/l10n_it_intrastat_statement/migrations/13.0.1.0.0/post-migration.py b/l10n_it_intrastat_statement/migrations/13.0.1.0.0/post-migration.py index a77f64ca6e72..f6a659acdcaa 100644 --- a/l10n_it_intrastat_statement/migrations/13.0.1.0.0/post-migration.py +++ b/l10n_it_intrastat_statement/migrations/13.0.1.0.0/post-migration.py @@ -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), - ), - ) diff --git a/l10n_it_intrastat_statement/migrations/13.0.1.0.0/pre-migration.py b/l10n_it_intrastat_statement/migrations/13.0.1.0.0/pre-migration.py new file mode 100644 index 000000000000..6e759ea0098c --- /dev/null +++ b/l10n_it_intrastat_statement/migrations/13.0.1.0.0/pre-migration.py @@ -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), + ), + )