|
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 |
88 | 90 | end |
89 | 91 | end |
0 commit comments