-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(electric): Prevent creation of duplicate DDL commands in migratio…
…n history (#439)
- Loading branch information
1 parent
1dd9500
commit a4c5ce6
Showing
4 changed files
with
77 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@core/electric": patch | ||
--- | ||
|
||
VAX-1036 - fixes bugs reported by @hugodutka by preventing insertion of duplicate ddl commands in migration history |
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
50 changes: 50 additions & 0 deletions
50
...ectric/postgres/extension/migrations/20230918115714_add_unique_constraint_ddl_commands.ex
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule Electric.Postgres.Extension.Migrations.Migration_20230918115714_DDLCommandUniqueConstraint do | ||
alias Electric.Postgres.Extension | ||
|
||
@behaviour Extension.Migration | ||
|
||
@txid_type "xid8" | ||
|
||
@impl true | ||
def version, do: 2023_09_18_11_57_14 | ||
|
||
@impl true | ||
def up(schema) do | ||
ddl_table = Extension.ddl_table() | ||
|
||
[ | ||
""" | ||
ALTER TABLE #{ddl_table} | ||
ADD CONSTRAINT ddl_table_unique_migrations | ||
UNIQUE (txid, txts ,version, query); | ||
""", | ||
""" | ||
CREATE OR REPLACE FUNCTION #{schema}.create_active_migration( | ||
_txid #{@txid_type}, | ||
_txts timestamptz, | ||
_version text, | ||
_query text DEFAULT NULL | ||
) RETURNS int8 AS | ||
$function$ | ||
DECLARE | ||
trid int8; | ||
BEGIN | ||
IF _query IS NULL THEN | ||
_query := current_query(); | ||
END IF; | ||
RAISE NOTICE 'capture migration: % => %', _version, _query; | ||
INSERT INTO #{ddl_table} (txid, txts, version, query) VALUES | ||
(_txid, _txts, _version, _query) | ||
ON CONFLICT ON CONSTRAINT ddl_table_unique_migrations DO NOTHING | ||
RETURNING id INTO trid; | ||
RETURN trid; | ||
END; | ||
$function$ | ||
LANGUAGE PLPGSQL; | ||
""" | ||
] | ||
end | ||
|
||
@impl true | ||
def down(_), do: [] | ||
end |
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