Skip to content

Instantly share code, notes, and snippets.

@sorentwo
Created September 2, 2024 16:47
Show Gist options
  • Save sorentwo/40357770426dbf4d2efd6f20ec525f92 to your computer and use it in GitHub Desktop.
Save sorentwo/40357770426dbf4d2efd6f20ec525f92 to your computer and use it in GitHub Desktop.
A simplified Oban migration that's compatible with CockroachDB
execute """
CREATE TYPE oban_job_state AS ENUM (
'available',
'scheduled',
'executing',
'retryable',
'completed',
'cancelled',
'discarded'
)
"""
create table(:oban_jobs, primary_key: false) do
add :id, :bigserial, primary_key: true
add :state, :"public.oban_job_state", null: false, default: "available"
add :queue, :text, null: false, default: "default"
add :worker, :text, null: false
add :args, :map, null: false
add :meta, :map, null: false
add :tags, :jsonb, null: false, default: fragment("('[]')")
add :errors, :jsonb, null: false, default: fragment("('[]')")
add :attempt, :integer, null: false, default: 0
add :max_attempts, :integer, null: false, default: 20
add :priority, :integer, null: false, default: 0
add :attempted_by, {:array, :text}
add :inserted_at, :utc_datetime_usec, null: false, default: fragment("timezone('UTC', now())")
add :scheduled_at, :utc_datetime_usec, null: false, default: fragment("timezone('UTC', now())")
add :attempted_at, :utc_datetime_usec
add :completed_at, :utc_datetime_usec
add :cancelled_at, :utc_datetime_usec
add :discarded_at, :utc_datetime_usec
end
create index(:oban_jobs, [:state, :queue, :priority, :scheduled_at, :id])
create index(:oban_jobs, [:args], using: :gin)
create index(:oban_jobs, [:meta], using: :gin)
create table(:oban_peers, primary_key: false) 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment