diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index 1177760a..84c07b47 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -3109,11 +3109,39 @@ defmodule AshPostgres.MigrationGenerator do not is_nil(Map.get(attribute, :references)) and !(attribute.references.multitenancy && attribute.references.multitenancy.strategy == :context && - (is_nil(multitenancy) || multitenancy.strategy == :attribute)) + (is_nil(multitenancy) || multitenancy.strategy == :attribute)) and + !(multitenancy && multitenancy.strategy == :context && + attribute.references.multitenancy && + attribute.references.multitenancy.strategy == :context) end - def get_existing_snapshot(snapshot, opts) do - folder = get_snapshot_folder(snapshot, opts) + defp get_snapshot_folder(snapshot, opts) do + base = + opts + |> snapshot_path(snapshot.repo) + |> Path.join(repo_name(snapshot.repo)) + + if snapshot.multitenancy.strategy == :context do + Path.join(base, "tenants") + else + base + end + end + + defp get_alternate_snapshot_folder(snapshot, opts) do + base = + opts + |> snapshot_path(snapshot.repo) + |> Path.join(repo_name(snapshot.repo)) + + if snapshot.multitenancy.strategy == :context do + base + else + Path.join(base, "tenants") + end + end + + defp load_snapshot_from_folder(folder, snapshot, opts) do snapshot_path = get_snapshot_path(snapshot, folder) if File.exists?(snapshot_path) do @@ -3138,16 +3166,13 @@ defmodule AshPostgres.MigrationGenerator do end end - defp get_snapshot_folder(snapshot, opts) do - if snapshot.multitenancy.strategy == :context do - opts - |> snapshot_path(snapshot.repo) - |> Path.join(repo_name(snapshot.repo)) - |> Path.join("tenants") + def get_existing_snapshot(snapshot, opts) do + result = load_snapshot_from_folder(get_snapshot_folder(snapshot, opts), snapshot, opts) + + if is_nil(result) do + load_snapshot_from_folder(get_alternate_snapshot_folder(snapshot, opts), snapshot, opts) else - opts - |> snapshot_path(snapshot.repo) - |> Path.join(repo_name(snapshot.repo)) + result end end diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index c1c939b6..7b40fb89 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -1264,6 +1264,90 @@ defmodule AshPostgres.MigrationGeneratorTest do end end + describe "removing :context multitenancy" do + setup %{snapshot_path: snapshot_path, migration_path: migration_path} do + defposts do + multitenancy do + strategy(:context) + end + + attributes do + uuid_primary_key(:id) + attribute(:title, :string, public?: true) + end + end + + defcomments do + multitenancy do + strategy(:context) + end + + attributes do + uuid_primary_key(:id) + attribute(:post_id, :uuid, public?: true) + end + + relationships do + belongs_to(:post, Post, public?: true) + end + end + + defdomain([Post, Comment]) + + AshPostgres.MigrationGenerator.generate(Domain, + snapshot_path: snapshot_path, + migration_path: migration_path, + quiet: true, + format: false, + auto_name: true + ) + + :ok + end + + test "when removing :context multitenancy, no invalid drop constraint is generated", %{ + snapshot_path: snapshot_path, + migration_path: migration_path + } do + defposts do + attributes do + uuid_primary_key(:id) + attribute(:title, :string, public?: true) + end + end + + defcomments do + attributes do + uuid_primary_key(:id) + attribute(:post_id, :uuid, public?: true) + end + + relationships do + belongs_to(:post, Post, public?: true) + end + end + + defdomain([Post, Comment]) + + AshPostgres.MigrationGenerator.generate(Domain, + snapshot_path: snapshot_path, + migration_path: migration_path, + quiet: true, + format: false, + auto_name: true + ) + + migration_files = Enum.sort(Path.wildcard("#{migration_path}/**/*_migrate_resources*.exs")) |> Enum.reject(&String.contains?(&1, "extensions")) + + all_contents = Enum.map_join(migration_files, "", &File.read!/1) + + [up_contents] =Regex.run(~r/def up do(.+)def down do/s, all_contents, capture: :all_but_first) + + + refute up_contents =~ ~S{drop constraint(:comments, "comments_post_id_fkey")} + end + end + describe "creating follow up migrations" do setup %{snapshot_path: snapshot_path, migration_path: migration_path} do :ok