Skip to content

Commit

Permalink
[IMP] runbot_merge: split staging heads out to join tables
Browse files Browse the repository at this point in the history
Currently the heads of a staging (both staging heads and merged heads)
are just JSON data on the staging itself. Historically this was
convenient as the heads were mostly of use to the staging process, and
thus accessed directly through the staging essentially exclusively.

However this makes finding stagings from merged commits e.g. for
forensic research almost impossible, because querying based on
the *values* of a JSON map is expensive, and indexing it is difficult.

To make this use case more feasible, split the `heads` field into two
join tables, one for the staging heads and one for the merged heads,
this makes looking for stagings by commits much more
efficient (although the queries may not be trivial). Also add two
utility RPC methods, so that it's possible to query stagings
reasonably easily and efficiently based on a set of commits (branch
heads).

related to #768
  • Loading branch information
xmo-odoo committed Aug 10, 2023
1 parent cdffa83 commit b1af2e5
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 117 deletions.
2 changes: 1 addition & 1 deletion runbot_merge/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
'name': 'merge bot',
'version': '1.7',
'version': '1.8',
'depends': ['contacts', 'website'],
'data': [
'security/security.xml',
Expand Down
6 changes: 6 additions & 0 deletions runbot_merge/migrations/15.0.1.8/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pathlib import Path

def migrate(cr, version):
sql = Path(__file__).parent.joinpath('upgrade.sql')\
.read_text(encoding='utf-8')
cr.execute(sql)
62 changes: 62 additions & 0 deletions runbot_merge/migrations/15.0.1.8/upgrade.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
CREATE TABLE runbot_merge_stagings_commits (
id serial NOT NULL,
staging_id integer not null references runbot_merge_stagings (id),
commit_id integer not null references runbot_merge_commit (id),
repository_id integer not null references runbot_merge_repository (id)
);

CREATE TABLE runbot_merge_stagings_heads (
id serial NOT NULL,
staging_id integer NOT NULL REFERENCES runbot_merge_stagings (id),
commit_id integer NOT NULL REFERENCES runbot_merge_commit (id),
repository_id integer NOT NULL REFERENCES runbot_merge_repository (id)
);

-- some of the older stagings only have the head, not the commit,
-- add the commit
UPDATE runbot_merge_stagings
SET heads = heads::jsonb || jsonb_build_object(
'odoo/odoo^', heads::json->'odoo/odoo',
'odoo/enterprise^', heads::json->'odoo/enterprise'
)
WHERE heads NOT ILIKE '%^%';

-- some of the stagings have heads which don't exist in the commits table,
-- because they never got a status from the runbot...
-- create fake commits so we don't lose heads
INSERT INTO runbot_merge_commit (sha, statuses, create_uid, create_date, write_uid, write_date)
SELECT r.value, '{}', s.create_uid, s.create_date, s.create_uid, s.create_date
FROM runbot_merge_stagings s,
json_each_text(s.heads::json) r
ON CONFLICT DO NOTHING;

CREATE TEMPORARY TABLE staging_commits (
id integer NOT NULL,
repo integer NOT NULL,
-- the staging head (may be a dedup, may be the same as commit)
head integer NOT NULL,
-- the staged commit
commit integer NOT NULL
);
-- the splatting works entirely off of the staged head
-- (the one without the ^ suffix), we concat the `^` to get the corresponding
-- merge head (the actual commit to push to the branch)
INSERT INTO staging_commits (id, repo, head, commit)
SELECT s.id, re.id AS repo, h.id AS head, c.id AS commit
FROM runbot_merge_stagings s,
json_each_text(s.heads::json) r,
runbot_merge_commit h,
runbot_merge_commit c,
runbot_merge_repository re
WHERE r.key NOT ILIKE '%^'
AND re.name = r.key
AND h.sha = r.value
AND c.sha = s.heads::json->>(r.key || '^');

INSERT INTO runbot_merge_stagings_heads (staging_id, repository_id, commit_id)
SELECT id, repo, head FROM staging_commits;

INSERT INTO runbot_merge_stagings_commits (staging_id, repository_id, commit_id)
SELECT id, repo, commit FROM staging_commits;

ALTER TABLE runbot_merge_stagings DROP COLUMN heads;
Loading

0 comments on commit b1af2e5

Please sign in to comment.