fix: consider old/new formats for generating migrations

This commit is contained in:
Zach Daniel 2024-06-19 12:41:24 -04:00
parent aa40778cc4
commit 39e2cfd149
3 changed files with 28 additions and 18 deletions

View file

@ -2891,7 +2891,7 @@ defmodule AshPostgres.MigrationGenerator do
AshPostgres.DataLayer.Info.calculation_to_sql(resource, key) ||
raise "Must define an entry for :#{key} in `postgres.calculations_to_sql`, or skip this identity with `postgres.skip_unique_indexes`"
{:sql, "(" <> sql <> ")"}
"(" <> sql <> ")"
end
end),
where:
@ -3004,8 +3004,8 @@ defmodule AshPostgres.MigrationGenerator do
Enum.map(identities, fn identity ->
keys =
Enum.map(identity.keys, fn
{:sql, value} when is_binary(value) -> ["sql", value]
key -> key
value when is_binary(value) -> %{"type" => "string", "value" => value}
value when is_atom(value) -> %{"type" => "atom", "value" => value}
end)
%{identity | keys: keys}
@ -3265,8 +3265,18 @@ defmodule AshPostgres.MigrationGenerator do
|> Map.update!(
:keys,
&Enum.map(&1, fn
["sql", value] when is_binary(value) -> {:sql, value}
key -> key
%{type: "atom", value: value} ->
maybe_to_atom(value)
%{type: "string", value: value} ->
value
value ->
if String.contains?(value, "(") do
value
else
maybe_to_atom(value)
end
end)
)
end

View file

@ -30,7 +30,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
# sobelow_skip ["DOS.StringToAtom"]
def as_atom(value), do: Macro.inspect_atom(:remote_call, String.to_atom(value))
def index_key({:sql, value}) when is_binary(value), do: inspect(value)
def index_key(value) when is_binary(value), do: inspect(value)
def index_key(value) when is_binary(value) or is_atom(value), do: ":#{as_atom(value)}"
def option(key, value) when key in [:nulls_distinct, "nulls_distinct"] do

View file

@ -156,15 +156,15 @@ defmodule AshPostgres.MigrationGeneratorTest do
# the migration creates unique_indexes based on the identities of the resource
assert file_contents =~
~S{create unique_index(:posts, ["title"], name: "posts_title_index")}
~S{create unique_index(:posts, [:title], name: "posts_title_index")}
# the migration creates unique_indexes based on the identities of the resource
assert file_contents =~
~S{create unique_index(:posts, ["title", "second_title"], name: "posts_thing_index")}
~S{create unique_index(:posts, [:title, :second_title], name: "posts_thing_index")}
# the migration creates unique_indexes using the `source` on the attributes of the identity on the resource
assert file_contents =~
~S{create unique_index(:posts, ["title", "t_w_s"], name: "posts_thing_with_source_index")}
~S{create unique_index(:posts, [:title, :t_w_s], name: "posts_thing_with_source_index")}
end
end
@ -241,7 +241,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
assert file_contents =~ ~S{create index(:posts, ["id"]}
assert file_contents =~
~S{create unique_index(:posts, ["second_title"], name: "posts_second_title_index", prefix: "example", nulls_distinct: false, where: "((second_title like '%foo%'))")}
~S{create unique_index(:posts, [:second_title], name: "posts_second_title_index", prefix: "example", nulls_distinct: false, where: "((second_title like '%foo%'))")}
# the migration adds the id, with its default
assert file_contents =~
@ -255,7 +255,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
# the migration creates unique_indexes based on the identities of the resource
assert file_contents =~
~S{create unique_index(:posts, ["title"], name: "posts_title_index", prefix: "example")}
~S{create unique_index(:posts, [:title], name: "posts_title_index", prefix: "example")}
end
end
@ -535,11 +535,11 @@ defmodule AshPostgres.MigrationGeneratorTest do
assert [file_before, _] =
String.split(
File.read!(file2),
~S{create unique_index(:posts, ["title"], name: "posts_title_index", where: "(title != 'fred' and title != 'george')")}
~S{create unique_index(:posts, [:title], name: "posts_title_index", where: "(title != 'fred' and title != 'george')")}
)
assert file_before =~
~S{drop_if_exists unique_index(:posts, ["title"], name: "posts_title_index")}
~S{drop_if_exists unique_index(:posts, [:title], name: "posts_title_index")}
end
test "when adding a field, it adds the field" do
@ -753,18 +753,18 @@ defmodule AshPostgres.MigrationGeneratorTest do
file1_content = File.read!(file1)
assert file1_content =~
"create unique_index(:posts, [\"title\"], name: \"posts_title_index\")"
"create unique_index(:posts, [:title], name: \"posts_title_index\")"
file2_content = File.read!(file2)
assert file2_content =~
"drop_if_exists unique_index(:posts, [\"title\"], name: \"posts_title_index\")"
"drop_if_exists unique_index(:posts, [:title], name: \"posts_title_index\")"
assert file2_content =~
"create unique_index(:posts, [\"name\"], name: \"posts_unique_name_index\")"
"create unique_index(:posts, [:name], name: \"posts_unique_name_index\")"
assert file2_content =~
"create unique_index(:posts, [\"title\"], name: \"posts_unique_title_index\")"
"create unique_index(:posts, [:title], name: \"posts_unique_title_index\")"
end
test "when an attribute exists only on some of the resources that use the same table, it isn't marked as null: false" do
@ -1241,7 +1241,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
assert [file] = Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
assert File.read!(file) =~
~S{create unique_index(:users, ["name"], name: "users_unique_name_index")}
~S{create unique_index(:users, [:name], name: "users_unique_name_index")}
end
test "when modified, the foreign key is dropped before modification" do