ash-project / ash-project/ash

`ash.codegen` generates wrong sequence of migration

Open
#1,394 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Elixir
Stars
2.5k
Forks
426
Avg merge
23h 26m
Merged PRs (30d)
46

Description

**Describe the bug**
When a boolean attribute in a Resource is renamed, the migration code generated by `ash.codegen` will fail on rollback.

**To Reproduce**
1. Create a Resource `ResourceX`, with `attribute :is_authentic, :boolean`
2. `mix ash.codegen create_resource_x_table`
3. `mix ecto.migrate`
4. Edit the ResourceX file and change the attribute line to `attribute :is_spicy, :boolean`
5. `mix ash.codegen modify_resource_x_table`
6. `mix ecto.migrate`
All good up till here
7. Rollback with `mix ecto.rollback -n 1`
Fails

**Expected behavior**
I expect Step 7 above to work. I have added additional information in 2 sections below

**Actual Generated Migration Code at Step 5**
```use Ecto.Migration

def up do
rename table(:resource_x), :is_authentic, to: :is_spicy

alter table(:resource_x) do
modify :is_spicy, :boolean
end
end

def down do
alter table(:resource_x) do
modify :is_authentic, :boolean
end

rename table(:resource_x), :is_spicy, to: :is_authentic
end
```

The sequence for `def down` is incorrect

**Expected/Correct Code**
```
def down do
rename table(:resource_x), :is_spicy, to: :is_authentic

alter table(:resource_x) do
modify :is_authentic, :boolean
end
end
```

**Runtime**
- Elixir version
`Elixir 1.17.2 (compiled with Erlang/OTP 27)`

- Erlang version
27.0.1
`Erlang/OTP 27 [erts-15.0.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]`

- OS
`Linux prometheus 6.10.4-zen2-1-zen #1 ZEN SMP PREEMPT_DYNAMIC Sun, 11 Aug 2024 16:18:46 +0000 x86_64 GNU/Linux`

- Ash version
- any related extension versions
:ash, "3.4.1"
:ash_phoenix, "2.1.1"
:ash_postgres, "2.2.1"
:ash_sql, "0.2.30"

Add any other context about the problem here.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the listed `mix ash.codegen`, `mix ecto.migrate`, and `mix ecto.rollback -n 1` commands using a boolean attribute rename. Compare the generated `def up` and `def down` migration sequences; done means the rollback succeeds and reverses the rename before modifying the restored attribute.

Written by the indexing model from the issue text.

Assessment

Tech stack
elixir
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.