DbOps (db_ops v0.2.2)

Copy Markdown View Source

Lightweight runtime database management utility for Elixir/Ecto Releases.

Designed to be invoked via eval on release binaries in production/staging environments where Mix is not installed.

Summary

Functions

Returns the configuration for all Ecto repos associated with the application.

Creates all databases associated with the application's Ecto repos.

Drops all databases associated with the application's Ecto repos.

Returns a PostgreSQL connection URL that is safe to pass to psql in production.

Runs the database seed script.

Checks the database status for all Ecto repos associated with the application.

Functions

config(app \\ default_app())

@spec config(atom()) :: [{module(), keyword()}]

Returns the configuration for all Ecto repos associated with the application.

Examples

DbOps.config()
DbOps.config(:my_app)

create(app \\ default_app())

@spec create(atom()) :: :ok

Creates all databases associated with the application's Ecto repos.

Examples

# In release execution:
# ./bin/my_app eval "DbOps.create(:my_app)"

drop(app, opts \\ [])

@spec drop(atom(), keyword()) :: :ok | {:error, :confirmation_required}

Drops all databases associated with the application's Ecto repos.

Requires explicit confirmation parameter to prevent accidental deletion in production.

Examples

# Refused execution:
# ./bin/my_app eval "DbOps.drop(:my_app)"

# Confirmed execution:
# ./bin/my_app eval "DbOps.drop(:my_app, confirm: "YES_DELETE_DATABASE")"

psql_url(app \\ default_app())

@spec psql_url(atom() | keyword() | String.t()) :: String.t()

Returns a PostgreSQL connection URL that is safe to pass to psql in production.

This method accepts the application's repo config or the DATABASE_URL value from the runtime environment and normalizes it to a stable postgres://... URL.

Examples

DbOps.psql_url(:my_app)
DbOps.psql_url(MyApp.Repo.config())
DbOps.psql_url("ecto://postgres:postgres@localhost/ecto_simple?ssl=true&pool_size=10")

seed(app \\ default_app(), seed_file \\ nil)

@spec seed(atom(), String.t() | nil) :: :ok | {:error, :seed_not_found}

Runs the database seed script.

Defaults to priv/repo/seeds.exs inside the application's priv directory.

Examples

# ./bin/my_app eval "DbOps.seed(:my_app)"
# ./bin/my_app eval "DbOps.seed(:my_app, "priv/repo/custom_seeds.exs")"

status(app \\ default_app())

@spec status(atom()) :: :ok

Checks the database status for all Ecto repos associated with the application.

Examples

# ./bin/my_app eval "DbOps.status()"
# ./bin/my_app eval "DbOps.status(:my_app)"