diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index 0406d89..63b5848 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -474,7 +474,10 @@ defmodule AshPostgres.MigrationGenerator do |> Enum.uniq_by(&{&1.table, &1.schema}) |> Enum.map(fn snapshot -> if opts.no_shell? do - Enum.find(existing_snapshots, &(&1.table == snapshot.table)) + Enum.find( + existing_snapshots, + &(&1.table == snapshot.table && &1.schema == snapshot.schema) + ) else get_existing_snapshot(snapshot, opts) end @@ -910,7 +913,12 @@ defmodule AshPostgres.MigrationGenerator do |> Path.join(repo_name) end - snapshot_file = Path.join(snapshot_folder, "#{snapshot.table}/#{timestamp()}.json") + snapshot_file = + if snapshot.schema do + Path.join(snapshot_folder, "#{snapshot.schema}.#{snapshot.table}/#{timestamp()}.json") + else + Path.join(snapshot_folder, "#{snapshot.table}/#{timestamp()}.json") + end File.mkdir_p(Path.dirname(snapshot_file)) File.write!(snapshot_file, snapshot_binary, []) @@ -2212,7 +2220,18 @@ defmodule AshPostgres.MigrationGenerator do |> Path.join(repo_name) end - snapshot_folder = Path.join(folder, snapshot.table) + snapshot_folder = + if snapshot.schema do + schema_dir = Path.join(folder, "#{snapshot.schema}.#{snapshot.table}") + + if File.dir?(schema_dir) do + schema_dir + else + Path.join(folder, snapshot.table) + end + else + Path.join(folder, snapshot.table) + end if File.exists?(snapshot_folder) do snapshot_folder @@ -2246,12 +2265,27 @@ defmodule AshPostgres.MigrationGenerator do end defp get_old_snapshot(folder, snapshot) do - old_snapshot_file = Path.join(folder, "#{snapshot.table}.json") - # This is adapter code for the old version, where migrations were stored in a flat directory - if File.exists?(old_snapshot_file) do - old_snapshot_file - |> File.read!() - |> load_snapshot() + schema_file = + if snapshot.schema do + old_snapshot_file = Path.join(folder, "#{snapshot.schema}.#{snapshot.table}.json") + + if File.exists?(old_snapshot_file) do + old_snapshot_file + |> File.read!() + |> load_snapshot() + end + end + + if schema_file do + schema_file + else + old_snapshot_file = Path.join(folder, "#{snapshot.table}.json") + # This is adapter code for the old version, where migrations were stored in a flat directory + if File.exists?(old_snapshot_file) do + old_snapshot_file + |> File.read!() + |> load_snapshot() + end end end diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index e1ba37e..ce97eb6 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -222,7 +222,7 @@ defmodule AshPostgres.MigrationGeneratorTest do test "the migration sets up resources correctly" do # the snapshot exists and contains valid json - assert File.read!(Path.wildcard("test_snapshots_path/test_repo/posts/*.json")) + assert File.read!(Path.wildcard("test_snapshots_path/test_repo/example.posts/*.json")) |> Jason.decode!(keys: :atoms!) assert [file] = Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")