-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(electric): Reject tables that have columns with DEFAULT express…
- Loading branch information
Showing
20 changed files
with
157 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@core/electric": patch | ||
--- | ||
|
||
Reject tables that have columns with DEFAULT expressions. |
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
27 changes: 27 additions & 0 deletions
27
...electric/lib/electric/postgres/extension/functions/validate_table_column_defaults.sql.eex
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,27 @@ | ||
-- This function validates each column of the table that's being electrified | ||
-- and aborts electrification if any column has DEFAULT expression. | ||
|
||
CREATE OR REPLACE PROCEDURE <%= @schema %>.__validate_table_column_defaults(table_name text) | ||
SECURITY DEFINER AS $function$ | ||
DECLARE | ||
_col_name text; | ||
_col_has_default boolean; | ||
_invalid_cols text[]; | ||
BEGIN | ||
FOR _col_name, _col_has_default IN | ||
SELECT attname, atthasdef | ||
FROM pg_attribute | ||
WHERE attrelid = table_name::regclass AND attnum > 0 AND NOT attisdropped | ||
ORDER BY attnum | ||
LOOP | ||
IF _col_has_default THEN | ||
_invalid_cols = array_append(_invalid_cols, format('%I', _col_name)); | ||
END IF; | ||
END LOOP; | ||
|
||
IF _invalid_cols IS NOT NULL THEN | ||
RAISE EXCEPTION E'Cannot electrify "%" because some of its columns have DEFAULT expression which is not currently supported by Electric:\n %', | ||
table_name, array_to_string(_invalid_cols, E'\n '); | ||
END IF; | ||
END; | ||
$function$ LANGUAGE PLPGSQL; |
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
18 changes: 18 additions & 0 deletions
18
...ib/electric/postgres/extension/migrations/20231016141000_convert_function_to_procedure.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,18 @@ | ||
defmodule Electric.Postgres.Extension.Migrations.Migration_20231016141000_ConvertFunctionToProcedure do | ||
alias Electric.Postgres.Extension | ||
|
||
@behaviour Extension.Migration | ||
|
||
@impl true | ||
def version, do: 2023_10_16_14_10_00 | ||
|
||
@impl true | ||
def up(schema) do | ||
["DROP ROUTINE IF EXISTS #{schema}.__validate_table_column_types(text)"] | ||
end | ||
|
||
@impl true | ||
def down(_schema) do | ||
[] | ||
end | ||
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
CREATE TABLE entries ( | ||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
id UUID PRIMARY KEY, | ||
content VARCHAR NOT NULL, | ||
content_b TEXT | ||
); | ||
|
||
CREATE TABLE owned_entries ( | ||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
id UUID PRIMARY KEY, | ||
electric_user_id TEXT NOT NULL, | ||
content VARCHAR NOT NULL | ||
); |
Oops, something went wrong.