diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index 07270f2c..ed0e064f 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -309,20 +309,37 @@ defmodule AshPostgres.MigrationGenerator do Enum.each(snapshots, fn snapshot -> snapshot_binary = snapshot_to_binary(snapshot) - snapshot_file = + snapshot_table = "#{timestamp()}_#{snapshot.table}" + + snapshot_folder = if tenant? do opts.snapshot_path |> Path.join(repo_name) |> Path.join("tenants") - |> Path.join(snapshot.table <> ".json") else opts.snapshot_path |> Path.join(repo_name) - |> Path.join(snapshot.table <> ".json") end + snapshot_file = + snapshot_folder + |> Path.join(snapshot_table <> ".json") + File.mkdir_p(Path.dirname(snapshot_file)) File.write!(snapshot_file, snapshot_binary, []) + + # create a new version file to track latest migration file + version_file = + snapshot_folder + |> Path.join(snapshot.table <> ".version.json") + + version_binary = + snapshot_to_binary(%{ + latest_version: snapshot_file + }) + + File.mkdir_p(Path.dirname(version_file)) + File.write!(version_file, version_binary, []) end) end @@ -937,7 +954,21 @@ defmodule AshPostgres.MigrationGenerator do Path.join(opts.snapshot_path, repo_name) end - file = Path.join(folder, snapshot.table <> ".json") + # get name of latest version file. + version_file = Path.join(folder, snapshot.table <> ".version.json") + + file = + if File.exists?(version_file) do + file_content = + version_file + |> File.read!() + |> Jason.decode!(keys: :atoms!) + + file_content.latest_version + else + version_file = Path.join(folder, snapshot.table <> ".json") + version_file + end if File.exists?(file) do file diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index 68700358..63e18e47 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -79,16 +79,26 @@ defmodule AshPostgres.MigrationGeneratorTest do end test "it creates a snapshot for each resource" do - assert File.exists?(Path.join(["test_snapshots_path", "test_repo", "posts.json"])) + assert File.exists?(Path.wildcard("test_snapshots_path/test_repo/*_posts.json")) + assert File.exists?(Path.join(["test_snapshots_path", "test_repo", "posts.version.json"])) end test "the snapshots can be loaded" do - assert File.exists?(Path.join(["test_snapshots_path", "test_repo", "posts.json"])) + assert File.exists?(Path.wildcard("test_snapshots_path/test_repo/*_posts.json")) + assert File.exists?(Path.join(["test_snapshots_path", "test_repo", "posts.version.json"])) end test "the snapshots contain valid json" do - assert File.read!(Path.join(["test_snapshots_path", "test_repo", "posts.json"])) + assert File.exists?(Path.join(["test_snapshots_path", "test_repo", "posts.version.json"])) + + assert File.read!(Path.wildcard("test_snapshots_path/test_repo/*_posts.json")) |> Jason.decode!(keys: :atoms!) + + version_file_content = + File.read!(Path.join(["test_snapshots_path", "test_repo", "posts.version.json"])) + |> Jason.decode!(keys: :atoms!) + + assert Map.get(version_file_content, :latest_version, nil) != nil end test "the migration creates the table" do