From b66427e9bd29d3ee12f9590b494adb1612c92cf9 Mon Sep 17 00:00:00 2001 From: bcksl <121328003+bcksl@users.noreply.github.com> Date: Wed, 27 Mar 2024 23:05:14 +0200 Subject: [PATCH] feat: add `create?` and `drop?` callbacks to `AshPostgres.Repo` (#143) Co-authored-by: Zach Daniel --- lib/mix/tasks/ash_postgres.create.ex | 4 +++- lib/mix/tasks/ash_postgres.drop.ex | 4 +++- lib/repo.ex | 11 ++++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/mix/tasks/ash_postgres.create.ex b/lib/mix/tasks/ash_postgres.create.ex index e4de634..9ac3e9f 100644 --- a/lib/mix/tasks/ash_postgres.create.ex +++ b/lib/mix/tasks/ash_postgres.create.ex @@ -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 -> diff --git a/lib/mix/tasks/ash_postgres.drop.ex b/lib/mix/tasks/ash_postgres.drop.ex index b8ac91d..16c3777 100644 --- a/lib/mix/tasks/ash_postgres.drop.ex +++ b/lib/mix/tasks/ash_postgres.drop.ex @@ -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 -> diff --git a/lib/repo.ex b/lib/repo.ex index 78e3bb8..6437158 100644 --- a/lib/repo.ex +++ b/lib/repo.ex @@ -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