From e15f365e05101acc137946b3c860d7891edc7d8a Mon Sep 17 00:00:00 2001 From: tafaRU Date: Tue, 20 Oct 2020 10:57:10 +0200 Subject: [PATCH] l10n_it_fatturapa_in: avoid duplication in the code of post migration script (DRY principle) --- .../migrations/12.0.2.0.0/post-migration.py | 99 ++++++------------- 1 file changed, 31 insertions(+), 68 deletions(-) diff --git a/l10n_it_fatturapa_in/migrations/12.0.2.0.0/post-migration.py b/l10n_it_fatturapa_in/migrations/12.0.2.0.0/post-migration.py index eef1cd161fdd..864774d86616 100644 --- a/l10n_it_fatturapa_in/migrations/12.0.2.0.0/post-migration.py +++ b/l10n_it_fatturapa_in/migrations/12.0.2.0.0/post-migration.py @@ -1,78 +1,41 @@ # Copyright 2020 Sergio Corato +# Copyright 2020 Alex Comba - Agile Business Group # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openupgradelib import openupgrade from psycopg2 import sql def create_withholding_data_lines(env): - # create ftpa_withholding_ids from ftpa_withholding_type and ftpa_withholding_amount - column_name = openupgrade.get_legacy_name('ftpa_withholding_amount') - if openupgrade.column_exists(env.cr, 'account_invoice', column_name): - openupgrade.logged_query( - env.cr, sql.SQL( - """ - INSERT INTO withholding_data_line - ( - name, - amount, - invoice_id, - create_uid, - create_date, - write_date, - write_uid - ) - SELECT - ai.{ftpa_withholding_type}, - ai.{ftpa_withholding_amount}, - ai.id, - ai.create_uid, - ai.create_date, - ai.write_date, - ai.write_uid - FROM account_invoice ai - WHERE ai.{ftpa_withholding_type} IS NOT NULL; - """ - ).format( - ftpa_withholding_type=sql.Identifier( - openupgrade.get_legacy_name( - 'ftpa_withholding_type') - ), - ftpa_withholding_amount=sql.Identifier( - openupgrade.get_legacy_name( - 'ftpa_withholding_amount') - ), - ) - ) - else: - openupgrade.logged_query( - env.cr, sql.SQL( - """ - INSERT INTO withholding_data_line - ( - name, - invoice_id, - create_uid, - create_date, - write_date, - write_uid - ) - SELECT - ai.{ftpa_withholding_type}, - ai.id, - ai.create_uid, - ai.create_date, - ai.write_date, - ai.write_uid - FROM account_invoice ai - WHERE ai.{ftpa_withholding_type} IS NOT NULL; - """ - ).format( - ftpa_withholding_type=sql.Identifier( - openupgrade.get_legacy_name( - 'ftpa_withholding_type') - ), - ) - ) + """ + Create ftpa_withholding_ids from ftpa_withholding_type + and ftpa_withholding_amount + """ + column_wht_amount = openupgrade.get_legacy_name('ftpa_withholding_amount') + column_wht_type = openupgrade.get_legacy_name('ftpa_withholding_type') + exists = openupgrade.column_exists(env.cr, 'account_invoice', column_wht_amount) + mapping = { + 'name': 'ai.{ftpa_withholding_type}'.format( + ftpa_withholding_type=column_wht_type), + 'invoice_id': 'ai.id', + 'create_uid': 'ai.create_uid', + 'create_date': 'ai.create_date', + 'write_date': 'ai.write_date', + 'write_uid': 'ai.write_uid', + } + if exists: + mapping.update( + {'amount': 'ai.{ftpa_withholding_amount}'.format( + ftpa_withholding_amount=column_wht_amount)}) + query = """ + INSERT INTO withholding_data_line + ({columns}) + SELECT {values} + FROM account_invoice AS ai + WHERE ai.{ftpa_withholding_type} IS NOT NULL;""".format( + columns=','.join(mapping.keys()), + values=','.join(mapping.values()), + ftpa_withholding_type=column_wht_type) + openupgrade.logged_query(env.cr, sql.SQL(query)) @openupgrade.migrate()