Skip to content

[ADD] util/update_table_from_dict #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Pirols
Copy link
Contributor

@Pirols Pirols commented Jul 18, 2025

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.

@robodoo
Copy link
Contributor

robodoo commented Jul 18, 2025

Pull request status dashboard

@Pirols
Copy link
Contributor Author

Pirols commented Jul 18, 2025

@KangOl
Copy link
Contributor

KangOl commented Jul 18, 2025

upgradeci retry with always only base

@Pirols Pirols force-pushed the master-add_update_table_from_dict-pied branch 5 times, most recently from 1e5c9a3 to 31ecb56 Compare July 22, 2025 13:15
@Pirols Pirols requested review from KangOl and aj-fuentes July 22, 2025 14:55
@Pirols Pirols force-pushed the master-add_update_table_from_dict-pied branch 2 times, most recently from 3dbdbe7 to e3ab180 Compare July 22, 2025 15:01
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:

```python
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:

```python
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.
@Pirols Pirols force-pushed the master-add_update_table_from_dict-pied branch from e3ab180 to 876a5d9 Compare July 22, 2025 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants