Skip to content

Commit

Permalink
Merge pull request #336 from ForgeFlow/master-add-get_model2table
Browse files Browse the repository at this point in the history
[ADD] get_model2table: method to map nonstandard table names
  • Loading branch information
pedrobaeza authored Jul 14, 2023
2 parents bb6dbfe + 88973d2 commit 7819744
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
38 changes: 29 additions & 9 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def do_raise(error):
"update_module_names",
"add_ir_model_fields",
"get_legacy_name",
"get_model2table",
"m2o_to_x2m",
"float_to_integer",
"message",
Expand Down Expand Up @@ -875,8 +876,8 @@ def rename_models(cr, model_spec):
"""
for (old, new) in model_spec:
logger.info("model %s: renaming to %s", old, new)
_old = old.replace(".", "_")
_new = new.replace(".", "_")
_old = get_model2table(old)
_new = get_model2table(new)
logged_query(
cr,
"UPDATE ir_model SET model = %s WHERE model = %s",
Expand Down Expand Up @@ -996,7 +997,7 @@ def rename_models(cr, model_spec):
)
rows = cr.fetchall()
for row in rows:
table = row[0].replace(".", "_")
table = get_model2table(row[0])
if not table_exists(cr, table):
continue
column = row[1]
Expand Down Expand Up @@ -1094,7 +1095,7 @@ def merge_models(cr, old_model, new_model, ref_field):
tables. You should have to do that previously in the migration scripts.
"""
logger.info("model %s: merging to %s", old_model, new_model)
model_table = new_model.replace(".", "_")
model_table = get_model2table(new_model)
renames = [
("ir_attachment", "res_model", "res_id", ""),
("ir_model_data", "model", "res_id", ""),
Expand Down Expand Up @@ -1251,7 +1252,7 @@ def rename_xmlids(cr, xmlids_spec, allow_merge=False):
"to the same model (%s, %s)"
% (old, new, old_row[1], new_row[1])
)
table = old_row[1].replace(".", "_")
table = get_model2table(old_row[1])
if not table_exists(cr, table):
do_raise(
"Cannot merge XMLIDs %s, %s because the table I "
Expand Down Expand Up @@ -1815,6 +1816,22 @@ def get_legacy_name(original_name):
)


def get_model2table(model):
# map of nonstandard table names
model2table = {
"ir.actions.actions": "ir_actions",
"ir.actions.act_window": "ir_act_window",
"ir.actions.act_window.view": "ir_act_window_view",
"ir.actions.act_window_close": "ir_actions",
"ir.actions.act_url": "ir_act_url",
"ir.actions.server": "ir_act_server",
"ir.actions.client": "ir_act_client",
"ir.actions.report.xml": "ir_act_report_xml", # model < v11
"ir.actions.report": "ir_act_report_xml", # model >= v11
}
return model2table.get(model, model.replace(".", "_"))


def m2o_to_x2m(cr, model, table, field, source_field):
"""
Transform many2one relations into one2many or many2many.
Expand Down Expand Up @@ -2644,7 +2661,7 @@ def delete_record_translations(cr, module, xml_ids, field_list=None):
),
)
else:
table = model.replace(".", "_")
table = get_model2table(model)
# we use information_schema to assure the columns exist
cr.execute(
"""
Expand Down Expand Up @@ -2875,7 +2892,10 @@ def add_fields(env, field_spec):
init_value = vals[6] if len(vals) > 6 else False
# Add SQL column
if not table_name:
table_name = env[model_name]._table
try:
table_name = env[model_name]._table
except KeyError:
table_name = get_model2table(model_name)
if not column_exists(env.cr, table_name, field_name):
sql_type = sql_type or sql_type_mapping.get(field_type)
if sql_type:
Expand Down Expand Up @@ -2965,7 +2985,7 @@ def add_fields(env, field_spec):
# Add ir.model.data entry
if not module or version_info[0] >= 12:
continue
name1 = "field_%s_%s" % (model_name.replace(".", "_"), field_name)
name1 = "field_%s_%s" % (table_name, field_name)
try:
with env.cr.savepoint():
logged_query(
Expand Down Expand Up @@ -3090,7 +3110,7 @@ def update_module_moved_models(cr, model, old_module, new_module):
:param old_module: Previous module of the models
:param new_module: New module of the models
"""
table = model.replace(".", "_")
table = get_model2table(model)
logger.info(
"Moving model %s from module '%s' to module '%s'", model, old_module, new_module
)
Expand Down
38 changes: 24 additions & 14 deletions openupgradelib/openupgrade_merge_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from psycopg2.errorcodes import UNDEFINED_COLUMN, UNIQUE_VIOLATION
from psycopg2.extensions import AsIs

from .openupgrade import logged_query, version_info
from .openupgrade import get_model2table, logged_query, version_info
from .openupgrade_tools import column_exists, table_exists

logger = logging.getLogger("OpenUpgrade")
Expand Down Expand Up @@ -216,11 +216,11 @@ def _change_reference_refs_sql(
for row in rows:
try:
model = env[row[0]]
if not model._auto: # Discard SQL views
continue
table = model._table
except KeyError:
continue
if not model._auto: # Discard SQL views
continue
table = model._table
table = get_model2table(row[0])
if not table_exists(cr, table):
continue
column = row[1]
Expand Down Expand Up @@ -757,9 +757,10 @@ def _change_generic(
]:
try:
model = env[model_to_replace].with_context(active_test=False)
table = model._table
except KeyError:
continue
if (model._table, res_id_column) in exclude_columns:
table = get_model2table(model_to_replace)
if (table, res_id_column) in exclude_columns:
continue
if method == "orm":
if not model._fields.get(model_column) or not model._fields.get(
Expand Down Expand Up @@ -794,12 +795,12 @@ def _change_generic(
"Changed %s record(s) of model '%s'", len(records), model_to_replace
)
else:
if not column_exists(
env.cr, model._table, res_id_column
) or not column_exists(env.cr, model._table, model_column):
if not column_exists(env.cr, table, res_id_column) or not column_exists(
env.cr, table, model_column
):
continue
format_args = {
"table": sql.Identifier(model._table),
"table": sql.Identifier(table),
"res_id_column": sql.Identifier(res_id_column),
"model_column": sql.Identifier(model_column),
}
Expand Down Expand Up @@ -861,7 +862,10 @@ def _delete_records_sql(
env, model_name, record_ids, target_record_id, model_table=None
):
if not model_table:
model_table = env[model_name]._table
try:
model_table = env[model_name]._table
except KeyError:
model_table = get_model2table(model_name)
logged_query(
env.cr,
"DELETE FROM ir_model_data WHERE model = %s AND res_id IN %s",
Expand Down Expand Up @@ -892,7 +896,10 @@ def _delete_records_orm(env, model_name, record_ids, target_record_id):

def _check_recurrence(env, model_name, record_ids, target_record_id, model_table=None):
if not model_table:
model_table = env[model_name]._table
try:
model_table = env[model_name]._table
except KeyError:
model_table = get_model2table(model_name)
env.cr.execute(
"""
SELECT tc.table_name, kcu.column_name, COALESCE(imf.column1, 'id')
Expand Down Expand Up @@ -1015,7 +1022,10 @@ def merge_records(
else:
# Check which records to be merged exist
if not model_table:
model_table = env[model_name]._table
try:
model_table = env[model_name]._table
except KeyError:
model_table = get_model2table(model_name)
env.cr.execute(
sql.SQL("SELECT id FROM {} WHERE id IN %s").format(
sql.Identifier(model_table)
Expand Down

0 comments on commit 7819744

Please sign in to comment.