Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] account_credit_control: migration scripts #1

Open
wants to merge 17 commits into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions account_credit_control/migrations/11.0.1.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
if openupgrade.table_exists(env.cr, "credit_run_policy_rel"):
# move data from credit_control_policy_credit_control_run_rel to
# credit_run_policy_rel
env.cr.execute(
"""INSERT INTO credit_run_policy_rel
(run_id, policy_id)
SELECT credit_control_run_id, credit_control_policy_id
FROM credit_control_policy_credit_control_run_rel"""
)
else:
openupgrade.rename_tables(
env.cr,
[
(
"credit_control_policy_credit_control_run_rel",
"credit_run_policy_rel",
),
],
)
openupgrade.rename_columns(
env.cr,
{
"credit_run_policy_rel": [
("credit_control_run_id", "run_id"),
("credit_control_policy_id", "policy_id"),
]
},
)
33 changes: 33 additions & 0 deletions account_credit_control/migrations/13.0.1.0.0/noupdate_changes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version='1.0' encoding='utf-8' ?>
<odoo>
<record id="credit_control_run_rule" model="ir.rule">
<field
name="domain_force"
>['|',('company_id','=',False),('company_id','in',company_ids)]</field>
</record>
<record id="credit_control_communication_rule" model="ir.rule">
<field
name="domain_force"
>['|',('company_id','=',False),('company_id','in',company_ids)]</field>
</record>
<record id="credit_control_line_rule" model="ir.rule">
<field
name="domain_force"
>['|',('company_id','=',False),('company_id','in',company_ids)]</field>
</record>
<record id="credit_control_policy_rule" model="ir.rule">
<field
name="domain_force"
>['|',('company_id','=',False),('company_id','in',company_ids)]</field>
</record>
<record id="credit_control_analysis_rule" model="ir.rule">
<field
name="domain_force"
>['|',('company_id','=',False),('company_id','in',company_ids)]</field>
</record>
<record id="credit_control_partner_payment_action_type_rule" model="ir.rule">
<field
name="domain_force"
>['|',('company_id','=',False),('company_id','in',company_ids)]</field>
</record>
</odoo>
27 changes: 27 additions & 0 deletions account_credit_control/migrations/13.0.1.0.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openupgradelib import openupgrade # pylint: disable=W7936


@openupgrade.migrate()
def migrate(env, version):
openupgrade.load_data(
env.cr, "account_credit_control", "migrations/13.0.1.0.0/noupdate_changes.xml"
)
if openupgrade.column_exists(env.cr, "account_move", "old_invoice_id"):
openupgrade.logged_query(
env.cr,
"""UPDATE account_move am
SET credit_policy_id = ai.credit_policy_id
FROM account_invoice ai
WHERE ai.id = am.old_invoice_id""",
)
else:
openupgrade.logged_query(
env.cr,
"""UPDATE account_move am
SET credit_policy_id = ai.credit_policy_id
FROM account_invoice ai
WHERE ai.move_id = am.id""",
)
22 changes: 22 additions & 0 deletions account_credit_control/migrations/13.0.1.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openupgradelib import openupgrade # pylint: disable=W7936
from psycopg2 import sql


@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_columns(env.cr, {"credit_control_line": [("invoice_id", None)]})
openupgrade.logged_query(
env.cr, "ALTER TABLE credit_control_line ADD invoice_id INT4"
)
openupgrade.logged_query(
env.cr,
sql.SQL(
"""UPDATE credit_control_line ccl
SET invoice_id = ai.move_id
FROM account_invoice ai
WHERE ai.id = ccl.{}"""
).format(sql.Identifier(openupgrade.get_legacy_name("invoice_id"))),
)
47 changes: 47 additions & 0 deletions account_credit_control/migrations/13.0.2.0.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2020 Tecnativa - Jairo Llopis
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
# Update mail.template records for credit.control.communication
communication_model = env.ref(
"account_credit_control.model_credit_control_communication"
)
replacements = (
("object.contact_address", "object.contact_address_id"),
("object.current_policy_level", "object.policy_level_id"),
("object.get_contact_address()", "object.contact_address_id"),
)
for from_, to in replacements:
openupgrade.logged_query(
env.cr,
"""
UPDATE mail_template SET
subject = REPLACE(subject, %(from)s, %(to)s),
body_html = REPLACE(body_html, %(from)s, %(to)s)
WHERE model_id = %(model_id)s
""",
{"from": from_, "to": to, "model_id": communication_model.id},
)
openupgrade.logged_query(
env.cr,
"""
UPDATE ir_translation SET
value = REPLACE(value, %(from)s, %(to)s)
WHERE module = 'account_credit_control'
AND name ilike 'mail.template%%'
""",
{"from": from_, "to": to},
)
# Update the recipients config just for this template, others should be
# manually changed if desired althoug they'll keep working as there's
# backward compatibility with the old method
communication_template = env.ref(
"account_credit_control.email_template_credit_control_base"
)
communication_template.email_to = False
communication_template.partner_to = "${object.get_emailing_contact().id or ''}"
20 changes: 20 additions & 0 deletions account_credit_control/migrations/13.0.2.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2020 Tecnativa - Jairo Llopis
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
# Preserve mail_message_id historic data from credit.control.line records
mail_message_legacy = openupgrade.get_legacy_name("mail_message_id")
openupgrade.copy_columns(
env.cr,
{"credit_control_line": [("mail_message_id", mail_message_legacy, None)]},
)
# Vacuum meaningless transient data from credit.control.communication
openupgrade.logged_query(
env.cr,
"DELETE FROM credit_control_communication",
)