From 66138bdafae488727e51b7ffb4ed47c1c02e4bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Ra=C3=AFch?= Date: Tue, 16 Nov 2021 12:32:49 +0100 Subject: [PATCH] [FIX] contract: assure contracts exist if invoices point to them --- contract/migrations/12.0.4.0.0/pre-migration.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/contract/migrations/12.0.4.0.0/pre-migration.py b/contract/migrations/12.0.4.0.0/pre-migration.py index a99aa2abd..fd00f99d2 100644 --- a/contract/migrations/12.0.4.0.0/pre-migration.py +++ b/contract/migrations/12.0.4.0.0/pre-migration.py @@ -97,17 +97,24 @@ def create_contract_records(cr): INSERT INTO contract_contract SELECT * FROM account_analytic_account WHERE id IN (SELECT DISTINCT {} FROM contract_line) + OR id IN (SELECT DISTINCT contract_id FROM account_invoice) """).format( sql.Identifier(contract_field_name), ), ) # Deactivate disabled contracts openupgrade.logged_query( - cr, """UPDATE contract_contract cc + cr, sql.SQL(""" + UPDATE contract_contract cc SET active = False FROM account_analytic_account aaa WHERE aaa.id = cc.id - AND NOT aaa.recurring_invoices""", + AND (NOT aaa.recurring_invoices + OR aaa.id NOT IN ( + SELECT DISTINCT {} FROM contract_line) + )""").format( + sql.Identifier(contract_field_name), + ), ) # Handle id sequence cr.execute("CREATE SEQUENCE IF NOT EXISTS contract_contract_id_seq")