Skip to content

Wrong order for the down/0 migration when changing both a field's name and nullability #582

@ibarakaiev

Description

@ibarakaiev

Versions

Elixir: 1.18.4
OPT: 27

Operating system

MacOS

Current Behavior

I was introducing AshStateMachine to a resource which had an existing optional status field, which auto-generated the following migration:

defmodule MyApp.Repo.Migrations.UpdateProducts 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
    rename table(:products), :status, to: :state

    alter table(:products) do
      modify :state, :text, null: false
    end
  end

  def down do
    alter table(:products) do
      modify :status, :text, null: true
    end

    rename table(:product), :state, to: :status
  end
end

The down/0 migration here fails because it attempts to modify :status before :state is renamed to :status.

Reproduction

Have an existing resource with a nullable status field, then remove it, and introduce AshStateMachine (or just rename it to :state and make it nullable directly).

Expected Behavior

The correct order should be:

def down do
  alter table(:products) do
    modify :status, :text, null: true
  end

  rename table(:product), :state, to: :status
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    Status

    Someday

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions