Skip to content

Commit

Permalink
Add unlogged option for extended database support
Browse files Browse the repository at this point in the history
Some postgres-compatible databases such as Yugabyte don't support
unlogged tables. Making oban_peers unlogged isn't a requirement for Oban
to operate, so it can be disabled with a migration flag.
  • Loading branch information
sorentwo committed Jul 11, 2024
1 parent d2c19b4 commit 9d60b5a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/oban/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ defmodule Oban.Migration do
Run migrations in an alternate prefix but don't try to create the schema:
Oban.Migration.up(prefix: "payments", create_schema: false)
Disable `UNLOGGED` tables for databases that don't support it (e.g. Yugabyte)
Oban.Migration.up(unlogged: false)
"""
def up(opts \\ []) when is_list(opts) do
migrator().up(opts)
Expand Down
5 changes: 3 additions & 2 deletions lib/oban/migrations/postgres.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ defmodule Oban.Migrations.Postgres do
opts = Enum.into(opts, %{prefix: @default_prefix, version: version})

opts
|> Map.put(:quoted_prefix, inspect(opts.prefix))
|> Map.put(:escaped_prefix, String.replace(opts.prefix, "'", "\\'"))
|> Map.put_new(:unlogged, true)
|> Map.put_new(:create_schema, opts.prefix != @default_prefix)
|> Map.put_new(:quoted_prefix, inspect(opts.prefix))
|> Map.put_new(:escaped_prefix, String.replace(opts.prefix, "'", "\\'"))
end
end
6 changes: 4 additions & 2 deletions lib/oban/migrations/postgres/v11.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ defmodule Oban.Migrations.Postgres.V11 do

use Ecto.Migration

def up(%{prefix: prefix, quoted_prefix: quoted}) do
def up(%{prefix: prefix, quoted_prefix: quoted, unlogged: unlogged?}) do
create_if_not_exists table(:oban_peers, primary_key: false, prefix: prefix) do
add :name, :text, null: false, primary_key: true
add :node, :text, null: false
add :started_at, :utc_datetime_usec, null: false
add :expires_at, :utc_datetime_usec, null: false
end

execute "ALTER TABLE #{quoted}.oban_peers SET UNLOGGED"
if unlogged? do
execute "ALTER TABLE #{quoted}.oban_peers SET UNLOGGED"
end
end

def down(%{prefix: prefix}) do
Expand Down

0 comments on commit 9d60b5a

Please sign in to comment.