From 38bf5558676d3dd49223516029927dfe9e7459cd Mon Sep 17 00:00:00 2001 From: zimt28 <1764689+zimt28@users.noreply.github.com> Date: Wed, 13 Jan 2021 02:47:17 +0100 Subject: [PATCH] feat: Add check_migrated option to migration generator (#40) (#43) --- .formatter.exs | 1 + .../migration_generator.ex | 13 ++++++++- .../tasks/ash_postgres.generate_migrations.ex | 2 ++ test/migration_generator_test.exs | 28 +++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/.formatter.exs b/.formatter.exs index 114d838..04156da 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -12,6 +12,7 @@ locals_without_parens = [ ] [ + import_deps: [:ash], inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], locals_without_parens: locals_without_parens, export: [ diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index 7ce82ae..0d86c7b 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -16,11 +16,20 @@ defmodule AshPostgres.MigrationGenerator do quiet: false, format: true, dry_run: false, + check_generated: false, drop_columns: false def generate(apis, opts \\ []) do apis = List.wrap(apis) - opts = struct(__MODULE__, opts) + + opts = + case struct(__MODULE__, opts) do + %{check_generated: true} = opts -> + %{opts | dry_run: true} + + opts -> + opts + end {tenant_snapshots, snapshots} = apis @@ -68,6 +77,8 @@ defmodule AshPostgres.MigrationGenerator do :ok operations -> + if opts.check_generated, do: exit({:shutdown, 1}) + operations |> sort_operations() |> streamline() diff --git a/lib/mix/tasks/ash_postgres.generate_migrations.ex b/lib/mix/tasks/ash_postgres.generate_migrations.ex index 380451a..fe17690 100644 --- a/lib/mix/tasks/ash_postgres.generate_migrations.ex +++ b/lib/mix/tasks/ash_postgres.generate_migrations.ex @@ -16,6 +16,7 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do * `quiet` - messages for file creations will not be printed * `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_migrated` - no files are created, returns an exit(1) code if the current snapshots and resources don't fit #### Snapshots @@ -77,6 +78,7 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do quiet: :boolean, no_format: :boolean, dry_run: :boolean, + check_migrated: :boolean, drop_columns: :boolean ] ) diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index 0bdd859..2df5689 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -388,4 +388,32 @@ defmodule AshPostgres.MigrationGeneratorTest do ~S[add :views, :integer] end end + + describe "--check_migrated option" do + setup do + defposts do + attributes do + uuid_primary_key :id + attribute(:title, :string) + end + end + + defapi([Post]) + + [api: Api] + end + + test "returns code(1) if snapshots and resources don't fit", %{api: api} do + assert catch_exit( + AshPostgres.MigrationGenerator.generate(api, + snapshot_path: "test_snapshot_path", + migration_path: "test_migration_path", + check_generated: true + ) + ) == {:shutdown, 1} + + refute File.exists?(Path.wildcard("test_migration_path2/**/*_migrate_resources*.exs")) + refute File.exists?(Path.wildcard("test_snapshots_path2/test_repo/posts/*.json")) + end + end end