Current section

47 Versions

Jump to

Compare versions

6 files changed
+40 additions
-9 deletions
  @@ -7,7 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7 7
8 8 **IMPORTANT NOTE**: Make sure to see the [Upgrading Versions](guides/howtos/Upgrading Versions.md) guide in the [HexDocs documentation](https://hexdocs.pm/ecto_watch) if you're having an issue after upgrading.
9 9
10 - ## [0.12.6] - 2024-02-14
10 + ## [0.13.0] - 2024-04-04
11 +
12 + ### Added
13 +
14 + - Added support for `source` option in ecto schema fields (thanks @danwanek-wm)
15 + - Added `column_map` option for schemaless watchers (thanks @danwanek-wm)
16 + - `PGUSER`, `PGPASSWORD`, `PGHOST`, and `PGDATABASE` environment variables for testing of library (thanks @danwanek-wm)
17 +
18 + ## [0.12.6] - 2024-04-01
11 19
12 20 ### Added
  @@ -3,7 +3,7 @@
3 3 <<"https://github.com/cheerfulstoic/ecto_watch/blob/main/CHANGELOG.md">>},
4 4 {<<"GitHub">>,<<"https://github.com/cheerfulstoic/ecto_watch">>}]}.
5 5 {<<"name">>,<<"ecto_watch">>}.
6 - {<<"version">>,<<"0.12.6">>}.
6 + {<<"version">>,<<"0.13.0">>}.
7 7 {<<"description">>,
8 8 <<"EctoWatch allows you to easily get Phoenix.PubSub notifications directly from postgresql.">>}.
9 9 {<<"elixir">>,<<"~> 1.10">>}.
  @@ -16,6 +16,7 @@ defmodule EctoWatch.Options.WatcherOptions do
16 16 :schema_prefix,
17 17 :table_name,
18 18 :primary_key,
19 + :column_map,
19 20 :columns,
20 21 :association_columns,
21 22 :label
  @@ -32,6 +33,7 @@ defmodule EctoWatch.Options.WatcherOptions do
32 33 [primary_key] = schema_mod.__schema__(:primary_key)
33 34
34 35 fields = schema_mod.__schema__(:fields)
36 + column_map = Map.new(fields, &{&1, schema_mod.__schema__(:field_source, &1)})
35 37
36 38 association_columns =
37 39 schema_mod.__schema__(:associations)
  @@ -42,6 +44,7 @@ defmodule EctoWatch.Options.WatcherOptions do
42 44 schema_prefix: schema_prefix,
43 45 table_name: table_name,
44 46 primary_key: primary_key,
47 + column_map: column_map,
45 48 columns: fields,
46 49 association_columns: association_columns,
47 50 label: schema_mod
  @@ -59,6 +62,7 @@ defmodule EctoWatch.Options.WatcherOptions do
59 62 schema_prefix: to_string(schema_prefix),
60 63 table_name: to_string(opts.table_name),
61 64 primary_key: opts.primary_key,
65 + column_map: opts[:column_map] || %{},
62 66 columns: opts.columns,
63 67 association_columns: opts[:association_columns] || [],
64 68 label: "#{schema_prefix}|#{opts.table_name}"
  @@ -116,6 +120,11 @@ defmodule EctoWatch.Options.WatcherOptions do
116 120 type: {:list, :atom},
117 121 required: false,
118 122 default: []
123 + ],
124 + column_map: [
125 + type: {:map, :atom, :atom},
126 + required: false,
127 + default: %{}
119 128 ]
120 129 ]
  @@ -56,7 +56,12 @@ defmodule EctoWatch.WatcherServer do
56 56
57 57 :updated ->
58 58 if options.trigger_columns && options.trigger_columns != [] do
59 - "UPDATE OF #{Enum.join(options.trigger_columns, ", ")}"
59 + # Get the actual column names from the schema definition and make
60 + # sure they are quoted in case of special characters
61 + options.trigger_columns
62 + |> Enum.map(&source_column(options.schema_definition, &1))
63 + |> Enum.join(", ")
64 + |> then(&"UPDATE OF #{&1}")
60 65 else
61 66 "UPDATE"
62 67 end
  @@ -67,7 +72,10 @@ defmodule EctoWatch.WatcherServer do
67 72
68 73 columns_sql =
69 74 [options.schema_definition.primary_key | options.extra_columns]
70 - |> Enum.map_join(",", &"'#{&1}',row.#{&1}")
75 + |> Enum.map_join(
76 + ",",
77 + &"'#{&1}',row.#{source_column(options.schema_definition, &1)}"
78 + )
71 79
72 80 details =
73 81 watcher_details(%{unique_label: unique_label, repo_mod: repo_mod, options: options})
  @@ -131,6 +139,11 @@ defmodule EctoWatch.WatcherServer do
131 139 }}
132 140 end
133 141
142 + defp source_column(schema_definition, column) do
143 + Map.get(schema_definition.column_map, column, column)
144 + |> then(&"\"#{&1}\"")
145 + end
146 +
134 147 @impl true
135 148 def handle_call(
136 149 {:pub_sub_subscription_details, identifier, identifier_value},
  @@ -9,10 +9,11 @@ defmodule EctoWatch.TestRepo do
9 9 {:ok,
10 10 Keyword.merge(
11 11 config,
12 - username: "postgres",
13 - password: "postgres",
14 - hostname: "localhost",
15 - database: "ecto_watch",
12 + username: System.get_env("PGUSER", "postgres"),
13 + password: System.get_env("PGPASSWORD", "postgres"),
14 + hostname: System.get_env("PGHOST", "localhost"),
15 + database: System.get_env("PGDATABASE", "ecto_watch"),
16 + port: System.get_env("PGPORT", "5432"),
16 17 stacktrace: true,
17 18 show_sensitive_data_on_connection_error: true,
18 19 pool_size: 10
Loading more files…