-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tighten up schema of the migrations table a little more (#443)
I'd totally forgotten I'd even created it this way, but going back through migrations recently I realized that the table's primary key is a `bigserial`. This isn't totally bad, and even has some advantages in that rows are easier to target from a psql session, but in this instance it feels a little on the sloppy side because it's fairly easy to conflate the serial with migration `version` -- both are expected to be small integers that a human could easily mix up. Since we haven't shipped a new version yet containing the addition of `line` in #435, here I propose that we modify that a little further to reshape `river_migration` into a slightly smaller table with a compound key on `(line, version)`: CREATE TABLE river_migration( created_at timestamptz NOT NULL DEFAULT NOW(), line TEXT NOT NULL, version bigint NOT NULL, CONSTRAINT line_length CHECK (char_length(line) > 0 AND char_length(line) < 128), CONSTRAINT version_gte_1 CHECK (version >= 1), PRIMARY KEY (line, version) ); This has two tiny side benefits that (1) the table is smaller (drops a column), and (2) we don't need an extra index on `(line, version)` since it's already covered by the primary key. Very similar in spirit to what it was before, but I think a little cleaner in design.
- Loading branch information
Showing
13 changed files
with
123 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
48 changes: 11 additions & 37 deletions
48
riverdriver/riverdatabasesql/internal/dbsqlc/river_migration.sql.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
38 changes: 34 additions & 4 deletions
38
riverdriver/riverdatabasesql/migration/main/005_river_migration_add_line.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,36 @@ | ||
DROP INDEX river_migration_line_version_idx; | ||
CREATE UNIQUE INDEX river_migration_version_idx ON river_migration USING btree(version); | ||
-- | ||
-- If any non-main migration are present, 005 is considered irreversible. | ||
-- | ||
|
||
DO | ||
$body$ | ||
BEGIN | ||
IF EXISTS ( | ||
SELECT * | ||
FROM river_migration | ||
WHERE line <> 'main' | ||
) THEN | ||
RAISE EXCEPTION 'Found non-main migration lines in the database; version 005 migration is irreversible because it would result in loss of migration information.'; | ||
END IF; | ||
END; | ||
$body$ | ||
LANGUAGE 'plpgsql'; | ||
|
||
ALTER TABLE river_migration | ||
DROP CONSTRAINT line_length, | ||
DROP COLUMN line; | ||
RENAME TO river_migration_old; | ||
|
||
CREATE TABLE river_migration( | ||
id bigserial PRIMARY KEY, | ||
created_at timestamptz NOT NULL DEFAULT NOW(), | ||
version bigint NOT NULL, | ||
CONSTRAINT version CHECK (version >= 1) | ||
); | ||
|
||
CREATE UNIQUE INDEX ON river_migration USING btree(version); | ||
|
||
INSERT INTO river_migration | ||
(created_at, version) | ||
SELECT created_at, version | ||
FROM river_migration_old; | ||
|
||
DROP TABLE river_migration_old; |
22 changes: 14 additions & 8 deletions
22
riverdriver/riverdatabasesql/migration/main/005_river_migration_add_line.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
ALTER TABLE river_migration | ||
ADD COLUMN line text; | ||
RENAME TO river_migration_old; | ||
|
||
UPDATE river_migration | ||
SET line = 'main'; | ||
CREATE TABLE river_migration( | ||
line TEXT NOT NULL, | ||
version bigint NOT NULL, | ||
created_at timestamptz NOT NULL DEFAULT NOW(), | ||
CONSTRAINT line_length CHECK (char_length(line) > 0 AND char_length(line) < 128), | ||
CONSTRAINT version_gte_1 CHECK (version >= 1), | ||
PRIMARY KEY (line, version) | ||
); | ||
|
||
ALTER TABLE river_migration | ||
ALTER COLUMN line SET NOT NULL, | ||
ADD CONSTRAINT line_length CHECK (char_length(line) > 0 AND char_length(line) < 128); | ||
INSERT INTO river_migration | ||
(created_at, line, version) | ||
SELECT created_at, 'main', version | ||
FROM river_migration_old; | ||
|
||
CREATE UNIQUE INDEX river_migration_line_version_idx ON river_migration USING btree(line, version); | ||
DROP INDEX river_migration_version_idx; | ||
DROP TABLE river_migration_old; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.