|
16 | 16 |
|
17 | 17 | import psycopg2
|
18 | 18 | from psycopg2 import sql
|
19 |
| -from psycopg2.extras import Json, execute_values |
| 19 | +from psycopg2.extras import Json |
20 | 20 |
|
21 | 21 | try:
|
22 | 22 | from odoo import release
|
@@ -142,63 +142,6 @@ def _get_resolved_ir_exports(cr, models=None, fields=None):
|
142 | 142 | return result
|
143 | 143 |
|
144 | 144 |
|
145 |
| -def rename_ir_exports_fields(cr, models_fields_map): |
146 |
| - """ |
147 |
| - Rename fields references in ir.exports.line records. |
148 |
| -
|
149 |
| - :param dict[str, dict[str, str]] models_fields_map: a dict of models to the fields rename dict, |
150 |
| - like: `{"model.name": {"old_field": "new_field", ...}, ...}` |
151 |
| -
|
152 |
| - :meta private: exclude from online docs |
153 |
| - """ |
154 |
| - matching_exports = _get_resolved_ir_exports( |
155 |
| - cr, |
156 |
| - fields=[(model, field) for model, fields_map in models_fields_map.items() for field in fields_map], |
157 |
| - ) |
158 |
| - if not matching_exports: |
159 |
| - return |
160 |
| - _logger.debug("Renaming %d export template lines with renamed fields", len(matching_exports)) |
161 |
| - fixed_lines_paths = {} |
162 |
| - for line_id, resolved_paths in matching_exports.items(): |
163 |
| - for path_part in resolved_paths: |
164 |
| - assert path_part.field_model in models_fields_map |
165 |
| - fields_map = models_fields_map[path_part.field_model] |
166 |
| - assert path_part.field_name in fields_map |
167 |
| - assert path_part.path[path_part.part_index - 1] == path_part.field_name |
168 |
| - new_field_name = fields_map[path_part.field_name] |
169 |
| - fixed_path = fixed_lines_paths.get(line_id, list(path_part.path)) |
170 |
| - fixed_path[path_part.part_index - 1] = new_field_name |
171 |
| - fixed_lines_paths[line_id] = fixed_path |
172 |
| - execute_values( |
173 |
| - cr, |
174 |
| - """ |
175 |
| - UPDATE ir_exports_line el |
176 |
| - SET name = v.name |
177 |
| - FROM (VALUES %s) AS v(id, name) |
178 |
| - WHERE el.id = v.id |
179 |
| - """, |
180 |
| - [(k, "/".join(v)) for k, v in fixed_lines_paths.items()], |
181 |
| - ) |
182 |
| - |
183 |
| - |
184 |
| -def remove_ir_exports_lines(cr, models=None, fields=None): |
185 |
| - """ |
186 |
| - Delete ir.exports.line records that reference models or fields that are/will be removed. |
187 |
| -
|
188 |
| - Only one of ``models`` or ``fields`` arguments should be provided. |
189 |
| -
|
190 |
| - :param list[str] models: a list of model names to match in exports |
191 |
| - :param list[(str, str)] fields: a list of (model, field) tuples to match in exports |
192 |
| -
|
193 |
| - :meta private: exclude from online docs |
194 |
| - """ |
195 |
| - matching_exports = _get_resolved_ir_exports(cr, models=models, fields=fields) |
196 |
| - if not matching_exports: |
197 |
| - return |
198 |
| - _logger.debug("Deleting %d export template lines with removed models/fields", len(matching_exports)) |
199 |
| - cr.execute("DELETE FROM ir_exports_line WHERE id IN %s", [tuple(matching_exports.keys())]) |
200 |
| - |
201 |
| - |
202 | 145 | def ensure_m2o_func_field_data(cr, src_table, column, dst_table):
|
203 | 146 | """
|
204 | 147 | Fix broken m2o relations.
|
@@ -323,7 +266,9 @@ def clean_context(context):
|
323 | 266 | )
|
324 | 267 |
|
325 | 268 | # ir.exports.line
|
326 |
| - remove_ir_exports_lines(cr, fields=[(model, fieldname)]) |
| 269 | + matching_exports = _get_resolved_ir_exports(cr, fields=[(model, fieldname)]) |
| 270 | + if matching_exports: |
| 271 | + cr.execute("DELETE FROM ir_exports_line WHERE id IN %s", [tuple(matching_exports.keys())]) |
327 | 272 |
|
328 | 273 | def adapter(leaf, is_or, negated):
|
329 | 274 | # replace by TRUE_LEAF, unless negated or in a OR operation but not negated
|
@@ -1195,7 +1140,31 @@ def _update_field_usage_multi(cr, models, old, new, domain_adapter=None, skip_in
|
1195 | 1140 |
|
1196 | 1141 | # ir.exports.line
|
1197 | 1142 | if only_models:
|
1198 |
| - rename_ir_exports_fields(cr, {model: {old: new} for model in only_models}) |
| 1143 | + cr.execute( |
| 1144 | + """ |
| 1145 | + SELECT el.id, |
| 1146 | + e.resource, |
| 1147 | + STRING_TO_ARRAY(el.name, '/') |
| 1148 | + FROM ir_exports_line el |
| 1149 | + JOIN ir_exports e |
| 1150 | + ON el.export_id = e.id |
| 1151 | + WHERE el.name ~ %s |
| 1152 | + """, |
| 1153 | + [r"\y{}\y".format(old)], |
| 1154 | + ) |
| 1155 | + fixed_lines_paths = {} |
| 1156 | + for line_id, model, path in cr.fetchall(): |
| 1157 | + new_path = [ |
| 1158 | + new if x.field_name == old and x.field_model in only_models else x.field_name |
| 1159 | + for x in resolve_model_fields_path(cr, model, path) |
| 1160 | + ] |
| 1161 | + if len(new_path) == len(path) and new_path != path: |
| 1162 | + fixed_lines_paths[line_id] = "/".join(new_path) |
| 1163 | + if fixed_lines_paths: |
| 1164 | + cr.execute( |
| 1165 | + "UPDATE ir_exports_line SET name = (%s::jsonb)->>(id::text) WHERE id IN %s", |
| 1166 | + [Json(fixed_lines_paths), tuple(fixed_lines_paths)], |
| 1167 | + ) |
1199 | 1168 |
|
1200 | 1169 | # mail.alias
|
1201 | 1170 | if column_exists(cr, "mail_alias", "alias_defaults"):
|
|
0 commit comments