Skip to content

Commit a0a1c35

Browse files
committed
fix: don't rewrite identities when only global? is changed
fix: don't modify an attribute when it only needs to be renamed
1 parent aafb473 commit a0a1c35

File tree

2 files changed

+123
-6
lines changed

2 files changed

+123
-6
lines changed

lib/migration_generator/migration_generator.ex

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,21 +2305,22 @@ defmodule AshPostgres.MigrationGenerator do
23052305
Enum.find(
23062306
old_snapshot.attributes,
23072307
fn old_attribute ->
2308-
source_match =
2309-
Enum.find_value(attributes_to_rename, old_attribute.source, fn {new, old} ->
2308+
renaming_to_source =
2309+
Enum.find_value(attributes_to_rename, fn {new, old} ->
23102310
if old.source == old_attribute.source do
23112311
new.source
23122312
end
23132313
end)
23142314

2315-
source_match ==
2315+
(renaming_to_source || old_attribute.source) ==
23162316
attribute.source &&
23172317
attributes_unequal?(
23182318
old_attribute,
23192319
attribute,
23202320
snapshot.repo,
23212321
old_snapshot,
2322-
snapshot
2322+
snapshot,
2323+
not is_nil(renaming_to_source)
23232324
)
23242325
end
23252326
)}
@@ -2522,11 +2523,25 @@ defmodule AshPostgres.MigrationGenerator do
25222523

25232524
# This exists to handle the fact that the remapping of the key name -> source caused attributes
25242525
# to be considered unequal. We ignore things that only differ in that way using this function.
2525-
defp attributes_unequal?(left, right, repo, _old_snapshot, _new_snapshot) do
2526+
defp attributes_unequal?(left, right, repo, _old_snapshot, _new_snapshot, ignore_names?) do
25262527
left = clean_for_equality(left, repo)
25272528

25282529
right = clean_for_equality(right, repo)
25292530

2531+
left =
2532+
if ignore_names? do
2533+
Map.drop(left, [:source, :name])
2534+
else
2535+
left
2536+
end
2537+
2538+
right =
2539+
if ignore_names? do
2540+
Map.drop(left, [:source, :name])
2541+
else
2542+
right
2543+
end
2544+
25302545
left != right
25312546
end
25322547

@@ -2583,7 +2598,7 @@ defmodule AshPostgres.MigrationGenerator do
25832598
end
25842599

25852600
def changing_multitenancy_affects_identities?(snapshot, old_snapshot) do
2586-
snapshot.multitenancy != old_snapshot.multitenancy ||
2601+
Map.delete(snapshot.multitenancy, :global) != Map.delete(old_snapshot.multitenancy, :global) ||
25872602
snapshot.base_filter != old_snapshot.base_filter
25882603
end
25892604

test/migration_generator_test.exs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,77 @@ defmodule AshPostgres.MigrationGeneratorTest do
584584
end
585585
end
586586

587+
describe "changing global multitenancy" do
588+
setup do
589+
on_exit(fn ->
590+
File.rm_rf!("test_snapshots_path")
591+
File.rm_rf!("test_migration_path")
592+
end)
593+
594+
defposts do
595+
identities do
596+
identity(:title, [:title])
597+
end
598+
599+
multitenancy do
600+
strategy(:attribute)
601+
attribute(:organization_id)
602+
global?(false)
603+
end
604+
605+
attributes do
606+
uuid_primary_key(:id)
607+
attribute(:title, :string, public?: true)
608+
attribute(:organization_id, :string)
609+
end
610+
end
611+
612+
defdomain([Post])
613+
614+
AshPostgres.MigrationGenerator.generate(Domain,
615+
snapshot_path: "test_snapshots_path",
616+
migration_path: "test_migration_path",
617+
quiet: true,
618+
format: false
619+
)
620+
621+
:ok
622+
end
623+
624+
test "when changing multitenancy to global, identities aren't rewritten" do
625+
defposts do
626+
identities do
627+
identity(:title, [:title])
628+
end
629+
630+
multitenancy do
631+
strategy(:attribute)
632+
attribute(:organization_id)
633+
global?(true)
634+
end
635+
636+
attributes do
637+
uuid_primary_key(:id)
638+
attribute(:title, :string, public?: true)
639+
attribute(:organization_id, :string)
640+
end
641+
end
642+
643+
defdomain([Post])
644+
645+
AshPostgres.MigrationGenerator.generate(Domain,
646+
snapshot_path: "test_snapshots_path",
647+
migration_path: "test_migration_path",
648+
quiet: true,
649+
format: false
650+
)
651+
652+
assert [_file1] =
653+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
654+
|> Enum.reject(&String.contains?(&1, "extensions"))
655+
end
656+
end
657+
587658
describe "creating follow up migrations" do
588659
setup do
589660
on_exit(fn ->
@@ -614,6 +685,37 @@ defmodule AshPostgres.MigrationGeneratorTest do
614685
:ok
615686
end
616687

688+
test "when renaming an attribute of an index, it is properly renamed without modifying the attribute" do
689+
defposts do
690+
identities do
691+
identity(:title, [:foobar])
692+
end
693+
694+
attributes do
695+
uuid_primary_key(:id)
696+
attribute(:foobar, :string, public?: true)
697+
end
698+
end
699+
700+
defdomain([Post])
701+
702+
send(self(), {:mix_shell_input, :yes?, true})
703+
704+
AshPostgres.MigrationGenerator.generate(Domain,
705+
snapshot_path: "test_snapshots_path",
706+
migration_path: "test_migration_path",
707+
quiet: true,
708+
format: false
709+
)
710+
711+
assert [_file1, file2] =
712+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
713+
|> Enum.reject(&String.contains?(&1, "extensions"))
714+
715+
contents = File.read!(file2)
716+
refute contents =~ "modify"
717+
end
718+
617719
test "when renaming an index, it is properly renamed" do
618720
defposts do
619721
postgres do

0 commit comments

Comments
 (0)