|
| 1 | +# Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo) |
| 2 | +# Copyright 2024 Tecnativa - Pedro M. Baeza |
| 3 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 4 | +import logging |
| 5 | + |
| 6 | +from openupgradelib import openupgrade |
| 7 | + |
| 8 | +from odoo import tools |
| 9 | + |
| 10 | +from odoo.addons.openupgrade_scripts.apriori import merged_modules, renamed_modules |
| 11 | + |
| 12 | +_logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +_xmlids_renames = [ |
| 15 | + ( |
| 16 | + "mail.model_res_users_settings", |
| 17 | + "base.model_res_users_settings", |
| 18 | + ), |
| 19 | + ( |
| 20 | + "mail.access_res_users_settings_all", |
| 21 | + "base.access_res_users_settings_all", |
| 22 | + ), |
| 23 | + ( |
| 24 | + "mail.access_res_users_settings_user", |
| 25 | + "base.access_res_users_settings_user", |
| 26 | + ), |
| 27 | + ( |
| 28 | + "mail.res_users_settings_rule_admin", |
| 29 | + "base.res_users_settings_rule_admin", |
| 30 | + ), |
| 31 | + ( |
| 32 | + "mail.res_users_settings_rule_user", |
| 33 | + "base.res_users_settings_rule_user", |
| 34 | + ), |
| 35 | + ( |
| 36 | + "mail.constraint_res_users_settings_unique_user_id", |
| 37 | + "base.constraint_res_users_settings_unique_user_id", |
| 38 | + ), |
| 39 | +] |
| 40 | +_column_renames = { |
| 41 | + "res_partner": [("display_name", "complete_name")], |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +def _fill_ir_server_object_lines_into_action_server(cr): |
| 46 | + openupgrade.logged_query( |
| 47 | + cr, |
| 48 | + """ |
| 49 | + ALTER TABLE ir_act_server |
| 50 | + ADD COLUMN IF NOT EXISTS old_ias_id VARCHAR, |
| 51 | + ADD COLUMN IF NOT EXISTS evaluation_type VARCHAR, |
| 52 | + ADD COLUMN IF NOT EXISTS resource_ref VARCHAR, |
| 53 | + ADD COLUMN IF NOT EXISTS selection_value INTEGER, |
| 54 | + ADD COLUMN IF NOT EXISTS update_boolean_value VARCHAR, |
| 55 | + ADD COLUMN IF NOT EXISTS update_field_id INTEGER, |
| 56 | + ADD COLUMN IF NOT EXISTS update_m2m_operation VARCHAR, |
| 57 | + ADD COLUMN IF NOT EXISTS update_path VARCHAR, |
| 58 | + ADD COLUMN IF NOT EXISTS update_related_model_id INTEGER, |
| 59 | + ADD COLUMN IF NOT EXISTS value TEXT; |
| 60 | + """, |
| 61 | + ) |
| 62 | + # Update operations |
| 63 | + openupgrade.logged_query( |
| 64 | + cr, |
| 65 | + """ |
| 66 | + INSERT INTO ir_act_server |
| 67 | + ( |
| 68 | + old_ias_id, |
| 69 | + evaluation_type, |
| 70 | + update_field_id, |
| 71 | + update_path, |
| 72 | + update_related_model_id, |
| 73 | + value, |
| 74 | + resource_ref, |
| 75 | + selection_value, |
| 76 | + update_boolean_value, |
| 77 | + update_m2m_operation, |
| 78 | + binding_type, |
| 79 | + state, |
| 80 | + type, |
| 81 | + usage, |
| 82 | + model_id, |
| 83 | + name |
| 84 | + ) |
| 85 | + SELECT |
| 86 | + ias.id, |
| 87 | + CASE |
| 88 | + WHEN isol.evaluation_type = 'equation' then 'equation' |
| 89 | + ELSE 'value' |
| 90 | + END, |
| 91 | + imf.id, |
| 92 | + imf.name, |
| 93 | + im.id, |
| 94 | + CASE WHEN isol.evaluation_type = 'equation' |
| 95 | + THEN isol.value |
| 96 | + ELSE NULL |
| 97 | + END, |
| 98 | + CASE WHEN imf.ttype in ('many2one', 'many2many') |
| 99 | + THEN imf.relation || ',' || isol.value |
| 100 | + ELSE NULL |
| 101 | + END, |
| 102 | + imfs.id, |
| 103 | + CASE WHEN imf.ttype = 'boolean' |
| 104 | + THEN isol.value::bool |
| 105 | + ELSE NULL |
| 106 | + END, |
| 107 | + 'add', |
| 108 | + 'action', |
| 109 | + 'object_write', |
| 110 | + 'ir.actions.server', |
| 111 | + 'ir_actions_server', |
| 112 | + ias.model_id, |
| 113 | + ias.name |
| 114 | + FROM ir_act_server ias |
| 115 | + JOIN ir_server_object_lines isol ON isol.server_id = ias.id |
| 116 | + JOIN ir_model_fields imf ON imf.id = isol.col1 |
| 117 | + LEFT JOIN ir_model im ON im.model = imf.relation |
| 118 | + LEFT JOIN ir_model_fields_selection imfs |
| 119 | + ON imf.id = imfs.field_id AND imfs.value = isol.value |
| 120 | + WHERE ias.state = 'object_write' |
| 121 | + RETURNING id, old_ias_id |
| 122 | + """, |
| 123 | + ) |
| 124 | + for row in cr.fetchall(): |
| 125 | + cr.execute( |
| 126 | + """ |
| 127 | + INSERT INTO rel_server_actions |
| 128 | + (action_id, server_id) |
| 129 | + VALUES (%s, %s) |
| 130 | + """, |
| 131 | + (row[0], row[1]), |
| 132 | + ) |
| 133 | + openupgrade.logged_query( |
| 134 | + cr, |
| 135 | + """UPDATE ir_act_server ias |
| 136 | + SET state = 'multi' |
| 137 | + FROM ir_server_object_lines isol |
| 138 | + WHERE ias.state = 'object_write' |
| 139 | + AND isol.server_id = ias.id |
| 140 | + """, |
| 141 | + ) |
| 142 | + # Create operations |
| 143 | + openupgrade.logged_query( |
| 144 | + cr, |
| 145 | + """UPDATE ir_act_server ias |
| 146 | + SET value = isol.value |
| 147 | + FROM ir_server_object_lines isol |
| 148 | + JOIN ir_model_fields imf ON imf.id = isol.col1 |
| 149 | + WHERE ias.state = 'object_create' |
| 150 | + AND isol.server_id = ias.id |
| 151 | + AND isol.evaluation_type = 'value' |
| 152 | + AND imf.name = 'name' |
| 153 | + """, |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +def _fill_empty_country_codes(cr): |
| 158 | + openupgrade.logged_query( |
| 159 | + cr, |
| 160 | + """ |
| 161 | + UPDATE res_country |
| 162 | + SET code = 'OU' || id::VARCHAR |
| 163 | + WHERE code IS NULL |
| 164 | + """, |
| 165 | + ) |
| 166 | + |
| 167 | + |
| 168 | +def _handle_partner_private_type(cr): |
| 169 | + # Copy private records into a new table |
| 170 | + openupgrade.logged_query( |
| 171 | + cr, |
| 172 | + """ |
| 173 | + CREATE TABLE ou_res_partner_private AS |
| 174 | + SELECT * FROM res_partner |
| 175 | + WHERE type = 'private' |
| 176 | + """, |
| 177 | + ) |
| 178 | + # Copy column for preserving the old type values |
| 179 | + _column_copies = {"res_partner": [("type", None, None)]} |
| 180 | + openupgrade.copy_columns(cr, _column_copies) |
| 181 | + # Change contact type and erase sensitive information |
| 182 | + query = "type = 'contact'" |
| 183 | + for field in [ |
| 184 | + "street", |
| 185 | + "street2", |
| 186 | + "city", |
| 187 | + "zip", |
| 188 | + "vat", |
| 189 | + "function", |
| 190 | + "phone", |
| 191 | + "mobile", |
| 192 | + "email", |
| 193 | + "website", |
| 194 | + "comment", |
| 195 | + ]: |
| 196 | + query += f", {field} = CASE WHEN {field} IS NULL THEN NULL ELSE '*****' END" |
| 197 | + openupgrade.logged_query( |
| 198 | + cr, |
| 199 | + f""" |
| 200 | + UPDATE res_partner |
| 201 | + SET {query}, |
| 202 | + country_id = NULL, |
| 203 | + state_id = NULL |
| 204 | + WHERE type = 'private' |
| 205 | + """, |
| 206 | + ) |
| 207 | + |
| 208 | + |
| 209 | +@openupgrade.migrate(use_env=False) |
| 210 | +def migrate(cr, version): |
| 211 | + """ |
| 212 | + Don't request an env for the base pre-migration as flushing the env in |
| 213 | + odoo/modules/registry.py will break on the 'base' module not yet having |
| 214 | + been instantiated. |
| 215 | + """ |
| 216 | + if "openupgrade_framework" not in tools.config["server_wide_modules"]: |
| 217 | + _logger.error( |
| 218 | + "openupgrade_framework is not preloaded. You are highly " |
| 219 | + "recommended to run the Odoo with --load=openupgrade_framework " |
| 220 | + "when migrating your database." |
| 221 | + ) |
| 222 | + openupgrade.update_module_names(cr, renamed_modules.items()) |
| 223 | + openupgrade.update_module_names(cr, merged_modules.items(), merge_modules=True) |
| 224 | + openupgrade.rename_xmlids(cr, _xmlids_renames) |
| 225 | + openupgrade.rename_columns(cr, _column_renames) |
| 226 | + _fill_ir_server_object_lines_into_action_server(cr) |
| 227 | + _fill_empty_country_codes(cr) |
| 228 | + _handle_partner_private_type(cr) |
0 commit comments