-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add implementation of embedded client
which talks to electric over the internal api, not http
- Loading branch information
1 parent
8955a58
commit 78ce6d0
Showing
17 changed files
with
494 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ jobs: | |
POSTGRES_VERSION: 140006 | ||
ELECTRIC_PORT: 3100 | ||
PG_PORT: 54323 | ||
ELECTRIC_URL: "http://127.0.0.1:3100" | ||
ELECTRIC_URL: "http://127.0.0.1:3333" | ||
DATABASE_URL: "postgresql://postgres:[email protected]:54323/postgres?sslmode=disable" | ||
services: | ||
postgres: | ||
|
@@ -67,59 +67,12 @@ jobs: | |
${{ runner.os }}-elixir-client-build-[${{ github.ref_name }}]- | ||
${{ runner.os }}-elixir-client-build- | ||
- name: Cache sync-service dependencies | ||
uses: actions/cache@v4 | ||
with: | ||
path: packages/sync-service/deps | ||
key: "${{ runner.os }}-sync-service-deps-${{ env.MIX_ENV }}-${{ hashFiles('packages/sync-service/mix.lock') }}" | ||
restore-keys: | | ||
${{ runner.os }}-sync-service-deps-${{ env.MIX_ENV }}-${{ hashFiles('packages/sync-service/mix.lock') }} | ||
${{ runner.os }}-sync-service-deps-${{ env.MIX_ENV }}- | ||
${{ runner.os }}-sync-service-deps- | ||
- name: Cache sync-service compiled code | ||
uses: actions/cache@v4 | ||
with: | ||
path: | | ||
packages/sync-service/_build/*/lib | ||
!packages/sync-service/_build/*/lib/electric | ||
key: "${{ runner.os }}-sync-service-build-${{ env.MIX_ENV }}-[${{ github.ref_name }}]-${{ github.sha }}" | ||
restore-keys: | | ||
${{ runner.os }}-sync-service-build-${{ env.MIX_ENV }}-[${{ github.ref_name }}]-${{ github.sha }} | ||
${{ runner.os }}-sync-service-build-${{ env.MIX_ENV }}-[${{ github.ref_name }}]- | ||
${{ runner.os }}-sync-service-build-${{ env.MIX_ENV }}- | ||
${{ runner.os }}-sync-service-build- | ||
- name: Install dependencies | ||
run: mix deps.get && mix deps.compile | ||
|
||
- name: Compiles without warnings | ||
run: mix compile --force --all-warnings --warnings-as-errors | ||
|
||
- name: Install sync-service dependencies | ||
run: mix deps.get && mix deps.compile | ||
working-directory: packages/sync-service | ||
env: | ||
MIX_ENV: prod | ||
|
||
- uses: JarvusInnovations/background-action@v1 | ||
name: Bootstrap System Under Test (SUT) | ||
with: | ||
run: | | ||
mix start_dev | ||
env mix run --no-halt & | ||
wait-on: "http://127.0.0.1:3100" | ||
|
||
tail: true | ||
log-output-resume: stderr | ||
wait-for: 1m | ||
log-output: true | ||
log-output-if: failure | ||
working-directory: packages/sync-service | ||
env: | ||
MIX_ENV: prod | ||
|
||
- name: Run tests | ||
run: mix test | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,5 @@ elixir_client-*.tar | |
|
||
# Temporary files, for example, from tests. | ||
/tmp/ | ||
|
||
/persistent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
if Code.ensure_loaded?(Electric.Shapes.Api) do | ||
defmodule Electric.Client.Embedded do | ||
alias Electric.Client.Fetch | ||
alias Electric.Client.ShapeDefinition | ||
alias Electric.Shapes.Api | ||
|
||
@behaviour Electric.Client.Fetch | ||
|
||
@impl Electric.Client.Fetch | ||
def fetch(%Fetch.Request{method: :delete} = request, opts) do | ||
{:ok, api} = Keyword.fetch(opts, :api) | ||
|
||
timestamp = DateTime.utc_now() | ||
|
||
with {:ok, request} <- Api.validate_for_delete(api, request_to_params(request)), | ||
%Api.Response{} = response <- Api.delete_shape(request) do | ||
{:ok, translate_response(response, timestamp)} | ||
end | ||
end | ||
|
||
def fetch(%Fetch.Request{method: :get} = request, opts) do | ||
{:ok, api} = Keyword.fetch(opts, :api) | ||
|
||
timestamp = DateTime.utc_now() | ||
|
||
with {:ok, request} <- Api.validate(api, request_to_params(request)), | ||
%Api.Response{} = response = Api.serve_shape_log(request) do | ||
{:ok, translate_response(response, timestamp)} | ||
end | ||
end | ||
|
||
defp translate_response(%Api.Response{} = response, timestamp) do | ||
%Fetch.Response{ | ||
status: response.status, | ||
last_offset: convert_offset(response.offset), | ||
shape_handle: response.handle, | ||
schema: Api.schema(response), | ||
next_cursor: nil, | ||
request_timestamp: timestamp, | ||
body: response.body | ||
} | ||
end | ||
|
||
defp request_to_params(%Fetch.Request{shape: %ShapeDefinition{} = shape} = request) do | ||
%{ | ||
table: Electric.Utils.relation_to_sql({shape.namespace || "public", shape.table}), | ||
offset: to_string(request.offset), | ||
handle: request.shape_handle, | ||
live: request.live, | ||
where: shape.where, | ||
columns: shape.columns, | ||
replica: request.replica | ||
} | ||
end | ||
|
||
defp convert_offset(nil) do | ||
nil | ||
end | ||
|
||
# TODO: when we remove offset parsing from the elixir client, then it makes | ||
# sense to use to_string on the server offset | ||
defp convert_offset(%Electric.Replication.LogOffset{} = server_offset) do | ||
Electric.Client.Offset.from_string!(to_string(server_offset)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.