Skip to content

Commit

Permalink
Prettify the log output of SQL routine definitions
Browse files Browse the repository at this point in the history
Log sample:

    00:43:08.719 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'capture_ddl.sql.eex'
    00:43:08.720 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'current_transaction_id.sql.eex'
    00:43:08.720 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'current_xact_id.sql.eex'
    00:43:08.721 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'current_xact_ts.sql.eex'
    00:43:08.723 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'ddlx/assign.sql.eex'
    00:43:08.724 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'ddlx/disable.sql.eex'
    00:43:08.725 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'ddlx/enable.sql.eex'
    00:43:08.725 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'ddlx/grant.sql.eex'
    00:43:08.726 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'ddlx/unassign.sql.eex'
    00:43:08.727 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'electrify.sql.eex'
    00:43:08.728 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'electrify/__validate_table_column_defaults.sql.eex'
    00:43:08.728 pid=<0.1285.0> origin=postgres_1 [debug] Successfully (re)defined SQL routine from 'electrify/__validate_table_column_types.sql.eex'
    00:45:22.624 pid=<0.1285.0> origin=postgres_1 [error] initialization for postgresql failed with reason: {:rollback, %RuntimeError{message: "Failed to define SQL routine from 'electrify.sql.eex' with error: {:error, {:error, :error, \"42601\", :syntax_error, \"syntax error at or near \\\"SELECD\\\"\", [file: \"scan.l\", line: \"1176\", position: \"243\", routine: \"scanner_yyerror\", severity: \"ERROR\"]}}"}}
  • Loading branch information
alco committed Oct 30, 2023
1 parent 74a5da9 commit 7fa9214
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions components/electric/lib/electric/postgres/extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,14 @@ defmodule Electric.Postgres.Extension do

@spec define_functions(conn) :: :ok
def define_functions(conn) do
Enum.each(Functions.list(), fn {name, sql} ->
Enum.each(Functions.list(), fn {path, sql} ->
conn
|> :epgsql.squery(sql)
|> List.wrap()
|> Enum.find(&(not match?({:ok, [], []}, &1)))
|> case do
nil -> Logger.debug("Successfully (re)defined SQL function/procedure '#{name}'")
error -> raise "Failed to define function '#{name}' with error: #{inspect(error)}"
nil -> Logger.debug("Successfully (re)defined SQL routine from '#{path}'")
error -> raise "Failed to define SQL routine from '#{path}' with error: #{inspect(error)}"
end
end)
end
Expand Down
14 changes: 9 additions & 5 deletions components/electric/lib/electric/postgres/extension/functions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ defmodule Electric.Postgres.Extension.Functions do
|> Path.expand(__DIR__)
|> Path.wildcard()

function_names =
function_paths =
for path <- sql_files do
@external_resource path

fpath = Path.relative_to(path, Path.expand("./functions", __DIR__))
name = path |> Path.basename(".sql.eex") |> String.to_atom()
_ = EEx.function_from_file(:def, name, path, [])

name
{fpath, name}
end

function_names = for {_fpath, name} <- function_paths, do: name

fn_name_type =
Enum.reduce(function_names, fn name, code ->
quote do
Expand All @@ -31,8 +34,9 @@ defmodule Electric.Postgres.Extension.Functions do

@typep name :: unquote(fn_name_type)
@typep sql :: String.t()
@type function_list :: [{name, sql}]
@type function_list :: [{binary, sql}]

@function_paths function_paths
@function_names function_names

@doc """
Expand All @@ -44,8 +48,8 @@ defmodule Electric.Postgres.Extension.Functions do
# here. See VAX-1016 for details.
@spec list :: function_list
def list do
for name <- @function_names do
{name, by_name(name)}
for {path, name} <- @function_paths do
{path, by_name(name)}
end
end

Expand Down

0 comments on commit 7fa9214

Please sign in to comment.