feat: Add check_migrated option to migration generator (#40) (#43)

This commit is contained in:
zimt28 2021-01-13 02:47:17 +01:00 committed by GitHub
parent 3ac292617e
commit 38bf555867
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 1 deletions

View file

@ -12,6 +12,7 @@ locals_without_parens = [
] ]
[ [
import_deps: [:ash],
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
locals_without_parens: locals_without_parens, locals_without_parens: locals_without_parens,
export: [ export: [

View file

@ -16,11 +16,20 @@ defmodule AshPostgres.MigrationGenerator do
quiet: false, quiet: false,
format: true, format: true,
dry_run: false, dry_run: false,
check_generated: false,
drop_columns: false drop_columns: false
def generate(apis, opts \\ []) do def generate(apis, opts \\ []) do
apis = List.wrap(apis) 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} = {tenant_snapshots, snapshots} =
apis apis
@ -68,6 +77,8 @@ defmodule AshPostgres.MigrationGenerator do
:ok :ok
operations -> operations ->
if opts.check_generated, do: exit({:shutdown, 1})
operations operations
|> sort_operations() |> sort_operations()
|> streamline() |> streamline()

View file

@ -16,6 +16,7 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do
* `quiet` - messages for file creations will not be printed * `quiet` - messages for file creations will not be printed
* `no_format` - files that are created will not be formatted with the code formatter * `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 * `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 #### Snapshots
@ -77,6 +78,7 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do
quiet: :boolean, quiet: :boolean,
no_format: :boolean, no_format: :boolean,
dry_run: :boolean, dry_run: :boolean,
check_migrated: :boolean,
drop_columns: :boolean drop_columns: :boolean
] ]
) )

View file

@ -388,4 +388,32 @@ defmodule AshPostgres.MigrationGeneratorTest do
~S[add :views, :integer] ~S[add :views, :integer]
end end
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 end