feat: add create? and drop? callbacks to AshPostgres.Repo (#143)

Co-authored-by: Zach Daniel <zach@zachdaniel.dev>
This commit is contained in:
bcksl 2024-03-27 23:05:14 +02:00 committed by GitHub
parent 3ad3f5f5d6
commit b66427e9bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 3 deletions

View file

@ -34,7 +34,9 @@ defmodule Mix.Tasks.AshPostgres.Create do
def run(args) do
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
repos = AshPostgres.MixHelpers.repos!(opts, args)
repos =
AshPostgres.MixHelpers.repos!(opts, args)
|> Enum.filter(fn repo -> repo.create? end)
repo_args =
Enum.flat_map(repos, fn repo ->

View file

@ -44,7 +44,9 @@ defmodule Mix.Tasks.AshPostgres.Drop do
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
opts = Keyword.merge(@default_opts, opts)
repos = AshPostgres.MixHelpers.repos!(opts, args)
repos =
AshPostgres.MixHelpers.repos!(opts, args)
|> Enum.filter(fn repo -> repo.drop? end)
repo_args =
Enum.flat_map(repos, fn repo ->

View file

@ -71,6 +71,10 @@ defmodule AshPostgres.Repo do
@doc "Allows overriding a given migration type for *all* fields, for example if you wanted to always use :timestamptz for :utc_datetime fields"
@callback override_migration_type(atom) :: atom
@doc "Should the repo should be created by `mix ash_postgres.create`?"
@callback create?() :: boolean
@doc "Should the repo should be dropped by `mix ash_postgres.drop`?"
@callback drop?() :: boolean
defmacro __using__(opts) do
quote bind_quoted: [opts: opts] do
@ -91,6 +95,9 @@ defmodule AshPostgres.Repo do
def migrations_path, do: nil
def default_prefix, do: "public"
def override_migration_type(type), do: type
def create?, do: true
def drop?, do: true
def transaction!(fun) do
case fun.() do
@ -226,7 +233,9 @@ defmodule AshPostgres.Repo do
all_tenants: 0,
tenant_migrations_path: 0,
default_prefix: 0,
override_migration_type: 1
override_migration_type: 1,
create?: 0,
drop?: 0
end
end
end