Content-Length: 966087 | pFad | http://github.com/electric-sql/phoenix_sync/commit/8d699813b24afc6cd3e0929ac5804b74d346e7e9

27 fix client configuration problem · electric-sql/phoenix_sync@8d69981 · GitHub
Skip to content

Commit 8d69981

Browse files
committed
fix client configuration problem
1 parent ccb9504 commit 8d69981

File tree

6 files changed

+62
-39
lines changed

6 files changed

+62
-39
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<a href="https://hexdocs.pm/phoenix_sync" target="_blank">
44
<picture>
55
<img alt="Phoenix sync illustration"
6-
src="./docs/phoenix-sync.png"
6+
src="https://github.com/electric-sql/phoenix_sync/raw/main/docs/phoenix-sync.png"
77
/>
88
</picture>
99
</a>
@@ -196,7 +196,7 @@ The available options are:
196196

197197
- `table` (required). The Postgres table name. Be aware of casing and [Postgres's handling of unquoted upper-case names](https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_upper_case_table_or_column_names).
198198
- `namespace` (optional). The Postgres namespace that the table belongs to. Defaults to `public`.
199-
- `where` (optional). Filter to apply to the synced data in SQL format, e.g. `where: "amount < 1.23 AND colour in ('red', 'green')`.
199+
- `where` (optional). Filter to apply to the synced data in SQL syntax, e.g. `where: "amount < 1.23 AND colour in ('red', 'green')`.
200200
- `columns` (optional). The columns to include in the synced data. By default Electric will include all columns in the table. The column list **must** include all primary keys. E.g. `columns: ["id", "title", "amount"]`.
201201
- `replica` (optional). By default Electric will only send primary keys + changed columns on updates. Set `replica: :full` to receive the full row, not just the changed columns.
202202

@@ -225,6 +225,7 @@ end
225225
# config/config.exs
226226
config :phoenix_sync,
227227
mode: :embedded,
228+
env: config_env(),
228229
repo: MyApp.Repo
229230

230231
# application.ex
@@ -250,6 +251,7 @@ end
250251
# config/config.exs
251252
config :phoenix_sync,
252253
mode: :http,
254+
env: config_env(),
253255
url: "https://api.electric-sql.cloud",
254256
credentials: [
255257
secret: "...", # required
@@ -280,6 +282,7 @@ end
280282
# config/config.exs
281283
config :phoenix_sync,
282284
mode: :http,
285+
env: config_env(),
283286
http: [
284287
port: 3000,
285288
],
@@ -318,11 +321,13 @@ end
318321
# config/dev.exs
319322
config :phoenix_sync,
320323
mode: :embedded,
324+
env: config_env(),
321325
repo: MyApp.Repo
322326

323327
# config/test.esx
324328
config :phoenix_sync,
325329
mode: :http,
330+
env: config_env(),
326331
http: [
327332
port: 3000,
328333
],

lib/phoenix/sync/adapter.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ defmodule Phoenix.Sync.Adapter do
33

44
@callback children(atom(), keyword()) :: {:ok, [Supervisor.child_spec()]} | {:error, String.t()}
55
@callback plug_opts(atom(), keyword()) :: keyword() | no_return()
6-
@callback client(keyword()) :: {:ok, struct()} | {:error, String.t()}
6+
@callback client(atom(), keyword()) :: {:ok, struct()} | {:error, String.t()}
77
end

lib/phoenix/sync/application.ex

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ defmodule Phoenix.Sync.Application do
33

44
require Logger
55

6-
@env Mix.env()
76
@default_adapter Phoenix.Sync.Electric
87

98
@impl true
109
def start(_type, _args) do
11-
case children() do
10+
case children() |> dbg do
1211
{:ok, children} ->
1312
Supervisor.start_link(children, strategy: :one_for_one, name: Phoenix.Sync.Supervisor)
1413

@@ -40,14 +39,17 @@ defmodule Phoenix.Sync.Application do
4039

4140
@doc false
4241
def children(opts) when is_list(opts) do
43-
children(@env, opts)
42+
{adapter, env} = adapter_env(opts)
43+
44+
apply(adapter, :children, [env, opts])
4445
end
4546

4647
@doc false
47-
def children(env, opts) do
48-
adapter = adapter(opts)
49-
50-
apply(adapter, :children, [env, opts])
48+
def adapter_env(opts) do
49+
{
50+
adapter(),
51+
Keyword.get(opts, :env, :prod)
52+
}
5153
end
5254

5355
@doc """
@@ -102,14 +104,9 @@ defmodule Phoenix.Sync.Application do
102104

103105
@doc false
104106
def plug_opts(opts) when is_list(opts) do
105-
plug_opts(@env, opts)
106-
end
107-
108-
@doc false
109-
def plug_opts(env, opts) do
110-
adapter = adapter(opts)
107+
{adapter, env} = adapter_env(opts)
111108

112-
apply(adapter, :plug_opts, [env, opts])
109+
apply(adapter, :plug_opts, [env, opts]) |> dbg
113110
end
114111

115112
@doc false

lib/phoenix/sync/client.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ defmodule Phoenix.Sync.Client do
3535
Alternatively use `stream/1` which wraps this functionality.
3636
"""
3737
def new(opts) do
38-
adapter = Phoenix.Sync.Application.adapter(opts)
38+
{adapter, env} = Phoenix.Sync.Application.adapter_env(opts)
3939

40-
apply(adapter, :client, [opts])
40+
apply(adapter, :client, [env, opts])
4141
end
4242

4343
def new! do

lib/phoenix/sync/electric.ex

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ defmodule Phoenix.Sync.Electric do
178178

179179
@doc false
180180
@impl Phoenix.Sync.Adapter
181-
def client(opts) do
181+
def client(env, opts) do
182182
{mode, electric_opts} = electric_opts(opts)
183183

184184
case mode do
185185
mode when mode in @client_valid_modes ->
186-
electric_opts
187-
|> stack_id()
186+
env
187+
|> core_configuration(electric_opts)
188188
|> configure_client(mode)
189189

190190
invalid_mode ->
@@ -304,10 +304,20 @@ defmodule Phoenix.Sync.Electric do
304304
end
305305

306306
defp env_defaults(opts, :dev) do
307+
# can't use new tmp dir for every run in dev because the storage
308+
# path must remain consistent between invocations of children(), plug_opts()
309+
# and client()...
310+
# if we want to use emphemeral dir for dev storage then we have to persist
311+
# the storage_dir into the application config.
307312
opts
313+
# |> Keyword.put_new(
314+
# :storage_dir,
315+
# Path.join(System.tmp_dir!(), "electric/shape-data#{System.monotonic_time()}")
316+
# )
308317
|> Keyword.put_new(
309-
:storage_dir,
310-
Path.join(System.tmp_dir!(), "electric/shape-data#{System.monotonic_time()}")
318+
:storage,
319+
{Electric.ShapeCache.InMemoryStorage,
320+
table_base_name: :"electric-storage#{opts[:stack_id]}", stack_id: opts[:stack_id]}
311321
)
312322
|> Keyword.put_new(:send_cache_headers?, false)
313323
end

test/phoenix/sync/application_test.exs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ defmodule Phoenix.Sync.ApplicationTest do
2222

2323
describe "children/1" do
2424
test "invalid mode" do
25-
assert {:error, _} = App.children(:prod, mode: :nonsense)
26-
assert {:error, _} = App.children(:prod, [])
25+
assert {:error, _} = App.children(mode: :nonsense)
26+
assert {:error, _} = App.children([])
2727
end
2828

2929
test "embedded mode" do
3030
config = [
3131
mode: :embedded,
32+
env: :prod,
3233
repo: Support.ConfigTestRepo,
3334
storage_dir: "/something"
3435
]
3536

36-
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(:prod, config)
37+
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(config)
3738

3839
validate_repo_connection_opts!(opts)
3940

@@ -44,20 +45,21 @@ defmodule Phoenix.Sync.ApplicationTest do
4445
end
4546

4647
test "no configuration set" do
47-
assert {:error, _} = App.children(:dev, [])
48+
assert {:error, _} = App.children([])
4849
end
4950

5051
test "disabled mode" do
51-
assert {:ok, []} = App.children(:dev, mode: :disabled)
52+
assert {:ok, []} = App.children(mode: :disabled)
5253
end
5354

5455
test "embedded mode dev env" do
5556
config = [
5657
mode: :embedded,
58+
env: :dev,
5759
repo: Support.ConfigTestRepo
5860
]
5961

60-
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(:dev, config)
62+
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(config)
6163

6264
validate_repo_connection_opts!(opts)
6365

@@ -71,22 +73,24 @@ defmodule Phoenix.Sync.ApplicationTest do
7173

7274
test "only repo config given and electric installed defaults to embedded" do
7375
config = [
76+
env: :dev,
7477
repo: Support.ConfigTestRepo
7578
]
7679

77-
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(:dev, config)
80+
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(config)
7881
validate_repo_connection_opts!(opts)
7982
end
8083

8184
test "embedded mode dev env doesn't overwrite explicit storage_dir" do
8285
config = [
8386
mode: :embedded,
87+
env: :dev,
8488
repo: Support.ConfigTestRepo,
8589
# don't overwrite this explict config
8690
storage_dir: "/something"
8791
]
8892

89-
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(:dev, config)
93+
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(config)
9094

9195
validate_repo_connection_opts!(opts)
9296

@@ -99,11 +103,12 @@ defmodule Phoenix.Sync.ApplicationTest do
99103
test "embedded mode test env" do
100104
config = [
101105
mode: :embedded,
106+
env: :test,
102107
repo: Support.ConfigTestRepo,
103108
storage_dir: "/something"
104109
]
105110

106-
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(:test, config)
111+
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(config)
107112

108113
validate_repo_connection_opts!(opts)
109114

@@ -116,6 +121,7 @@ defmodule Phoenix.Sync.ApplicationTest do
116121
test "embedded mode with explict connection_opts" do
117122
config = [
118123
mode: :embedded,
124+
env: :prod,
119125
connection_opts: [
120126
username: "postgres",
121127
hostname: "localhost",
@@ -125,7 +131,7 @@ defmodule Phoenix.Sync.ApplicationTest do
125131
storage_dir: "/something"
126132
]
127133

128-
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(:prod, config)
134+
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(config)
129135

130136
assert {pass_fun, connection_opts} = Keyword.pop!(opts[:connection_opts], :password)
131137
assert pass_fun.() == "password"
@@ -145,19 +151,21 @@ defmodule Phoenix.Sync.ApplicationTest do
145151
test "remote http mode" do
146152
config = [
147153
mode: :http,
154+
env: :prod,
148155
url: "https://api.electric-sql.cloud",
149156
credentials: [
150157
secret: "my-secret",
151158
source_id: "my-source-id"
152159
]
153160
]
154161

155-
assert {:ok, []} = App.children(:prod, config)
162+
assert {:ok, []} = App.children(config)
156163
end
157164

158165
test "embedded http mode" do
159166
config = [
160167
mode: :http,
168+
env: :prod,
161169
repo: Support.ConfigTestRepo,
162170
url: "http://localhost:4001",
163171
http: [
@@ -167,7 +175,7 @@ defmodule Phoenix.Sync.ApplicationTest do
167175
]
168176

169177
assert {:ok, [{Electric.StackSupervisor, opts}, {Bandit, http_opts}]} =
170-
App.children(:prod, config)
178+
App.children(config)
171179

172180
validate_repo_connection_opts!(opts)
173181

@@ -180,11 +188,12 @@ defmodule Phoenix.Sync.ApplicationTest do
180188
test "embedded mode" do
181189
config = [
182190
mode: :embedded,
191+
env: :dev,
183192
repo: Support.ConfigTestRepo,
184193
storage_dir: "/something"
185194
]
186195

187-
api = App.plug_opts(:dev, config)
196+
api = App.plug_opts(config)
188197

189198
assert %Electric.Shapes.Api{
190199
storage: {Electric.ShapeCache.FileStorage, %{base_path: "/something" <> _}},
@@ -197,6 +206,7 @@ defmodule Phoenix.Sync.ApplicationTest do
197206

198207
config = [
199208
mode: :http,
209+
env: :prod,
200210
url: url,
201211
credentials: [
202212
secret: "my-secret",
@@ -209,7 +219,7 @@ defmodule Phoenix.Sync.ApplicationTest do
209219

210220
endpoint = URI.new!(url) |> URI.append_path("/v1/shape")
211221

212-
assert api = App.plug_opts(:prod, config)
222+
assert api = App.plug_opts(config)
213223

214224
assert %Phoenix.Sync.Electric.ClientAdapter{
215225
client: %Electric.Client{
@@ -224,6 +234,7 @@ defmodule Phoenix.Sync.ApplicationTest do
224234

225235
config = [
226236
mode: :http,
237+
env: :prod,
227238
repo: Support.ConfigTestRepo,
228239
url: "http://localhost:4000",
229240
http: [
@@ -234,7 +245,7 @@ defmodule Phoenix.Sync.ApplicationTest do
234245

235246
endpoint = URI.new!(url) |> URI.append_path("/v1/shape")
236247

237-
assert api = App.plug_opts(:prod, config)
248+
assert api = App.plug_opts(config)
238249

239250
assert %Phoenix.Sync.Electric.ClientAdapter{
240251
client: %Electric.Client{

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/electric-sql/phoenix_sync/commit/8d699813b24afc6cd3e0929ac5804b74d346e7e9

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy