From 1cb31cf2c2f4a914bc946a491c7a2b3d132929d9 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 16 Jan 2024 09:42:57 +0100 Subject: [PATCH] [FIX] runbot_merge: 1.9 version & migration Forgot to bump the version when creating the migration. Also convert the migration to a single sql query, although the migration will never run because I ran the query manually to fix things up after finding out the data was "dirty" since the new code (assuming only modern statuses) was merged without running the migration. Thankfully it looks like the impact was not too severe (because the legacy statuses should only be present on very old commits / PRs), I don't remember when I deployed the update but apparently just a pair of PRs got affected, because their `previous_failure` was the old style and thus broke the "new failure" check. --- runbot_merge/__manifest__.py | 2 +- .../migrations/15.0.1.9/pre-migration.py | 36 +++++++++---------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/runbot_merge/__manifest__.py b/runbot_merge/__manifest__.py index 266605d3..d36aea6e 100644 --- a/runbot_merge/__manifest__.py +++ b/runbot_merge/__manifest__.py @@ -1,6 +1,6 @@ { 'name': 'merge bot', - 'version': '1.8', + 'version': '1.9', 'depends': ['contacts', 'website'], 'data': [ 'security/security.xml', diff --git a/runbot_merge/migrations/15.0.1.9/pre-migration.py b/runbot_merge/migrations/15.0.1.9/pre-migration.py index c8c31582..76539967 100644 --- a/runbot_merge/migrations/15.0.1.9/pre-migration.py +++ b/runbot_merge/migrations/15.0.1.9/pre-migration.py @@ -9,28 +9,24 @@ def migrate(cr, version): # style is like commit statuses, with the contexts at the toplevel # and the status info below. cr.execute(""" -UPDATE runbot_merge +UPDATE runbot_merge_pull_requests SET previous_failure = '{}' WHERE previous_failure::jsonb ? 'state' """) - # Getting this into postgres native manipulations seems a bit too - # complicated, and not really necessary. cr.execute(""" -SELECT id, statuses::json - FROM runbot_merge_commit - WHERE jsonb_path_match(statuses::jsonb, '$.*.type() != "object"') -""") - updated = [ - (id, { - k: {'state': r, 'target_url': None, 'description': None} - for k, r in st.items() - }) - for id, st in cr.fetchall() - ] - execute_values(cr._obj, """ -UPDATE runbot_merge_commit c - SET c.statuses = data.st - FROM (VALUES %s) AS data (id, st) - WHERE c.id = data.id -""", updated) +WITH new_statuses (id, statuses) AS ( + SELECT id, json_object_agg( + key, + CASE WHEN jsonb_typeof(value) = 'string' + THEN jsonb_build_object('state', value, 'target_url', null, 'description', null) + ELSE value + END + ) AS statuses + FROM runbot_merge_commit + CROSS JOIN LATERAL jsonb_each(statuses::jsonb) s + WHERE jsonb_path_match(statuses::jsonb, '$.*.type() != "object"') + GROUP BY id +) +UPDATE runbot_merge_commit SET statuses = new_statuses.statuses FROM new_statuses WHERE runbot_merge_commit.id = new_statuses.id + """)