ipld-eth-server/db/migrations/20180430230941_associate_receipts_with_blocks.sql
Rob Mulholand 925b1b0b1c Revert "Convert existing migrations from timestamp to versioned"
- This reverts commit 4e2536bf421fd9d2c9067d81f30e0f45c6b94fe7.
- Restore timestamped migrations so goose fix can handle ordering
  dynamically with plugins also supplying timestamp based migrations
2019-02-20 16:01:35 -06:00

45 lines
917 B
SQL

-- +goose Up
ALTER TABLE receipts
ADD COLUMN block_id INT;
UPDATE receipts
SET block_id = (
SELECT block_id FROM transactions WHERE transactions.id = receipts.transaction_id
);
ALTER TABLE receipts
ALTER COLUMN block_id SET NOT NULL;
ALTER TABLE receipts
ADD CONSTRAINT blocks_fk
FOREIGN KEY (block_id)
REFERENCES blocks (id)
ON DELETE CASCADE;
ALTER TABLE receipts
DROP COLUMN transaction_id;
-- +goose Down
ALTER TABLE receipts
ADD COLUMN transaction_id INT;
CREATE INDEX transaction_id_index ON receipts (transaction_id);
UPDATE receipts
SET transaction_id = (
SELECT id FROM transactions WHERE transactions.hash = receipts.tx_hash
);
ALTER TABLE receipts
ALTER COLUMN transaction_id SET NOT NULL;
ALTER TABLE receipts
ADD CONSTRAINT transaction_fk
FOREIGN KEY (transaction_id)
REFERENCES transactions (id)
ON DELETE CASCADE;
ALTER TABLE receipts
DROP COLUMN block_id;