@ -1,19 +1,15 @@
# Copyright 2019 ACSONE SA/NV
# Copyright 2019 ACSONE SA/NV
# Copyright 2019 Tecnativa 2019 - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
import logging
from openupgradelib import openupgrade
from openupgradelib import openupgrade
from psycopg2 import sql
_logger = logging . getLogger ( __name__ )
_logger = logging . getLogger ( __name__ )
@openupgrade.migrate ( )
def migrate ( env , version ) :
_logger . info ( " >> Pre-Migration 12.0.4.0.0 " )
cr = env . cr
models_to_rename = [
models_to_rename = [
# Contract Line Wizard
# Contract Line Wizard
( ' account.analytic.invoice.line.wizard ' , ' contract.line.wizard ' ) ,
( ' account.analytic.invoice.line.wizard ' , ' contract.line.wizard ' ) ,
# Abstract Contract
# Abstract Contract
@ -29,14 +25,21 @@ def migrate(env, version):
( ' account.analytic.contract ' , ' contract.template ' ) ,
( ' account.analytic.contract ' , ' contract.template ' ) ,
# Contract Template Line
# Contract Template Line
( ' account.analytic.contract.line ' , ' contract.template.line ' ) ,
( ' account.analytic.contract.line ' , ' contract.template.line ' ) ,
]
tables_to_rename = [
]
tables_to_rename = [
# Contract Line
( ' account_analytic_invoice_line ' , ' contract_line ' ) ,
# Contract Template
# Contract Template
( ' account_analytic_contract ' , ' contract_template ' ) ,
( ' account_analytic_contract ' , ' contract_template ' ) ,
# Contract Template Line
# Contract Template Line
( ' account_analytic_contract_line ' , ' contract_template_line ' ) ,
( ' account_analytic_contract_line ' , ' contract_template_line ' ) ,
]
xmlids_to_rename = [
]
columns_to_copy = {
' contract_line ' : [
( ' analytic_account_id ' , ' contract_id ' , None ) ,
] ,
}
xmlids_to_rename = [
(
(
' contract.account_analytic_cron_for_invoice ' ,
' contract.account_analytic_cron_for_invoice ' ,
' contract.contract_cron_for_invoice ' ,
' contract.contract_cron_for_invoice ' ,
@ -65,49 +68,46 @@ def migrate(env, version):
' contract.account_analytic_contract_line_user ' ,
' contract.account_analytic_contract_line_user ' ,
' contract.contract_template_line_user ' ,
' contract.contract_template_line_user ' ,
) ,
) ,
]
openupgrade . rename_models ( cr , models_to_rename )
openupgrade . rename_tables ( cr , tables_to_rename )
openupgrade . rename_xmlids ( cr , xmlids_to_rename )
# A temporary column is needed to avoid breaking the foreign key constraint
# The temporary column is dropped in the post-migration script
openupgrade . logged_query (
cr ,
]
def _get_contract_field_name ( cr ) :
"""
"""
ALTER TABLE account_invoice_line
ADD COLUMN contract_line_id_tmp INTEGER
""" ,
)
Contract field changed the name from analytic_account_id to contract_id
in 12.0 . 2.0 . 0. This method used to get the contract field name in
account_analytic_invoice_line """
return (
' contract_id '
if openupgrade . column_exists (
if openupgrade . column_exists (
cr , ' account_invoice_line ' , ' contract_line_id '
) :
openupgrade . logged_query (
cr ,
"""
UPDATE account_invoice_line
SET contract_line_id_tmp = contract_line_id
""" ,
cr , ' account_analytic_invoice_line ' , ' contract_id '
)
)
openupgrade . logged_query (
cr ,
"""
UPDATE account_invoice_line SET contract_line_id = NULL
""" ,
else ' analytic_account_id '
)
)
if not openupgrade . column_exists (
cr , ' account_invoice ' , ' old_contract_id '
) :
def create_contract_records ( cr ) :
contract_field_name = _get_contract_field_name ( cr )
openupgrade . logged_query (
openupgrade . logged_query (
cr ,
"""
ALTER TABLE account_invoice
ADD COLUMN old_contract_id_tmp INTEGER
""" ,
cr , """
CREATE TABLE contract_contract
( LIKE account_analytic_account INCLUDING ALL ) """ ,
)
)
openupgrade . logged_query (
openupgrade . logged_query (
cr ,
"""
UPDATE account_invoice
SET old_contract_id_tmp = contract_id
""" ,
cr , sql . SQL ( """
INSERT INTO contract_contract
SELECT * FROM account_analytic_account
WHERE id IN ( SELECT DISTINCT { } FROM contract_line )
""" ).format(
sql . Identifier ( contract_field_name ) ,
) ,
)
)
@openupgrade.migrate ( )
def migrate ( env , version ) :
cr = env . cr
openupgrade . rename_models ( cr , models_to_rename )
openupgrade . rename_tables ( cr , tables_to_rename )
openupgrade . rename_xmlids ( cr , xmlids_to_rename )
openupgrade . copy_columns ( cr , columns_to_copy )
create_contract_records ( cr )