Changeset View
Changeset View
Standalone View
Standalone View
swh/provenance/postgresql/archive.py
Show First 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | def revision_get_parents(self, id: Sha1Git) -> Iterable[Sha1Git]: | ||||
# There should be at most one row anyway | # There should be at most one row anyway | ||||
for row in cursor.fetchall(): | for row in cursor.fetchall(): | ||||
yield from row[0] | yield from row[0] | ||||
def snapshot_get_heads(self, id: Sha1Git) -> Iterable[Sha1Git]: | def snapshot_get_heads(self, id: Sha1Git) -> Iterable[Sha1Git]: | ||||
with self.conn.cursor() as cursor: | with self.conn.cursor() as cursor: | ||||
cursor.execute( | cursor.execute( | ||||
""" | """ | ||||
WITH S AS (SELECT object_id FROM snapshot WHERE snapshot.id=%s) | WITH | ||||
(SELECT B.target AS head | snaps AS (SELECT object_id FROM snapshot WHERE snapshot.id=%s), | ||||
FROM S | heads AS ((SELECT R.id, R.date | ||||
JOIN snapshot_branches AS BS ON (S.object_id=BS.snapshot_id) | FROM snaps | ||||
JOIN snapshot_branch AS B ON (BS.branch_id=B.object_id) | JOIN snapshot_branches AS BS | ||||
ON (snaps.object_id=BS.snapshot_id) | |||||
JOIN snapshot_branch AS B | |||||
ON (BS.branch_id=B.object_id) | |||||
JOIN revision AS R | |||||
ON (B.target=R.id) | |||||
WHERE B.target_type='revision'::snapshot_target) | WHERE B.target_type='revision'::snapshot_target) | ||||
UNION | UNION | ||||
(SELECT R.target AS head | (SELECT RV.id, RV.date | ||||
FROM S | FROM snaps | ||||
JOIN snapshot_branches AS BS ON (S.object_id=BS.snapshot_id) | JOIN snapshot_branches AS BS | ||||
JOIN snapshot_branch AS B ON (BS.branch_id=B.object_id) | ON (snaps.object_id=BS.snapshot_id) | ||||
JOIN release AS R ON (B.target=R.id) | JOIN snapshot_branch AS B | ||||
ON (BS.branch_id=B.object_id) | |||||
JOIN release AS RL | |||||
ON (B.target=RL.id) | |||||
JOIN revision AS RV | |||||
ON (RL.target=RV.id) | |||||
WHERE B.target_type='release'::snapshot_target | WHERE B.target_type='release'::snapshot_target | ||||
AND R.target_type='revision'::object_type) | AND RL.target_type='revision'::object_type) | ||||
ORDER BY date, id) | |||||
SELECT id FROM heads | |||||
""", | """, | ||||
(id,), | (id,), | ||||
) | ) | ||||
heads = [row[0] for row in cursor.fetchall()] | yield from (row[0] for row in cursor.fetchall()) | ||||
douardda: (a bit nitpick but) since you do not need the dates to be returned, the SQL query should not… | |||||
print([head.hex() for head in heads]) | |||||
yield from heads | |||||
# for row in cursor.fetchall(): | |||||
# yield row[0] |
(a bit nitpick but) since you do not need the dates to be returned, the SQL query should not select them.