Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dev flag for generating migrations #496

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
148 changes: 113 additions & 35 deletions lib/migration_generator/migration_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule AshPostgres.MigrationGenerator do
format: true,
dry_run: false,
check: false,
dev: false,
snapshots_only: false,
dont_drop_columns: false

Expand Down Expand Up @@ -452,6 +453,23 @@ defmodule AshPostgres.MigrationGenerator do
:ok

operations ->
dev_migrations = get_dev_migrations(opts, tenant?, repo)

if !opts.dev and dev_migrations != [] do
if opts.check do
Mix.shell().error("""
Generated migrations are from dev mode.

Generate migrations without `--dev` flag.
""")

exit({:shutdown, 1})
else
remove_dev_migrations(dev_migrations, tenant?, repo, opts)
remove_dev_snapshots(snapshots, opts)
end
end

if opts.check do
Mix.shell().error("""
Migrations would have been generated, but the --check flag was provided.
Expand Down Expand Up @@ -491,6 +509,55 @@ defmodule AshPostgres.MigrationGenerator do
end)
end

defp get_dev_migrations(opts, tenant?, repo) do
opts
|> migration_path(repo, tenant?)
|> File.ls()
|> case do
{:error, _error} -> []
{:ok, migrations} -> Enum.filter(migrations, &String.contains?(&1, "_dev.exs"))
end
end

defp remove_dev_migrations(dev_migrations, tenant?, repo, opts) do
version = dev_migrations |> Enum.min() |> String.split("_") |> hd()
test_env = [env: [{"MIX_ENV", "test"}]]
System.cmd("mix", ["ash_postgres.rollback", "--to", version])
System.cmd("mix", ["ash_postgres.rollback", "--to", version], test_env)

if tenant? do
Enum.each(repo.all_tenants(), fn tenant ->
args = ["ash_postgres.rollback", "--to", version, "--prefix", tenant]
System.cmd("mix", args)
System.cmd("mix", args, test_env)
end)
end

dev_migrations
|> Enum.each(fn migration_name ->
opts
|> migration_path(repo, tenant?)
|> Path.join(migration_name)
|> File.rm!()
end)
end

def remove_dev_snapshots(snapshots, opts) do
Enum.each(snapshots, fn snapshot ->
folder = get_snapshot_folder(snapshot, opts)
snapshot_path = get_snapshot_path(snapshot, folder)

snapshot_path
|> File.ls!()
|> Enum.filter(&String.contains?(&1, "_dev.json"))
|> Enum.each(fn snapshot_name ->
snapshot_path
|> Path.join(snapshot_name)
|> File.rm!()
end)
end)
end

defp split_into_migrations(operations) do
operations
|> Enum.split_with(fn
Expand Down Expand Up @@ -960,7 +1027,7 @@ defmodule AshPostgres.MigrationGenerator do

migration_file =
migration_path
|> Path.join(migration_name <> ".exs")
|> Path.join(migration_name <> "#{if opts.dev, do: "_dev"}.exs")

module_name =
if tenant? do
Expand Down Expand Up @@ -1054,20 +1121,25 @@ defmodule AshPostgres.MigrationGenerator do
|> Path.join(repo_name)
end

dev = if opts.dev, do: "_dev"

snapshot_file =
if snapshot.schema do
Path.join(snapshot_folder, "#{snapshot.schema}.#{snapshot.table}/#{timestamp()}.json")
Path.join(
snapshot_folder,
"#{snapshot.schema}.#{snapshot.table}/#{timestamp()}#{dev}.json"
)
else
Path.join(snapshot_folder, "#{snapshot.table}/#{timestamp()}.json")
Path.join(snapshot_folder, "#{snapshot.table}/#{timestamp()}#{dev}.json")
end

File.mkdir_p(Path.dirname(snapshot_file))
create_file(snapshot_file, snapshot_binary, force: true)

old_snapshot_folder = Path.join(snapshot_folder, "#{snapshot.table}.json")
old_snapshot_folder = Path.join(snapshot_folder, "#{snapshot.table}#{dev}.json")

if File.exists?(old_snapshot_folder) do
new_snapshot_folder = Path.join(snapshot_folder, "#{snapshot.table}/initial.json")
new_snapshot_folder = Path.join(snapshot_folder, "#{snapshot.table}/initial#{dev}.json")
File.rename(old_snapshot_folder, new_snapshot_folder)
end
end)
Expand Down Expand Up @@ -2610,43 +2682,22 @@ defmodule AshPostgres.MigrationGenerator do
end

def get_existing_snapshot(snapshot, opts) do
repo_name = snapshot.repo |> Module.split() |> List.last() |> Macro.underscore()

folder =
if snapshot.multitenancy.strategy == :context do
opts
|> snapshot_path(snapshot.repo)
|> Path.join(repo_name)
|> Path.join("tenants")
else
opts
|> snapshot_path(snapshot.repo)
|> Path.join(repo_name)
end

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
folder = get_snapshot_folder(snapshot, opts)
snapshot_path = get_snapshot_path(snapshot, folder)

if File.exists?(snapshot_folder) do
snapshot_folder
if File.exists?(snapshot_path) do
snapshot_path
|> File.ls!()
|> Enum.filter(&String.match?(&1, ~r/^\d{14}\.json$/))
|> Enum.filter(
&(String.match?(&1, ~r/^\d{14}\.json$/) or
(opts.dev and String.match?(&1, ~r/^\d{14}\_dev.json$/)))
)
|> case do
[] ->
get_old_snapshot(folder, snapshot)

snapshot_files ->
snapshot_folder
snapshot_path
|> Path.join(Enum.max(snapshot_files))
|> File.read!()
|> load_snapshot()
Expand All @@ -2656,6 +2707,33 @@ 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")
else
opts
|> snapshot_path(snapshot.repo)
|> Path.join(repo_name(snapshot.repo))
end
end

defp get_snapshot_path(snapshot, folder) do
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
end

defp get_old_snapshot(folder, snapshot) do
schema_file =
if snapshot.schema do
Expand Down
6 changes: 4 additions & 2 deletions lib/mix/tasks/ash_postgres.generate_migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do
* `no-format` - files that are created will not be formatted with the code formatter
* `dry-run` - no files are created, instead the new migration is printed
* `check` - no files are created, returns an exit(1) code if the current snapshots and resources don't fit
* 'dev' - dev files are created
* `snapshots-only` - no migrations are generated, only snapshots are stored
* `concurrent-indexes` - new identities will be run concurrently and in a separate migration (like concurrent custom indexes)

Expand Down Expand Up @@ -97,6 +98,7 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do
no_format: :boolean,
dry_run: :boolean,
check: :boolean,
dev: :boolean,
dont_drop_columns: :boolean,
concurrent_indexes: :boolean
]
Expand All @@ -110,9 +112,9 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do
|> Keyword.delete(:no_format)
|> Keyword.put_new(:name, name)

if !opts[:name] && !opts[:dry_run] && !opts[:check] && !opts[:snapshots_only] do
if !opts[:name] && !opts[:dry_run] && !opts[:check] && !opts[:snapshots_only] && !opts[:dev] do
IO.warn("""
Name must be provided when generating migrations, unless `--dry-run` or `--check` is also provided.
Name must be provided when generating migrations, unless `--dry-run` or `--check` or `--dev` is also provided.
Using an autogenerated name will be deprecated in a future release.

Please provide a name. for example:
Expand Down
96 changes: 96 additions & 0 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,102 @@ defmodule AshPostgres.MigrationGeneratorTest do
end
end

describe "--dev option" do
setup do
on_exit(fn ->
File.rm_rf!("test_snapshots_path")
File.rm_rf!("test_migration_path")
File.rm_rf!("test_tenant_migration_path")
end)
end

test "generates dev migration" do
defposts do
attributes do
uuid_primary_key(:id)
attribute(:title, :string, public?: true)
end
end

defdomain([Post])

AshPostgres.MigrationGenerator.generate(Domain,
snapshot_path: "test_snapshots_path",
migration_path: "test_migration_path",
dev: true
)

assert [dev_file] =
Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
|> Enum.reject(&String.contains?(&1, "extensions"))

assert String.contains?(dev_file, "_dev.exs")
contents = File.read!(dev_file)

AshPostgres.MigrationGenerator.generate(Domain,
snapshot_path: "test_snapshots_path",
migration_path: "test_migration_path"
)

assert [file] =
Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
|> Enum.reject(&String.contains?(&1, "extensions"))

refute String.contains?(file, "_dev.exs")

assert contents == File.read!(file)
end

test "generates dev migration for tenant" do
defposts do
postgres do
schema("example")
end

attributes do
uuid_primary_key(:id)
attribute(:title, :string, public?: true, primary_key?: true, allow_nil?: false)
end

multitenancy do
strategy(:context)
end
end

defdomain([Post])

send(self(), {:mix_shell_input, :yes?, true})

AshPostgres.MigrationGenerator.generate(Domain,
snapshot_path: "test_snapshots_path",
migration_path: "test_migration_path",
tenant_migration_path: "test_tenant_migration_path",
dev: true
)

assert [dev_file] =
Enum.sort(Path.wildcard("test_tenant_migration_path/**/*_migrate_resources*.exs"))
|> Enum.reject(&String.contains?(&1, "extensions"))

assert String.contains?(dev_file, "_dev.exs")
contents = File.read!(dev_file)

AshPostgres.MigrationGenerator.generate(Domain,
snapshot_path: "test_snapshots_path",
migration_path: "test_migration_path",
tenant_migration_path: "test_tenant_migration_path"
)

assert [file] =
Path.wildcard("test_tenant_migration_path/**/*_migrate_resources*.exs")
|> Enum.reject(&String.contains?(&1, "extensions"))

refute String.contains?(file, "_dev.exs")

assert contents == File.read!(file)
end
end

describe "references" do
setup do
on_exit(fn ->
Expand Down
Loading