Skip to content

feat: dev flag for generating migrations #496

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ if Mix.env() == :test do
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox

config :ash_postgres, AshPostgres.DevTestRepo,
username: "postgres",
password: "postgres",
database: "ash_postgres_dev_test",
hostname: "localhost",
migration_primary_key: [name: :id, type: :binary_id],
pool: Ecto.Adapters.SQL.Sandbox

# sobelow_skip ["Config.Secrets"]
config :ash_postgres, AshPostgres.TestRepo, password: "postgres"

Expand All @@ -54,7 +62,7 @@ if Mix.env() == :test do
migration_primary_key: [name: :id, type: :binary_id]

config :ash_postgres,
ecto_repos: [AshPostgres.TestRepo, AshPostgres.TestNoSandboxRepo],
ecto_repos: [AshPostgres.TestRepo, AshPostgres.DevTestRepo, AshPostgres.TestNoSandboxRepo],
ash_domains: [
AshPostgres.Test.Domain,
AshPostgres.MultitenancyTest.Domain,
Expand Down
139 changes: 104 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,46 @@ 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()
Mix.Task.reenable("ash_postgres.rollback")
Mix.Task.run("ash_postgres.rollback", ["--repo", inspect(repo), "--to", version])

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 +1018,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 +1112,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 @@ -2623,43 +2686,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
folder = get_snapshot_folder(snapshot, opts)
snapshot_path = get_snapshot_path(snapshot, folder)

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
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 @@ -2669,6 +2711,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
Loading
Loading