Skip to content

Commit 86ba333

Browse files
committed
Remove version_downloads.id
This column is no longer being used, and can be safely removed. Rolling back this migration in production requires creating indexes concurrently, which Diesel CLI doesn't support, so rolling back this migration in production will require queries to be run manually. A blocking version is included for development and testing
1 parent 3c92ca2 commit 86ba333

File tree

2 files changed

+34
-0
lines changed
  • migrations/2019-03-22-174825_remove_version_downloads_pk

2 files changed

+34
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- Diesel always runs migrations in a transaction, and we can't create indexes
2+
-- concurrently in a transaction. We can't block crate downloads on this, so
3+
-- just raise if we try to run this in production
4+
DO $$
5+
DECLARE
6+
tmp_index_exists boolean;
7+
BEGIN
8+
tmp_index_exists := (SELECT EXISTS (
9+
SELECT * FROM pg_index
10+
INNER JOIN pg_class
11+
ON pg_class.oid = pg_index.indexrelid
12+
WHERE relname = 'version_downloads_tmp'
13+
));
14+
IF NOT tmp_index_exists THEN
15+
IF (SELECT COUNT(*) FROM version_downloads) > 1000 THEN
16+
RAISE EXCEPTION 'Indexes need to be created concurrently in production, manually create them and try again';
17+
ELSE
18+
ALTER TABLE version_downloads ADD COLUMN id SERIAL;
19+
CREATE UNIQUE INDEX version_downloads_tmp ON version_downloads (id);
20+
CREATE UNIQUE INDEX version_downloads_unique ON version_downloads (version_id, date);
21+
CREATE UNIQUE INDEX index_version_downloads_not_processed ON version_downloads (id) WHERE processed = false;
22+
END IF;
23+
END IF;
24+
END $$;
25+
26+
ALTER TABLE version_downloads
27+
DROP CONSTRAINT version_downloads_pkey,
28+
ADD CONSTRAINT version_downloads_pkey PRIMARY KEY USING INDEX version_downloads_tmp;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- ALERT! DESTRUCTIVE MIGRATION
2+
-- DO NOT DEPLOY UNTIL PREVIOUS COMMIT IS DEPLOYED
3+
ALTER TABLE version_downloads
4+
DROP CONSTRAINT version_downloads_pkey,
5+
ADD CONSTRAINT version_downloads_pkey PRIMARY KEY USING INDEX version_downloads_unique,
6+
DROP COLUMN id;

0 commit comments

Comments
 (0)