From dcb78a45e9e48c5c7c3c5bff5c41ff030f0c2ff8 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Thu, 23 Nov 2023 11:53:31 +0100 Subject: [PATCH 1/2] [IMP] rename_fields: Be able to use a cursor instead of Environment In some cases, we want to use the rename_fields() method without using a full environment but with a cursor instead. --- openupgradelib/openupgrade.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/openupgradelib/openupgrade.py b/openupgradelib/openupgrade.py index cae812e7..774b5df8 100644 --- a/openupgradelib/openupgrade.py +++ b/openupgradelib/openupgrade.py @@ -658,7 +658,9 @@ def rename_fields(env, field_spec, no_deep=False): :param no_deep: If True, avoids to perform any operation that involves the environment. Not used for now. """ - cr = env.cr + # This method has never implemented Environment usage apart cursor. + # To keep backward compatibility, check if env == Environment + cr = env.cr if isinstance(env, core.api.Environment) else env for model, table, old_field, new_field in field_spec: if column_exists(cr, table, old_field): rename_columns(cr, {table: [(old_field, new_field)]}) @@ -776,7 +778,7 @@ def rename_fields(env, field_spec, no_deep=False): (model,), ) # TODO: Rename when the field in ir_ui_view_custom - if table_exists(env.cr, "mail_alias"): + if table_exists(cr, "mail_alias"): # Rename appearances on mail alias cr.execute( """ From 86f48a552bd8c1360f878a6aa8a6e8f108213d7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Fri, 24 Nov 2023 09:25:46 +0100 Subject: [PATCH 2/2] [IMP] add rename_fields_cr, keep rename_fields for backwards compatibility --- openupgradelib/openupgrade.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/openupgradelib/openupgrade.py b/openupgradelib/openupgrade.py index 774b5df8..4977b040 100644 --- a/openupgradelib/openupgrade.py +++ b/openupgradelib/openupgrade.py @@ -632,9 +632,16 @@ def rename_columns(cr, column_spec): def rename_fields(env, field_spec, no_deep=False): - """Rename fields. Typically called in the pre script. WARNING: If using - this on base module, pass the argument ``no_deep`` with True value for - avoiding the using of the environment (which is not yet loaded). + """Rename fields. See rename_fields_cr for details. + + :param no_deep: If True, avoids to perform any operation that involves + the environment. Not used for now. + """ + return rename_fields_cr(env.cr, field_spec) + + +def rename_fields_cr(cr, field_spec): + """Rename fields. Typically called in the pre script. This, in contrast of ``rename_columns``, performs all the steps for completely rename a field from one name to another. This is needed for @@ -647,20 +654,12 @@ def rename_fields(env, field_spec, no_deep=False): This method performs also the SQL column renaming, so only one call is needed. - :param env: Environment/pool variable. The database cursor is the only - thing needed, but added in prevision of TODO tasks for not breaking - API later. :param field_spec: a list of tuples with the following elements: * Model name. The name of the Odoo model * Table name. The name of the SQL table for the model. * Old field name. The name of the old field. * New field name. The name of the new field. - :param no_deep: If True, avoids to perform any operation that involves - the environment. Not used for now. """ - # This method has never implemented Environment usage apart cursor. - # To keep backward compatibility, check if env == Environment - cr = env.cr if isinstance(env, core.api.Environment) else env for model, table, old_field, new_field in field_spec: if column_exists(cr, table, old_field): rename_columns(cr, {table: [(old_field, new_field)]})