Skip to content

Commit

Permalink
db: Fix uniqueness constraint on CollateralTxIn table
Browse files Browse the repository at this point in the history
The uniqueness constraints used to be on `txOutId` and `txOutIndex` but
that is not correct because collateral inputs that are not consumed can
be reused. Instead we put the uniqueness constraint on the `txInId`.

Closes: #664
  • Loading branch information
erikd committed Jun 23, 2021
1 parent 3bc3fc7 commit a4425d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cardano-db/src/Cardano/Db/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ share
txInId TxId OnDeleteCascade -- The transaction where this is used as an input.
txOutId TxId OnDeleteCascade -- The transaction where this was created as an output.
txOutIndex Word16 sqltype=txindex
UniqueColTxin txOutId txOutIndex
UniqueColTxin txInId

-- A table containing metadata about the chain. There will probably only ever be one
-- row in this table.
Expand Down
20 changes: 20 additions & 0 deletions schema/migration-2-0008-20210623.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Persistent generated migration.

CREATE FUNCTION migrate() RETURNS void AS $$
DECLARE
next_version int ;
BEGIN
SELECT stage_two + 1 INTO next_version FROM schema_version ;
IF next_version = 8 THEN
EXECUTE 'ALTER TABLE "collateral_tx_in" DROP CONSTRAINT "unique_col_txin"' ;
EXECUTE 'ALTER TABLE "collateral_tx_in" ADD CONSTRAINT "unique_col_txin" UNIQUE("tx_in_id")' ;
-- Hand written SQL statements can be added here.
UPDATE schema_version SET stage_two = next_version ;
RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ;
END IF ;
END ;
$$ LANGUAGE plpgsql ;

SELECT migrate() ;

DROP FUNCTION migrate() ;

0 comments on commit a4425d3

Please sign in to comment.