Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: order when renaming attribute with an index #514

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/migration_generator/migration_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,19 @@ defmodule AshPostgres.MigrationGenerator do
true
end

defp after?(
%Operation.AddCustomIndex{
table: table,
schema: schema
},
%Operation.RenameAttribute{
table: table,
schema: schema
}
) do
true
end

defp after?(
%Operation.AddReferenceIndex{
table: table,
Expand Down
81 changes: 81 additions & 0 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,87 @@ defmodule AshPostgres.MigrationGeneratorTest do
end
end

describe "custom_indexes with follow up migrations" do
setup do
on_exit(fn ->
File.rm_rf!("test_snapshots_path")
File.rm_rf!("test_migration_path")
end)

defposts do
postgres do
custom_indexes do
index([:title])
end
end

attributes do
uuid_primary_key(:id)
attribute(:title, :string, public?: true)
end
end

defdomain([Post])

AshPostgres.MigrationGenerator.generate(Domain,
snapshot_path: "test_snapshots_path",
migration_path: "test_migration_path",
quiet: true,
format: false
)
end

test "it changes attribute and index in the correct order" do
defposts do
postgres do
custom_indexes do
index([:title_short])
end
end

attributes do
uuid_primary_key(:id)
attribute(:title_short, :string, public?: true)
end
end

defdomain([Post])

send(self(), {:mix_shell_input, :yes?, true})

AshPostgres.MigrationGenerator.generate(Domain,
snapshot_path: "test_snapshots_path",
migration_path: "test_migration_path",
quiet: true,
format: false
)

assert [_file1, file2] =
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
|> Enum.reject(&String.contains?(&1, "extensions"))

contents = File.read!(file2)

[up_side, down_side] = String.split(contents, "def down", parts: 2)

up_side_parts = String.split(up_side, "\n", trim: true)

assert Enum.find_index(up_side_parts, fn x ->
x == "rename table(:posts), :title, to: :title_short"
end) <
Enum.find_index(up_side_parts, fn x ->
x == "create index(:posts, [:title_short])"
end)

down_side_parts = String.split(down_side, "\n", trim: true)

assert Enum.find_index(down_side_parts, fn x ->
x == "rename table(:posts), :title_short, to: :title"
end) <
Enum.find_index(down_side_parts, fn x -> x == "create index(:posts, [:title])" end)
end
end

describe "creating follow up migrations with a composite primary key" do
setup do
on_exit(fn ->
Expand Down
Loading