OCA / openupgradelib

A library with support functions to be called from Odoo migration scripts.
GNU Affero General Public License v3.0
85 stars 171 forks source link

rename_selection_value: new function of openupgradelib #361

Open jcdrubay opened 5 months ago

jcdrubay commented 5 months ago

Is your feature request related to a problem?

Not a problem, but it's a new feature that facilitates the work in case the selection values needs to be updated.

A typical use case might be in case a typo has been made in the selection value and made it to a production database. (not the case of an upgrade of major odoo version upgrade).

I am sure that there are more cases where this could also be useful.

Describe the solution you'd like

In https://github.com/OCA/openupgradelib/blob/master/openupgradelib/openupgrade.py

def rename_selection_value(env, model_name, table_name, field_name, old_value, new_value):
    openupgrade.logged_query(
        env.cr,
        f"""
        UPDATE {table_name}
        SET {field_name} = '{new_value}'
        WHERE check_function = '{old_value}' """,
    )
    openupgrade.logged_query(
        env.cr,
        f"""
        DELETE FROM ir_model_fields_selection imfs
        WHERE
            imfs.value = '{old_value}'
            and exists (
                SELECT 1
                FROM ir_model_fields imf
                WHERE
                    imf.name='{field_name}'
                    AND imf.model='{model_name}'
                    AND imf.id = imfs.field_id
            )
        """,
    )

Next

Let me know if you think this is relevant and then I ll create a PR.

jcdrubay commented 5 months ago

We could also split into 2 functions:

pedrobaeza commented 5 months ago

We have already map_values for changing values in DB, and isn't the ir_model_fields_selection changed automatically by the ORM?