defmodule Oban.Pro.Migrations.Batch do
  @moduledoc false

  use Ecto.Migration

  def change(_opts \\ []), do: :ok

  def up(_opts \\ []), do: :ok

  def down(_opts \\ []), do: :ok
end

defmodule Oban.Pro.Migrations.Workflow do
  @moduledoc false

  use Ecto.Migration

  def change(_opts \\ []), do: :ok

  def up(_opts \\ []), do: :ok

  def down(_opts \\ []), do: :ok
end

defmodule Oban.Pro.Migrations.V140 do
  @moduledoc false

  use Ecto.Migration

  def up_ddl(opts) do
    alter table(:oban_crons, prefix: opts.prefix) do
      add_if_not_exists :insertions, {:array, :utc_datetime_usec}, default: []
    end
  end

  def up_idx(opts) do
    create_if_not_exists index(
                           :oban_jobs,
                           [:state, "(meta->>'workflow_id')", "(meta->>'name')"],
                           concurrently: opts.concurrently,
                           name: :oban_jobs_state_meta_workflow_index,
                           prefix: opts.prefix,
                           where: "meta ? 'workflow_id'"
                         )
  end

  def down_ddl(opts) do
    alter table(:oban_crons, prefix: opts.prefix) do
      remove_if_exists :insertions, {:array, :utc_datetime_usec}
    end
  end

  def down_idx(opts) do
    drop_if_exists index(:oban_jobs, [],
                     name: :oban_jobs_state_meta_workflow_index,
                     concurrently: opts.concurrently,
                     prefix: opts.prefix
                   )
  end
end
