ash_hq/priv/repo/migrations/20220823191104_add_library_order.exs

31 lines
716 B
Elixir
Raw Normal View History

defmodule AshHq.Repo.Migrations.AddLibraryOrder do
@moduledoc """
Updates resources based on their most recent snapshots.
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""
use Ecto.Migration
def up do
alter table(:libraries) do
add :order, :bigint, null: true
end
execute("""
WITH sorted AS (SELECT id, ROW_NUMBER() OVER(ORDER BY name ASC) AS new_order FROM libraries)
UPDATE libraries SET "order" = (SELECT new_order FROM sorted WHERE sorted.id = libraries.id) - 1;
""")
alter table(:libraries) do
modify :order, :bigint, null: false
end
end
def down do
alter table(:libraries) do
remove :order
end
end
end