Skip to content

Commit

Permalink
Merge pull request #71 from supabase/fix/22
Browse files Browse the repository at this point in the history
fix: converts the slot_name to lower case so that it matches Postgres
  • Loading branch information
kiwicopple authored Sep 14, 2020
2 parents 4abbcf6 + 847d563 commit 9a39fb2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ DB_NAME # {string} Postgres database name
DB_USER # {string} Database user
DB_PASSWORD # {string} Database password
DB_PORT # {number} Database port
SLOT_NAME # {string} A unique name for Postgres to track where this server has "listened until". If the server dies, it can pick up from the last position.
SLOT_NAME # {string} A unique name for Postgres to track where this server has "listened until". If the server dies, it can pick up from the last position. This should be lowercase.
PORT # {number} Port which you can connect your client/listeners
```

Expand Down
22 changes: 21 additions & 1 deletion server/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
# Realtime Elixir Instructions
# Realtime server

## Run locally

```sh
SECRET_KEY_BASE=SOMETHING_SECRET \
PORT=4000 \
HOSTNAME=localhost \
DB_USER=postgres \
DB_HOST=localhost \
DB_PASSWORD=postgres \
DB_NAME=postgres \
DB_PORT=5432 \
DB_PORT=5432 \
SLOT_NAME=TEST_SLOT \
mix phx.server
```


## Realtime Elixir Instructions

Create the release:

Expand Down Expand Up @@ -27,6 +46,7 @@ DB_HOST=localhost \
DB_PASSWORD=postgres \
DB_NAME=postgres \
DB_PORT=5432 \
DB_PORT=5432 \
_build/prod/rel/realtime/bin/realtime start
```

Expand Down
34 changes: 23 additions & 11 deletions server/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@
# General application configuration
import Config

# These configs mirror the defaults in releases.exs, remember not to change one
# These defaults mirror the ones in config.exs, remember not to change one
# without changing the other.
app_hostname = System.get_env("HOSTNAME", "localhost")
app_port = String.to_integer(System.get_env("PORT", "4000"))
db_host = System.get_env("DB_HOST", "localhost")
db_port = String.to_integer(System.get_env("DB_PORT", "5432"))
db_name = System.get_env("DB_NAME", "postgres")
db_user = System.get_env("DB_USER", "postgres")
db_password = System.get_env("DB_PASSWORD", "postgres")
# HACK: There's probably a better way to set boolean from env
db_ssl = System.get_env("DB_SSL", "true") === "true"
slot_name = System.get_env("SLOT_NAME") || :temporary
configuration_file = System.get_env("CONFIGURATION_FILE") || nil

config :realtime,
app_hostname: "localhost",
app_port: 4000,
db_host: "localhost",
db_port: 5432,
db_name: "postgres",
db_user: "postgres",
db_password: "postgres",
db_ssl: true,
slot_name: :temporary,
configuration_file: nil
app_hostname: app_hostname,
app_port: app_port,
db_host: db_host,
db_port: db_port,
db_name: db_name,
db_user: db_user,
db_password: db_password,
db_ssl: db_ssl,
slot_name: slot_name,
configuration_file: configuration_file

# Configures the endpoint
config :realtime, RealtimeWeb.Endpoint,
Expand Down
11 changes: 5 additions & 6 deletions server/lib/adapters/postgres/epgsql_implementation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

defmodule Realtime.Adapters.Postgres.EpgsqlImplementation do
@behaviour Realtime.Adapters.Postgres.AdapterBehaviour
require Logger

alias Realtime.Replication.State

Expand Down Expand Up @@ -58,13 +59,11 @@ defmodule Realtime.Adapters.Postgres.EpgsqlImplementation do
case slot do
name when is_binary(name) ->
# Simple query for replication mode so no prepared statements are supported
escaped_name = String.replace(name, "'", "\\'")
escaped_name = String.downcase(String.replace(name, "'", "\\'"))
query =
"SELECT COUNT(*) >= 1 FROM pg_replication_slots WHERE slot_name = '#{escaped_name}'"

{:ok, _, [{existing_slot}]} =
:epgsql.squery(
epgsql_pid,
"SELECT COUNT(*) >= 1 FROM pg_replication_slots WHERE slot_name = '#{escaped_name}'"
)
{:ok, _, [{existing_slot}]} = :epgsql.squery(epgsql_pid, query)

case existing_slot do
"t" ->
Expand Down
2 changes: 2 additions & 0 deletions server/lib/realtime/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ defmodule Realtime.Application do
# Hostname must be a char list for some reason
# Use this var to convert to sigil at connection
host = Application.fetch_env!(:realtime, :db_host)

# Use a named replication slot if you want realtime to pickup from where
# it left after a restart because of, for example, a crash.
# This will always be converted to lower-case.
# You can get a list of active replication slots with
# `select * from pg_replication_slots`
slot_name = Application.get_env(:realtime, :slot_name)
Expand Down

0 comments on commit 9a39fb2

Please sign in to comment.