diff --git a/sql/upgrades/131.sql b/sql/upgrades/131.sql new file mode 100644 --- /dev/null +++ b/sql/upgrades/131.sql @@ -0,0 +1,12 @@ +-- SWH DB schema upgrade +-- from_version: 130 +-- to_version: 131 +-- description: Use sha1 instead of bigint as FK from origin_visit to snapshot (part 1: add new column) + +insert into dbversion(version, release, description) + values(131, now(), 'Work In Progress'); + +alter table origin_visit add column snapshot sha1_git; +comment on column origin_visit.snapshot is 'Origin snapshot at visit time'; + +alter table origin_visit add constraint origin_visit_snapshot_fkey foreign key (snapshot) references snapshot(id) not valid; diff --git a/swh/storage/db.py b/swh/storage/db.py --- a/swh/storage/db.py +++ b/swh/storage/db.py @@ -311,6 +311,11 @@ update_cols.append('metadata=%s') values.append(jsonize(updates.pop('metadata'))) if 'snapshot' in updates: + # New 'snapshot' column + update_cols.append('snapshot=%s') + values.append(updates['snapshot']) + + # Old 'snapshot_id' column update_cols.append('snapshot_id=snapshot.object_id') from_ = 'FROM snapshot' where.append('snapshot.id=%s') diff --git a/swh/storage/sql/30-swh-schema.sql b/swh/storage/sql/30-swh-schema.sql --- a/swh/storage/sql/30-swh-schema.sql +++ b/swh/storage/sql/30-swh-schema.sql @@ -211,7 +211,8 @@ date timestamptz not null, status origin_visit_status not null, metadata jsonb, - snapshot_id bigint + snapshot_id bigint, + snapshot sha1_git ); comment on column origin_visit.origin is 'Visited origin'; @@ -220,6 +221,7 @@ comment on column origin_visit.status is 'Visit result'; comment on column origin_visit.metadata is 'Origin metadata at visit time'; comment on column origin_visit.snapshot_id is 'Origin snapshot at visit time'; +comment on column origin_visit.snapshot is 'Origin snapshot at visit time'; -- A snapshot represents the entire state of a software origin as crawled by diff --git a/swh/storage/sql/60-swh-indexes.sql b/swh/storage/sql/60-swh-indexes.sql --- a/swh/storage/sql/60-swh-indexes.sql +++ b/swh/storage/sql/60-swh-indexes.sql @@ -138,6 +138,9 @@ alter table origin_visit add constraint origin_visit_snapshot_id_fkey foreign key (snapshot_id) references snapshot(object_id) not valid; alter table origin_visit validate constraint origin_visit_snapshot_id_fkey; +alter table origin_visit add constraint origin_visit_snapshot_fkey foreign key (snapshot) references snapshot(id) not valid; +-- NOT validated + -- release create unique index concurrently release_pkey on release(id); alter table release add primary key using index release_pkey;