Skip to content

Commit

Permalink
fix: Parse and validate REPLICATION_STREAM_ID (#1859)
Browse files Browse the repository at this point in the history
While setting up another project, I noticed that replication slots
_cannot_ contain special characters, so parsing and validating the
provided id early will prevent unnecessary headaches.
  • Loading branch information
msfstef authored Oct 16, 2024
1 parent e942a8e commit bdbfd46
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-carpets-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@core/sync-service": patch
---

Parse and validate `REPLICATION_STREAM_ID` as it cannot include special characters
15 changes: 14 additions & 1 deletion packages/sync-service/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ chunk_bytes_threshold =
storage_dir: shape_path, electric_instance_id: electric_instance_id}
)

replication_stream_id =
env!(
"REPLICATION_STREAM_ID",
fn replication_stream_id ->
{:ok, parsed_id} =
replication_stream_id
|> Electric.Postgres.Identifiers.parse_unquoted_identifier()

parsed_id
end,
"default"
)

storage = {storage_mod, storage_opts}

prometheus_port = env!("PROMETHEUS_PORT", :integer, nil)
Expand All @@ -174,7 +187,7 @@ config :electric,
electric_instance_id: electric_instance_id,
telemetry_statsd_host: statsd_host,
db_pool_size: env!("DB_POOL_SIZE", :integer, 20),
replication_stream_id: env!("REPLICATION_STREAM_ID", :string, "default"),
replication_stream_id: replication_stream_id,
service_port: env!("PORT", :integer, 3000),
prometheus_port: prometheus_port,
storage: storage,
Expand Down
4 changes: 2 additions & 2 deletions packages/sync-service/lib/electric/postgres/identifiers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ defmodule Electric.Postgres.Identifiers do
else: {:ok, unescape_quotes(ident)}
end

defp parse_unquoted_identifier("", _, _), do: parse_quoted_identifier("")
def parse_unquoted_identifier("", _, _), do: parse_quoted_identifier("")

defp parse_unquoted_identifier(ident, truncate, single_byte_encoding) do
def parse_unquoted_identifier(ident, truncate, single_byte_encoding) do
unless valid_unquoted_identifier?(ident),
do: {:error, "Invalid unquoted identifier contains special characters: #{ident}"},
else: {:ok, downcase(ident, truncate, single_byte_encoding)}
Expand Down

0 comments on commit bdbfd46

Please sign in to comment.