From 0dc1f204ed9f8c51bb096b13812da97ebc2b89ba Mon Sep 17 00:00:00 2001 From: transhapHigsn Date: Tue, 1 Dec 2020 22:13:00 +0530 Subject: [PATCH 1/2] chore: create migration file name with pattern timestamp_table.json and create table.version.json to track latest file Signed-off-by: transhapHigsn --- .../migration_generator.ex | 37 +++++++++++++++++-- test/migration_generator_test.exs | 15 ++++++-- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index 07270f2c..fef962ce 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -309,20 +309,36 @@ 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 +953,20 @@ 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..af5a567e 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -79,16 +79,25 @@ 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 From 19b3ccf2ea573e171b653e7a3d1a57c4561dbe00 Mon Sep 17 00:00:00 2001 From: transhapHigsn Date: Tue, 1 Dec 2020 22:21:17 +0530 Subject: [PATCH 2/2] chore: format changes Signed-off-by: transhapHigsn --- lib/migration_generator/migration_generator.ex | 10 ++++++---- test/migration_generator_test.exs | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index fef962ce..ed0e064f 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -333,12 +333,13 @@ defmodule AshPostgres.MigrationGenerator do snapshot_folder |> Path.join(snapshot.table <> ".version.json") - version_binary = snapshot_to_binary(%{ - latest_version: snapshot_file - }) + version_binary = + snapshot_to_binary(%{ + latest_version: snapshot_file + }) + File.mkdir_p(Path.dirname(version_file)) File.write!(version_file, version_binary, []) - end) end @@ -955,6 +956,7 @@ defmodule AshPostgres.MigrationGenerator do # get name of latest version file. version_file = Path.join(folder, snapshot.table <> ".version.json") + file = if File.exists?(version_file) do file_content = diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index af5a567e..63e18e47 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -90,6 +90,7 @@ defmodule AshPostgres.MigrationGeneratorTest do test "the snapshots contain valid json" do 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!)