Skip to content

Commit

Permalink
fix(migration): don't recreate miner_sector_deals primary key if it i…
Browse files Browse the repository at this point in the history
…s correct (#300)
  • Loading branch information
iand authored Dec 4, 2020
1 parent 6e246cf commit b51c90f
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions storage/migrations/23_miner_sector_deals_pk.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,37 @@ import (

func init() {
up := batch(`
-- old index from when table was named miner_deal_sectors
ALTER TABLE public.miner_sector_deals DROP CONSTRAINT IF EXISTS miner_deal_sectors_pkey;
-- Only run this destructive migration if the constraint doesn't exist
DO $$
BEGIN
IF (
SELECT count(*)
FROM information_schema.constraint_column_usage
WHERE table_schema = 'public'
AND table_name = 'miner_sector_deals'
AND constraint_name='miner_sector_deals_pkey'
AND column_name IN ('height','miner_id','sector_id','deal_id')
) != 4
ALTER TABLE public.miner_sector_deals DROP CONSTRAINT IF EXISTS miner_sector_deals_pkey;
ALTER TABLE public.miner_sector_deals ADD PRIMARY KEY (height, miner_id, sector_id, deal_id);
THEN
-- Can't change primary key while data exists
TRUNCATE TABLE public.miner_sector_deals;
-- old index from when table was named miner_deal_sectors
ALTER TABLE public.miner_sector_deals DROP CONSTRAINT IF EXISTS miner_deal_sectors_pkey;
ALTER TABLE public.miner_sector_deals DROP CONSTRAINT IF EXISTS miner_sector_deals_pkey;
ALTER TABLE public.miner_sector_deals ADD PRIMARY KEY (height, miner_id, sector_id, deal_id);
END IF;
END
$$;
`)

down := batch(`
ALTER TABLE public.miner_sector_deals DROP CONSTRAINT IF EXISTS miner_sector_deals_pkey;
ALTER TABLE public.miner_sector_deals ADD PRIMARY KEY (height, miner_id, sector_id);
-- don't recreate the buggy constraint
SELECT 1;
`)

migrations.MustRegisterTx(up, down)
Expand Down

0 comments on commit b51c90f

Please sign in to comment.