Skip to content

Commit

Permalink
fix: always use scoped storage, even when a storage dir is provided (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
icehaunter authored Nov 21, 2024
1 parent 6b61ddd commit 6a88009
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-rice-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@core/sync-service": patch
---

fix: correctly scope the storage based on the tenant and use already-provided tenant id
6 changes: 3 additions & 3 deletions integration-tests/tests/_macros.luxinc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
[endmacro]

[macro setup_electric]
[invoke setup_electric_with_env "ELECTRIC_DATABASE_ID=integration_test_tenant DATABASE_URL=$database_url"]
[invoke setup_electric_with_env "DATABASE_URL=$database_url"]
[endmacro]

[macro setup_multi_tenant_electric]
Expand All @@ -96,7 +96,7 @@
[endmacro]

[macro setup_electric_with_env_and_tenant env]
[invoke setup_electric_with_env "ELECTRIC_DATABASE_ID=integration_test_tenant DATABASE_URL=$database_url $env"]
[invoke setup_electric_with_env "DATABASE_URL=$database_url $env"]
[endmacro]

[macro setup_electric_shell shell_name port env]
Expand All @@ -107,7 +107,7 @@
[endmacro]

[macro setup_electric_shell_with_tenant shell_name port]
[invoke setup_electric_shell $shell_name $port "ELECTRIC_DATABASE_ID=integration_test_tenant DATABASE_URL=$database_url"]
[invoke setup_electric_shell $shell_name $port "DATABASE_URL=$database_url"]
[endmacro]

[macro add_tenant tenant_id electric_port]
Expand Down
1 change: 0 additions & 1 deletion packages/sync-service/.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ ELECTRIC_CACHE_MAX_AGE=1
ELECTRIC_CACHE_STALE_AGE=3
# using a small chunk size of 10kB for dev to speed up tests
ELECTRIC_LOG_CHUNK_BYTES_THRESHOLD=10000
ELECTRIC_DATABASE_ID=test_tenant
# configuring a second database for multi-tenancy integration testing
OTHER_DATABASE_URL=postgresql://postgres:password@localhost:54322/electric?sslmode=disable
1 change: 0 additions & 1 deletion packages/sync-service/.env.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
LOG_LEVEL=info
DATABASE_URL=postgresql://postgres:password@localhost:54321/postgres?sslmode=disable
ELECTRIC_DATABASE_ID=test_tenant
4 changes: 4 additions & 0 deletions packages/sync-service/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,12 @@ replication_stream_id =
storage = {storage_mod, storage_opts}

prometheus_port = env!("ELECTRIC_PROMETHEUS_PORT", :integer, nil)
# The provided database id is relevant if you had used v0.8 and want to keep the storage
# instead of having hanging files. We use a provided value as stack id, but nothing else.
provided_database_id = env!("ELECTRIC_DATABASE_ID", :string, "single_stack")

config :electric,
provided_database_id: provided_database_id,
allow_shape_deletion: enable_integration_testing,
cache_max_age: cache_max_age,
cache_stale_age: cache_stale_age,
Expand Down
2 changes: 1 addition & 1 deletion packages/sync-service/lib/electric/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Electric.Application do
# We have "instance id" identifier as the node ID, however that's generated every runtime,
# so isn't stable across restarts. Our storages however scope themselves based on this stack ID
# so we're just hardcoding it here.
stack_id = "single_stack"
stack_id = Application.get_env(:electric, :provided_database_id, "single_stack")

router_opts =
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ defmodule Electric.ShapeCache.FileStorage do
@impl Electric.ShapeCache.Storage
def shared_opts(opts) do
stack_id = Keyword.fetch!(opts, :stack_id)
storage_dir = Keyword.get(opts, :storage_dir, "./shapes/#{stack_id}")
storage_dir = Keyword.get(opts, :storage_dir, "./shapes")

%{base_path: storage_dir, stack_id: stack_id}
# Always scope the provided storage dir by stack id
%{base_path: Path.join(storage_dir, stack_id), stack_id: stack_id}
end

@impl Electric.ShapeCache.Storage
Expand Down

0 comments on commit 6a88009

Please sign in to comment.