-
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.
chore(electric): redefine column checks in
electrify
on every start…
- Loading branch information
Showing
8 changed files
with
99 additions
and
59 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 | ||
--- | ||
|
||
Rewrite the type-validating part of `electify` function to expand allowed types when Electric instance is upgraded |
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
26 changes: 26 additions & 0 deletions
26
components/electric/lib/electric/postgres/extension/functions.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,26 @@ | ||
defmodule Electric.Postgres.Extension.Functions do | ||
require EEx | ||
|
||
sql_files = | ||
"functions/*.sql.eex" | ||
|> Path.expand(__DIR__) | ||
|> Path.wildcard() | ||
|
||
for path <- sql_files, do: @external_resource(path) | ||
|
||
@function_defs Map.new(sql_files, fn path -> | ||
{Path.basename(path, ".sql.eex"), {Path.basename(path), File.read!(path)}} | ||
end) | ||
|
||
@doc """ | ||
Get a list of SQL statements that create various internal SQL functions in the `electric` schema. | ||
Every function in the list is defined as `CREATE OR REPLACE FUNCTION`. | ||
""" | ||
def list do | ||
for {name, args} <- [{"validate_table_column_types", []}] do | ||
{filename, sql} = @function_defs[name] | ||
{name, EEx.eval_string(sql, args, file: filename)} | ||
end | ||
end | ||
end |
47 changes: 47 additions & 0 deletions
47
...ts/electric/lib/electric/postgres/extension/functions/validate_table_column_types.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,47 @@ | ||
<% | ||
valid_column_types = | ||
~w[ | ||
bool | ||
date | ||
float8 | ||
int2 int4 | ||
text | ||
time | ||
timestamp timestamptz | ||
uuid | ||
varchar | ||
]a | ||
|> Enum.map(&"'#{&1}'") | ||
|> Enum.join(",") | ||
%> | ||
|
||
CREATE OR REPLACE FUNCTION electric.__validate_table_column_types(table_name text) | ||
RETURNS VOID AS $function$ | ||
DECLARE | ||
_col_name text; | ||
_col_type text; | ||
_col_typmod int; | ||
_col_type_pretty text; | ||
_invalid_cols text[]; | ||
BEGIN | ||
FOR _col_name, _col_type, _col_typmod, _col_type_pretty IN | ||
SELECT attname, typname, atttypmod, format_type(atttypid, atttypmod) | ||
FROM pg_attribute | ||
JOIN pg_type on atttypid = pg_type.oid | ||
WHERE attrelid = table_name::regclass AND attnum > 0 AND NOT attisdropped | ||
ORDER BY attnum | ||
LOOP | ||
IF _col_type NOT IN (<%= valid_column_types %>) | ||
-- We only support unsized varchar type | ||
OR ('varchar' IN (<%= valid_column_types %>) AND _col_type = 'varchar' AND _col_typmod <> -1) | ||
THEN | ||
_invalid_cols = array_append(_invalid_cols, format('"%s" %s', _col_name, _col_type_pretty)); | ||
END IF; | ||
END LOOP; | ||
|
||
IF _invalid_cols IS NOT NULL THEN | ||
RAISE EXCEPTION E'Cannot electrify "%" because some of its columns have types not 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
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