Skip to content

Commit e8c72ad

Browse files
committed
drop electric version, fix compilation w/o electric
1 parent 7c0be92 commit e8c72ad

File tree

10 files changed

+550
-516
lines changed

10 files changed

+550
-516
lines changed

lib/phoenix/sync/electric.ex

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,16 @@ defmodule Phoenix.Sync.Electric do
289289
|> Keyword.fetch!(:api)
290290
end
291291

292-
defp plug_opts(_env, :sandbox, _electric_opts) do
293-
%Phoenix.Sync.Sandbox.APIAdapter{}
292+
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
293+
defp plug_opts(_env, :sandbox, _electric_opts) do
294+
%Phoenix.Sync.Sandbox.APIAdapter{}
295+
end
296+
else
297+
defp plug_opts(_env, :sandbox, _electric_opts) do
298+
raise ArgumentError,
299+
message:
300+
"phoenix_sync configured in `mode: :sandbox` but Ecto.SQL not installed. Please add `:ecto_sql` to your dependencies or use `:http` mode."
301+
end
294302
end
295303
else
296304
defp plug_opts(_env, :embedded, _electric_opts) do
@@ -529,8 +537,14 @@ defmodule Phoenix.Sync.Electric do
529537
# to a real instance -- I think that's reasonable. The overhead of a real
530538
# electric consuming a real replication stream is way too high for a simple
531539
# consumer of streams
532-
defp configure_client(_opts, :sandbox) do
533-
Phoenix.Sync.Sandbox.client()
540+
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
541+
defp configure_client(_opts, :sandbox) do
542+
Phoenix.Sync.Sandbox.client()
543+
end
544+
else
545+
defp configure_client(_opts, :sandbox) do
546+
{:error, "sandbox mode is only available if Ecto.SQL is installed"}
547+
end
534548
end
535549
else
536550
defp configure_client(_opts, :embedded) do
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
defmodule Phoenix.Sync.Sandbox.APIAdapter do
2-
@moduledoc false
1+
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
2+
defmodule Phoenix.Sync.Sandbox.APIAdapter do
3+
@moduledoc false
34

4-
defstruct [:shape]
5+
defstruct [:shape]
56

6-
alias Phoenix.Sync.Adapter.PlugApi
7+
alias Phoenix.Sync.Adapter.PlugApi
78

8-
defimpl Phoenix.Sync.Adapter.PlugApi do
9-
def predefined_shape(adapter, shape) do
10-
{:ok, %{adapter | shape: shape}}
11-
end
9+
defimpl Phoenix.Sync.Adapter.PlugApi do
10+
def predefined_shape(adapter, shape) do
11+
{:ok, %{adapter | shape: shape}}
12+
end
1213

13-
def call(%{shape: nil} = _adapter, conn, params) do
14-
shape_api = lookup_api!()
15-
PlugApi.call(shape_api, conn, params)
16-
end
14+
def call(%{shape: nil} = _adapter, conn, params) do
15+
shape_api = lookup_api!()
16+
PlugApi.call(shape_api, conn, params)
17+
end
1718

18-
def call(%{shape: shape} = _adapter, conn, params) do
19-
shape_api = lookup_api!()
20-
{:ok, shape_api} = PlugApi.predefined_shape(shape_api, shape)
19+
def call(%{shape: shape} = _adapter, conn, params) do
20+
shape_api = lookup_api!()
21+
{:ok, shape_api} = PlugApi.predefined_shape(shape_api, shape)
2122

22-
PlugApi.call(shape_api, conn, params)
23-
end
23+
PlugApi.call(shape_api, conn, params)
24+
end
2425

25-
defp lookup_api!() do
26-
Phoenix.Sync.Sandbox.retrieve_api!()
26+
defp lookup_api!() do
27+
Phoenix.Sync.Sandbox.retrieve_api!()
28+
end
2729
end
2830
end
2931
end
Lines changed: 89 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,91 @@
1-
defmodule Phoenix.Sync.Sandbox.Inspector do
2-
@moduledoc false
3-
4-
use GenServer
5-
6-
@behaviour Electric.Postgres.Inspector
7-
8-
@impl Electric.Postgres.Inspector
9-
10-
def load_relation_oid(relation, stack_id) do
11-
GenServer.call(name(stack_id), {:load_relation_oid, relation})
12-
end
13-
14-
@impl Electric.Postgres.Inspector
15-
def load_relation_info(relation, stack_id) do
16-
GenServer.call(name(stack_id), {:load_relation_info, relation})
17-
end
18-
19-
@impl Electric.Postgres.Inspector
20-
def load_column_info(relation, stack_id) do
21-
GenServer.call(name(stack_id), {:load_column_info, relation})
22-
end
23-
24-
@impl Electric.Postgres.Inspector
25-
def clean(_, _), do: true
26-
27-
@impl Electric.Postgres.Inspector
28-
def list_relations_with_stale_cache(_), do: {:ok, []}
29-
30-
def start_link(args) do
31-
GenServer.start_link(__MODULE__, args, name: name(args[:stack_id]))
32-
end
33-
34-
def name(stack_id) do
35-
Phoenix.Sync.Sandbox.name({__MODULE__, stack_id})
36-
end
37-
38-
def current_xmax(stack_id) do
39-
GenServer.call(name(stack_id), :current_xmax)
40-
end
41-
42-
@impl GenServer
43-
def init(args) do
44-
{:ok, stack_id} = Keyword.fetch(args, :stack_id)
45-
{:ok, repo} = Keyword.fetch(args, :repo)
46-
47-
{:ok, %{repo: repo, stack_id: stack_id, relations: %{}}}
48-
end
49-
50-
@impl GenServer
51-
def handle_call({:load_relation_oid, relation}, _from, state) do
52-
{
53-
:reply,
54-
Electric.Postgres.Inspector.DirectInspector.load_relation_oid(relation, pool(state)),
55-
state
56-
}
57-
end
58-
59-
def handle_call({:load_relation_info, relation}, _from, state) do
60-
{
61-
:reply,
62-
Electric.Postgres.Inspector.DirectInspector.load_relation_info(relation, pool(state)),
63-
state
64-
}
65-
end
66-
67-
def handle_call({:load_column_info, relation}, _from, state) do
68-
{
69-
:reply,
70-
Electric.Postgres.Inspector.DirectInspector.load_column_info(relation, pool(state)),
71-
state
72-
}
73-
end
74-
75-
def handle_call(:current_xmax, _from, state) do
76-
reply =
77-
with {:ok, %{rows: [[xmax]]}} <-
78-
Postgrex.query(pool(state), "SELECT pg_snapshot_xmax(pg_current_snapshot())", []) do
79-
{:ok, xmax}
80-
end
81-
82-
{:reply, reply, state}
83-
end
84-
85-
defp pool(state) do
86-
%{pid: pool} = Ecto.Adapter.lookup_meta(state.repo.get_dynamic_repo())
87-
pool
1+
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
2+
defmodule Phoenix.Sync.Sandbox.Inspector do
3+
@moduledoc false
4+
5+
use GenServer
6+
7+
@behaviour Electric.Postgres.Inspector
8+
9+
@impl Electric.Postgres.Inspector
10+
11+
def load_relation_oid(relation, stack_id) do
12+
GenServer.call(name(stack_id), {:load_relation_oid, relation})
13+
end
14+
15+
@impl Electric.Postgres.Inspector
16+
def load_relation_info(relation, stack_id) do
17+
GenServer.call(name(stack_id), {:load_relation_info, relation})
18+
end
19+
20+
@impl Electric.Postgres.Inspector
21+
def load_column_info(relation, stack_id) do
22+
GenServer.call(name(stack_id), {:load_column_info, relation})
23+
end
24+
25+
@impl Electric.Postgres.Inspector
26+
def clean(_, _), do: true
27+
28+
@impl Electric.Postgres.Inspector
29+
def list_relations_with_stale_cache(_), do: {:ok, []}
30+
31+
def start_link(args) do
32+
GenServer.start_link(__MODULE__, args, name: name(args[:stack_id]))
33+
end
34+
35+
def name(stack_id) do
36+
Phoenix.Sync.Sandbox.name({__MODULE__, stack_id})
37+
end
38+
39+
def current_xmax(stack_id) do
40+
GenServer.call(name(stack_id), :current_xmax)
41+
end
42+
43+
@impl GenServer
44+
def init(args) do
45+
{:ok, stack_id} = Keyword.fetch(args, :stack_id)
46+
{:ok, repo} = Keyword.fetch(args, :repo)
47+
48+
{:ok, %{repo: repo, stack_id: stack_id, relations: %{}}}
49+
end
50+
51+
@impl GenServer
52+
def handle_call({:load_relation_oid, relation}, _from, state) do
53+
{
54+
:reply,
55+
Electric.Postgres.Inspector.DirectInspector.load_relation_oid(relation, pool(state)),
56+
state
57+
}
58+
end
59+
60+
def handle_call({:load_relation_info, relation}, _from, state) do
61+
{
62+
:reply,
63+
Electric.Postgres.Inspector.DirectInspector.load_relation_info(relation, pool(state)),
64+
state
65+
}
66+
end
67+
68+
def handle_call({:load_column_info, relation}, _from, state) do
69+
{
70+
:reply,
71+
Electric.Postgres.Inspector.DirectInspector.load_column_info(relation, pool(state)),
72+
state
73+
}
74+
end
75+
76+
def handle_call(:current_xmax, _from, state) do
77+
reply =
78+
with {:ok, %{rows: [[xmax]]}} <-
79+
Postgrex.query(pool(state), "SELECT pg_snapshot_xmax(pg_current_snapshot())", []) do
80+
{:ok, xmax}
81+
end
82+
83+
{:reply, reply, state}
84+
end
85+
86+
defp pool(state) do
87+
%{pid: pool} = Ecto.Adapter.lookup_meta(state.repo.get_dynamic_repo())
88+
pool
89+
end
8890
end
8991
end

0 commit comments

Comments
 (0)