From 7fa921440a492764603c3c14985a44f4be344721 Mon Sep 17 00:00:00 2001 From: Oleksii Sholik Date: Tue, 31 Oct 2023 00:44:11 +0200 Subject: [PATCH] Prettify the log output of SQL routine definitions 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\"]}}"}} --- .../electric/lib/electric/postgres/extension.ex | 6 +++--- .../lib/electric/postgres/extension/functions.ex | 14 +++++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/components/electric/lib/electric/postgres/extension.ex b/components/electric/lib/electric/postgres/extension.ex index d5b92523ed..0049062ed6 100644 --- a/components/electric/lib/electric/postgres/extension.ex +++ b/components/electric/lib/electric/postgres/extension.ex @@ -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 diff --git a/components/electric/lib/electric/postgres/extension/functions.ex b/components/electric/lib/electric/postgres/extension/functions.ex index a667e208bf..ff68bad8ee 100644 --- a/components/electric/lib/electric/postgres/extension/functions.ex +++ b/components/electric/lib/electric/postgres/extension/functions.ex @@ -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 @@ -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 """ @@ -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