Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions lib/migration_generator/migration_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down