Skip to content

Commit

Permalink
[OU-ADD] base: Continue migration scripts
Browse files Browse the repository at this point in the history
- Comment some non desirable noupdate=1 changes
- Handle properly ir.actions.server update conversion
- Handle properly the private type removal
- Reformat analysis work file
- Other minor adjustments
  • Loading branch information
pedrobaeza committed Oct 11, 2024
1 parent a6108a4 commit ea7bcc9
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 154 deletions.
1 change: 0 additions & 1 deletion openupgrade_scripts/scripts/base/17.0.1.3/end-migration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo)
# Copyright 2022 ForgeFlow S.L. <https://www.forgeflow.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade

Expand Down
11 changes: 7 additions & 4 deletions openupgrade_scripts/scripts/base/17.0.1.3/noupdate_changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@
<record id="ec" model="res.country">
<field name="zip_required">0</field>
</record>
<record id="es" model="res.country">
<!-- <record id="es" model="res.country">
<field eval="'%(street)s\n%(street2)s\n%(zip)s %(city)s\n%(state_name)s\n%(country_name)s'" name="address_format"/>
</record>
</record> -->
<record id="europe" model="res.country.group">
<field name="name">European Union</field>
</record>
<record id="main_company" model="res.company">
<!-- <record id="main_company" model="res.company">
<field name="currency_id" ref="base.USD"/>
</record>
</record> -->
<record id="nz" model="res.country">
<field name="vat_label">GST</field>
</record>
<record id="res_partner_rule" model="ir.rule">
<field name="domain_force">['|', '|', ('partner_share', '=', False), ('company_id', 'parent_of', company_ids), ('company_id', '=', False)]</field>
</record>
<record id="sl" model="res.country">
<field name="currency_id" ref="SLE"/>
</record>
Expand Down
8 changes: 1 addition & 7 deletions openupgrade_scripts/scripts/base/17.0.1.3/post-migration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo)
# Copyright 2023 Hunki Enterprises BV (https://hunki-enterprises.com)
# Copyright 2024 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openupgradelib import openupgrade
Expand All @@ -10,16 +10,10 @@
]


def _partner_update_complete_name(env):
partners = env["res.partner"].with_context(active_test=False).search([])
partners._compute_complete_name()


@openupgrade.migrate()
def migrate(env, version):
openupgrade.load_data(env, "base", "17.0.1.3/noupdate_changes.xml")
openupgrade.delete_records_safely_by_xml_id(
env,
_deleted_xml_records,
)
_partner_update_complete_name(env)
181 changes: 123 additions & 58 deletions openupgrade_scripts/scripts/base/17.0.1.3/pre-migration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo)
# Copyright 2020 Odoo Community Association (OCA)
# Copyright 2020 Opener B.V. <stefan@opener.am>
# Copyright 2024 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import logging

Expand All @@ -13,6 +12,10 @@
_logger = logging.getLogger(__name__)

_xmlids_renames = [
(
"mail.model_res_users_settings",
"base.model_res_users_settings",
),
(
"mail.access_res_users_settings_all",
"base.access_res_users_settings_all",
Expand All @@ -34,13 +37,17 @@
"base.constraint_res_users_settings_unique_user_id",
),
]
_column_renames = {
"res_partner": [("display_name", "complete_name")],
}


def _fill_ir_server_object_lines_into_action_server(cr):
openupgrade.logged_query(
cr,
"""
ALTER TABLE ir_act_server
ADD COLUMN IF NOT EXISTS old_ias_id VARCHAR,
ADD COLUMN IF NOT EXISTS evaluation_type VARCHAR,
ADD COLUMN IF NOT EXISTS resource_ref VARCHAR,
ADD COLUMN IF NOT EXISTS selection_value INTEGER,
Expand All @@ -52,79 +59,136 @@ def _fill_ir_server_object_lines_into_action_server(cr):
ADD COLUMN IF NOT EXISTS value TEXT;
""",
)
# Update operations
openupgrade.logged_query(
cr,
"""
WITH tmp AS (
SELECT t1.id, t1.state, t2.col1, t2.value, t2.evaluation_type,
t3.name AS update_field_name, t3.ttype,
t3.relation, t4.id AS selection_field_id
FROM ir_act_server t1
JOIN ir_server_object_lines t2 on t1.id = t2.server_id
JOIN ir_model_fields t3 on t2.col1 = t3.id
LEFT JOIN ir_model_fields_selection t4 on t3.id = t4.field_id
INSERT INTO ir_act_server
(
old_ias_id,
evaluation_type,
update_field_id,
update_path,
update_related_model_id,
value,
resource_ref,
selection_value,
update_boolean_value,
update_m2m_operation
)
SELECT
ias.id,
CASE
WHEN isol.evaluation_type = 'equation' then 'equation'
ELSE 'value'
END,
imf.id,
imf.name,
im.id,
CASE WHEN isol.evaluation_type = 'equation'
THEN isol.value
ELSE NULL
END,
CASE WHEN imf.ttype in ('many2one', 'many2many')
THEN imf.relation || ',' || isol.value
ELSE NULL
END,
imfs.id,
CASE WHEN imf.ttype = 'boolean'
THEN isol.value::bool
ELSE NULL
END,
'add'
FROM ir_act_server ias
JOIN ir_server_object_lines isol ON isol.server_id = ias.id
JOIN ir_model_fields imf ON imf.id = isol.col1
LEFT JOIN ir_model im ON im.model = imf.relation
LEFT JOIN ir_model_fields_selection imfs
ON imf.id = imfs.field_id AND imfs.value = isol.value
WHERE ias.state = 'object_write'
RETURNING id, old_ias_id
""",
)
for row in cr.fetchall():
cr.execute(
"""
INSERT INTO rel_server_actions
(action_id, server_id)
VALUES (%s, %s)
""",
(row[0], row[1]),
)
UPDATE ir_act_server ias
SET
update_field_id = CASE
WHEN tmp.state = 'object_create' THEN NULL
WHEN tmp.state = 'object_write' THEN tmp.col1
ELSE NULL
END,
update_path = CASE
WHEN tmp.state = 'object_create' THEN NULL
WHEN tmp.state = 'object_write' THEN tmp.update_field_name
ELSE NULL
END,
update_related_model_id = CASE
WHEN tmp.state = 'object_write' AND tmp.evaluation_type = 'value'
AND tmp.relation IS NOT NULL THEN
(SELECT id FROM ir_model WHERE model=tmp.relation LIMIT 1)
ELSE NULL
END,
update_m2m_operation = 'add',
evaluation_type = CASE
WHEN tmp.evaluation_type = 'value' then 'value'
WHEN tmp.evaluation_type = 'reference' then 'value'
WHEN tmp.evaluation_type = 'equation' then 'equation'
ELSE 'VALUE'
END,
value = tmp.value,
resource_ref = CASE
WHEN tmp.ttype in ('many2one', 'many2many')
THEN tmp.relation || ',' || tmp.value
ELSE NULL
END,
selection_value = CASE
WHEN tmp.ttype = 'selection' THEN tmp.selection_field_id
ELSE NULL
END,
update_boolean_value = CASE
WHEN tmp.ttype = 'boolean' then 'true'
ELSE NULL
END
FROM tmp
WHERE ias.id = tmp.id
openupgrade.logged_query(
cr,
"""UPDATE ir_act_server ias
SET state = 'multi'
FROM ir_server_object_lines isol
WHERE ias.state = 'object_write'
AND isol.server_id = ias.id
""",
)
# Create operations
openupgrade.logged_query(
cr,
"""UPDATE ir_act_server ias
SET value = isol.value
FROM ir_server_object_lines isol
JOIN ir_model_fields imf ON imf.id = isol.col1
WHERE ias.state = 'object_create'
AND isol.server_id = ias.id
AND isol.evaluation_type = 'value'
AND imf.name = 'name'
""",
)


def _partner_create_column_complete_name(cr):
def _fill_empty_country_codes(cr):
openupgrade.logged_query(
cr,
"""
ALTER TABLE res_partner
ADD COLUMN IF NOT EXISTS complete_name VARCHAR;
UPDATE res_country
SET code = 'OU' || id::VARCHAR
WHERE code IS NULL
""",
)


def _update_partner_private_type(cr):
def _handle_partner_private_type(cr):
# Copy private records into a new table
openupgrade.logged_query(
cr,
"""
CREATE TABLE ou_res_partner_private AS
SELECT * FROM res_partner
WHERE type = 'private'
""",
)
# Copy column for preserving the old type values
_column_copies = {"res_partner": [("type", None, None)]}
openupgrade.copy_columns(cr, _column_copies)
# Change contact type and erase sensitive information
query = "type = 'contact'"
for field in [
"street",
"street2",
"city",
"zip",
"vat",
"function",
"phone",
"mobile",
"email",
"website",
"comment",
]:
query += f", {field} = CASE WHEN {field} IS NULL THEN NULL ELSE '*****' END"
openupgrade.logged_query(
cr,
f"""
UPDATE res_partner
SET type = 'contact'
SET {query},
country_id = NULL,
state_id = NULL
WHERE type = 'private'
""",
)
Expand All @@ -146,6 +210,7 @@ def migrate(cr, version):
openupgrade.update_module_names(cr, renamed_modules.items())
openupgrade.update_module_names(cr, merged_modules.items(), merge_modules=True)
openupgrade.rename_xmlids(cr, _xmlids_renames)
openupgrade.rename_columns(cr, _column_renames)
_fill_ir_server_object_lines_into_action_server(cr)
_update_partner_private_type(cr)
_partner_create_column_complete_name(cr)
_fill_empty_country_codes(cr)
_handle_partner_private_type(cr)
Loading

0 comments on commit ea7bcc9

Please sign in to comment.