odoo/upgrade-util#297

Created by Upgrade, Edoardo Piroli (pied)

Blocked

label
odoo-dev:master-add_update_table_from_dict-pied
head
876a5d994075fdffa617d6191371cd182f69cafb
odoo/upgrade-util
master #297 missing r+

[ADD] util/update_table_from_dict

A recurrent challenge in writing upgrade scripts is that of updating values in a table based on some form of already available mapping from the id (or another identifier) to the new value, this is often addressed with an iterative solution in the form:

for key, value in mapping.items():
    cr.execute(
        """
        UPDATE table
           SET col = %s
         WHERE key_col = %s
        """,
        [value, key],
    )

or in a more efficient (only issuing a single query) but hacky way:

cr.execute(
    """
    UPDATE table
       SET col = (%s::jsonb)->>(key_col::text)
     WHERE key_col = ANY(%s)
    """,
    [json.dumps(mapping), list(mapping)],
)

With the former being ineffective for big mappings and the latter often requiring some comments at review time to get it right. This commit introduces a util meant to make it easier to efficiently perform such updates.