Fly.Postgres (Fly Postgres v0.3.4) View Source

Help Elixir applications more easily take advantage of distributed Elixir applications using Ecto and PostgreSQL in a primary/replica configuration on Fly.io.

Link to this section Summary

Functions

Rewrite the database config based on the runtime environment.

Returns the Repo module used by the Tracker that is not the wrapped version. Used for making direct writable calls.

Function used to make the repository be read-only and error when creates, updates, or deletes are attempted. This behaves like a read-only replica which is helpful when modelling that setup locally in a dev environment.

Compute the database url to use for this app given the current configuration and runtime environment.

Rewrite the :url value to include DNS helpers of "top1.nearest.of" to find the closes database to target. If the host already contains that, leave it unchanged. If it is missing, add it and return the updated the url in the config. Raise an exception if the no URL set in the config.

Rewrite the :url value to target the Postgres replica port of 5433.

Execute the MFA (Module, Function, Arguments) on a node in the primary region. This waits for the data to be replicated to the current node before continuing on.

Generate and log "verbose" log messages only if enabled.

Generate and log "verbose" log messages only if running on a remote (not-primary region) and verbose logging is enabled.

Link to this section Types

Specs

env() :: :prod | :dev | :test

Link to this section Functions

Link to this function

config_repo_url(config, env)

View Source

Specs

config_repo_url(config :: keyword(), env()) :: {:ok, keyword()} | no_return()

Rewrite the database config based on the runtime environment.

This does not make changes when running a dev or test build.

Returns the Repo module used by the Tracker that is not the wrapped version. Used for making direct writable calls.

Example

Application is used to configure the tracker.

# Given Application config like this:
{Fly.Postgres.LSN.Tracker, repo: MyApp.Repo.Local}

Fly.Postgres()
#=> MyApp.Repo.Local

Fly.Postgres(tracker: Fly.Postgres.LSN.Tracker)
#=> MyApp.Repo.Local

# Given Application config like this:
{Fly.Postgres.LSN.Tracker, repo: MyApp.Repo.Local_1, name: :repo_tracker_1},
{Fly.Postgres.LSN.Tracker, repo: MyApp.Repo.Local_2, name: :repo_tracker_2},

Fly.Postgres(tracker: :repo_tracker_1)
#=> MyApp.Repo.Local_1

Fly.Postgres(tracker: :repo_tracker_2)
#=> MyApp.Repo.Local_2
Link to this function

make_connection_read_only!(conn)

View Source

Specs

make_connection_read_only!(DBConnection.t()) :: :ok | no_return()

Function used to make the repository be read-only and error when creates, updates, or deletes are attempted. This behaves like a read-only replica which is helpful when modelling that setup locally in a dev environment.

Example

In your config/dev.exs,

# Configure your database
config :my_app, MyApp.Repo.Local,
  username: "postgres",
  password: "postgres",
  database: "my_db_dev",
  hostname: "localhost",
  show_sensitive_data_on_connection_error: true,
  # Forcing the repo to be R/O locally for dev testing
  after_connect: {Fly, :make_connection_read_only!, []},
  pool_size: 10
Link to this function

rewrite_database_url!(config)

View Source

Specs

rewrite_database_url!(config :: keyword()) :: keyword() | no_return()

Compute the database url to use for this app given the current configuration and runtime environment.

Specs

rewrite_host(config :: keyword()) :: keyword()

Rewrite the :url value to include DNS helpers of "top1.nearest.of" to find the closes database to target. If the host already contains that, leave it unchanged. If it is missing, add it and return the updated the url in the config. Raise an exception if the no URL set in the config.

Link to this function

rewrite_replica_port(config)

View Source

Specs

rewrite_replica_port(config :: keyword()) :: keyword()

Rewrite the :url value to target the Postgres replica port of 5433.

Link to this function

rpc_and_wait(module, func, args, opts \\ [])

View Source

Execute the MFA (Module, Function, Arguments) on a node in the primary region. This waits for the data to be replicated to the current node before continuing on.

This presumes the primary region has direct access to a writable primary Postgres database.

Options

  • :tracker - The name of the tracker to wait on for replication tracking.
  • :rpc_timeout - Timeout duration to wait for RPC call to complete
  • :replication_timeout - Timeout duration to wait for replication to complete.

Generate and log "verbose" log messages only if enabled.

Link to this function

verbose_remote_log(kind, func)

View Source

Generate and log "verbose" log messages only if running on a remote (not-primary region) and verbose logging is enabled.