improvement: add mix ash_postgres.install (mix igniter.install ash_postgres)

This commit is contained in:
Zach Daniel 2024-06-21 19:49:47 -04:00
parent 46279527bf
commit 6b1f6bae2a
9 changed files with 375 additions and 37 deletions

View file

@ -1,2 +1,2 @@
erlang 26.2.2
elixir 1.16.2
erlang 27.0
elixir 1.17.0

View file

@ -401,6 +401,8 @@ defmodule AshPostgres.DataLayer do
A postgres data layer that leverages Ecto's postgres capabilities.
"""
require Igniter.Code.Common
use Spark.Dsl.Extension,
sections: @sections,
verifiers: [
@ -2919,6 +2921,24 @@ defmodule AshPostgres.DataLayer do
end
end
if Code.ensure_loaded?(Igniter) do
def install(igniter, module, Ash.Resource, path, _argv) do
table_name =
module
|> Module.split()
|> List.last()
|> Macro.underscore()
repo = Igniter.Code.Module.module_name("Repo")
igniter
|> Spark.Igniter.set_option(Ash.Resource, path, [:postgres, :table], table_name)
|> Spark.Igniter.set_option(Ash.Resource, path, [:postgres, :repo], repo)
end
def install(igniter, _, _, _), do: igniter
end
@impl true
def rollback(resource, term) do
AshPostgres.DataLayer.Info.repo(resource, :mutate).rollback(term)

View file

@ -45,6 +45,7 @@ defmodule AshPostgres.MigrationGenerator do
repos =
(snapshots ++ tenant_snapshots)
|> Enum.map(& &1.repo)
|> Enum.concat(find_repos())
|> Enum.uniq()
Mix.shell().info("\nExtension Migrations: ")
@ -55,6 +56,14 @@ defmodule AshPostgres.MigrationGenerator do
create_migrations(snapshots, opts, false)
end
defp find_repos do
Mix.Project.config()[:app]
|> Application.get_env(:ecto_repos, [])
|> Enum.filter(fn repo ->
Spark.implements_behaviour?(repo, AshPostgres.Repo)
end)
end
@doc """
A work in progress utility for getting snapshots.

View file

@ -1,6 +1,6 @@
defmodule AshPostgres.Mix.Helpers do
@moduledoc false
def domains!(opts, args) do
def domains!(opts, args, error_on_no_domains? \\ true) do
apps =
if apps_paths = Mix.Project.apps_paths() do
apps_paths |> Map.keys() |> Enum.sort()
@ -30,7 +30,11 @@ defmodule AshPostgres.Mix.Helpers do
|> Enum.map(&ensure_compiled(&1, args))
|> case do
[] ->
raise "must supply the --domains argument, or set `config :my_app, ash_domains: [...]` in config"
if error_on_no_domains? do
raise "must supply the --domains argument, or set `config :my_app, ash_domains: [...]` in config"
else
[]
end
domains ->
domains
@ -38,43 +42,54 @@ defmodule AshPostgres.Mix.Helpers do
end
def repos!(opts, args) do
domains = domains!(opts, args)
if opts[:domains] && opts[:domains] != "" do
domains = domains!(opts, args)
resources =
domains
|> Enum.flat_map(&Ash.Domain.Info.resources/1)
|> Enum.filter(&(Ash.DataLayer.data_layer(&1) == AshPostgres.DataLayer))
resources =
domains
|> Enum.flat_map(&Ash.Domain.Info.resources/1)
|> Enum.filter(&(Ash.DataLayer.data_layer(&1) == AshPostgres.DataLayer))
|> case do
[] ->
raise """
No resources with `data_layer: AshPostgres.DataLayer` found in the domains #{Enum.map_join(domains, ",", &inspect/1)}.
Must be able to find at least one resource with `data_layer: AshPostgres.DataLayer`.
"""
resources ->
resources
end
resources
|> Enum.flat_map(
&[
AshPostgres.DataLayer.Info.repo(&1, :read),
AshPostgres.DataLayer.Info.repo(&1, :mutate)
]
)
|> Enum.uniq()
|> case do
[] ->
raise """
No resources with `data_layer: AshPostgres.DataLayer` found in the domains #{Enum.map_join(domains, ",", &inspect/1)}.
No repos could be found configured on the resources in the domains: #{Enum.map_join(domains, ",", &inspect/1)}
Must be able to find at least one resource with `data_layer: AshPostgres.DataLayer`.
At least one resource must have a repo configured.
The following resources were found with `data_layer: AshPostgres.DataLayer`:
#{Enum.map_join(resources, "\n", &"* #{inspect(&1)}")}
"""
resources ->
resources
repos ->
repos
end
resources
|> Enum.flat_map(
&[AshPostgres.DataLayer.Info.repo(&1, :read), AshPostgres.DataLayer.Info.repo(&1, :mutate)]
)
|> Enum.uniq()
|> case do
[] ->
raise """
No repos could be found configured on the resources in the domains: #{Enum.map_join(domains, ",", &inspect/1)}
At least one resource must have a repo configured.
The following resources were found with `data_layer: AshPostgres.DataLayer`:
#{Enum.map_join(resources, "\n", &"* #{inspect(&1)}")}
"""
repos ->
repos
else
Mix.Project.config()[:app]
|> Application.get_env(:ecto_repos, [])
|> Enum.filter(fn repo ->
Spark.implements_behaviour?(repo, AshPostgres.Repo)
end)
end
end

View file

@ -98,7 +98,14 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do
]
)
domains = AshPostgres.Mix.Helpers.domains!(opts, args)
domains = AshPostgres.Mix.Helpers.domains!(opts, args, false)
if Enum.empty?(domains) do
IO.warn("""
No domains found, so no resource-related migrations will be generated.
Pass the `--domains` option or configure `config :your_app, ash_domains: [...]`
""")
end
opts =
opts

View file

@ -0,0 +1,287 @@
defmodule Mix.Tasks.AshPostgres.Install do
@moduledoc "Installs AshPostgres. Should be run with `mix igniter.install ash_postgres`"
@shortdoc @moduledoc
require Igniter.Code.Common
use Igniter.Mix.Task
def igniter(igniter, _argv) do
repo = Igniter.Code.Module.module_name("Repo")
otp_app = Igniter.Project.Application.app_name()
igniter
|> Igniter.Project.Formatter.import_dep(:ash_postgres)
|> setup_repo_module(otp_app, repo)
|> configure_config(otp_app, repo)
|> configure_dev(otp_app, repo)
|> configure_test(otp_app, repo)
|> configure_runtime(repo, otp_app)
|> Igniter.Project.Application.add_new_child(repo)
|> Igniter.add_task("ash.codegen", ["install_ash_postgres"])
end
defp configure_config(igniter, otp_app, repo) do
Igniter.Project.Config.configure(
igniter,
"config.exs",
otp_app,
[:ecto_repos],
[repo],
updater: fn zipper ->
Igniter.Code.List.prepend_new_to_list(
zipper,
repo
)
end
)
end
defp configure_runtime(igniter, otp_app, repo) do
default_runtime = """
import Config
if config_env() == :prod do
database_url =
System.get_env("DATABASE_URL") ||
raise \"\"\"
environment variable DATABASE_URL is missing.
For example: ecto://USER:PASS@HOST/DATABASE
\"\"\"
config #{inspect(otp_app)}, Helpdesk.Repo,
url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")
end
"""
igniter
|> Igniter.create_or_update_elixir_file("config/runtime.exs", default_runtime, fn zipper ->
if Igniter.Project.Config.configures?(zipper, [repo, :url], otp_app) do
zipper
else
patterns = [
"""
if config_env() == :prod do
__cursor__()
end
""",
"""
if :prod == config_env() do
__cursor__()
end
"""
]
zipper
|> Igniter.Code.Common.move_to_cursor_match_in_scope(patterns)
|> case do
{:ok, zipper} ->
case Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:=,
2,
fn call ->
Igniter.Code.Function.argument_matches_predicate?(
call,
0,
&match?({:database_url, _, Elixir}, &1)
)
end
) do
{:ok, zipper} ->
zipper
|> Igniter.Project.Config.modify_configuration_code(
[repo, :url],
otp_app,
{:database_url, [], Elixir}
)
|> Igniter.Project.Config.modify_configuration_code(
[repo, :pool_size],
otp_app,
quote do
String.to_integer(System.get_env("POOL_SIZE") || "10")
end
)
|> then(&{:ok, &1})
:error ->
Igniter.Code.Common.add_code(zipper, """
database_url =
System.get_env("DATABASE_URL") ||
raise \"\"\"
environment variable DATABASE_URL is missing.
For example: ecto://USER:PASS@HOST/DATABASE
\"\"\"
config #{inspect(otp_app)}, Helpdesk.Repo,
url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")
""")
end
:error ->
Igniter.Code.Common.add_code(zipper, """
if config_env() == :prod do
database_url =
System.get_env("DATABASE_URL") ||
raise \"\"\"
environment variable DATABASE_URL is missing.
For example: ecto://USER:PASS@HOST/DATABASE
\"\"\"
config #{inspect(otp_app)}, Helpdesk.Repo,
url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")
end
""")
end
end
end)
end
defp configure_dev(igniter, otp_app, repo) do
igniter
|> Igniter.Project.Config.configure_new("dev.exs", otp_app, [repo, :username], "postgres")
|> Igniter.Project.Config.configure_new("dev.exs", otp_app, [repo, :password], "postgres")
|> Igniter.Project.Config.configure_new("dev.exs", otp_app, [repo, :hostname], "localhost")
|> Igniter.Project.Config.configure_new(
"dev.exs",
otp_app,
[repo, :database],
"#{otp_app}_dev"
)
|> Igniter.Project.Config.configure_new(
"dev.exs",
otp_app,
[repo, :show_sensitive_data_on_connection_error],
true
)
|> Igniter.Project.Config.configure_new("dev.exs", otp_app, [repo, :pool_size], 10)
end
defp configure_test(igniter, otp_app, repo) do
database =
{:<<>>, [],
[
"#{otp_app}_test",
{:"::", [],
[
{{:., [], [Kernel, :to_string]}, [from_interpolation: true],
[
{{:., [], [{:__aliases__, [alias: false], [:System]}, :get_env]}, [],
["MIX_TEST_PARTITION"]}
]},
{:binary, [], Elixir}
]}
]}
|> Sourceror.to_string()
|> Sourceror.parse_string!()
igniter
|> Igniter.Project.Config.configure_new("test.exs", otp_app, [repo, :username], "postgres")
|> Igniter.Project.Config.configure_new("test.exs", otp_app, [repo, :password], "postgres")
|> Igniter.Project.Config.configure_new("test.exs", otp_app, [repo, :hostname], "localhost")
|> Igniter.Project.Config.configure_new(
"test.exs",
otp_app,
[repo, :database],
{:code, database}
)
|> Igniter.Project.Config.configure_new(
"test.exs",
otp_app,
[repo, :pool],
Ecto.Adapters.SQL.Sandbox
)
|> Igniter.Project.Config.configure_new("test.exs", otp_app, [repo, :pool_size], 10)
end
defp setup_repo_module(igniter, otp_app, repo) do
path = Igniter.Code.Module.proper_location(repo)
default_repo_contents =
"""
defmodule #{inspect(repo)} do
use AshPostgres.Repo, otp_app: #{inspect(otp_app)}
def installed_extensions do
# Add extensions here, and the migration generator will install them.
["ash-functions"]
end
end
"""
igniter
|> Igniter.create_or_update_elixir_file(path, default_repo_contents, fn zipper ->
zipper
|> set_otp_app(otp_app)
|> Sourceror.Zipper.top()
|> use_ash_postgres_instead_of_ecto()
|> Sourceror.Zipper.top()
|> add_installed_extensions_function()
|> Sourceror.Zipper.top()
|> remove_adapter_option()
end)
end
defp use_ash_postgres_instead_of_ecto(zipper) do
with {:ok, zipper} <- Igniter.Code.Module.move_to_module_using(zipper, Ecto.Repo),
{:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Ecto.Repo),
{:ok, zipper} <-
Igniter.Code.Function.update_nth_argument(zipper, 0, fn zipper ->
{:ok, Igniter.Code.Common.replace_code(zipper, AshPostgres.Repo)}
end) do
zipper
else
_ ->
zipper
end
end
defp remove_adapter_option(zipper) do
with {:ok, zipper} <- Igniter.Code.Module.move_to_module_using(zipper, AshPostgres.Repo),
{:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, AshPostgres.Repo),
{:ok, zipper} <-
Igniter.Code.Function.update_nth_argument(zipper, 1, fn values_zipper ->
Igniter.Code.Keyword.remove_keyword_key(values_zipper, :adapter)
end) do
zipper
else
_ ->
zipper
end
end
defp set_otp_app(zipper, otp_app) do
with {:ok, zipper} <- Igniter.Code.Module.move_to_module_using(zipper, AshPostgres.Repo),
{:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, AshPostgres.Repo),
{:ok, zipper} <-
Igniter.Code.Function.update_nth_argument(zipper, 0, fn zipper ->
{:ok, Igniter.Code.Common.replace_code(zipper, AshPostgres.Repo)}
end),
{:ok, zipper} <-
Igniter.Code.Function.update_nth_argument(zipper, 1, fn values_zipper ->
values_zipper
|> Igniter.Code.Keyword.set_keyword_key(:otp_app, otp_app, fn x -> {:ok, x} end)
end) do
zipper
else
_ ->
zipper
end
end
defp add_installed_extensions_function(zipper) do
case Igniter.Code.Module.move_to_module_using(zipper, AshPostgres.Repo) do
{:ok, zipper} ->
Igniter.Code.Common.add_code(zipper, """
def installed_extensions do
# Add extensions here, and the migration generator will install them.
["ash-functions"]
end
""")
_ ->
zipper
end
end
end

View file

@ -23,7 +23,7 @@
"git_ops": {:hex, :git_ops, "2.6.1", "cc7799a68c26cf814d6d1a5121415b4f5bf813de200908f930b27a2f1fe9dad5", [:mix], [{:git_cli, "~> 0.2", [hex: :git_cli, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "ce62d07e41fe993ec22c35d5edb11cf333a21ddaead6f5d9868fcb607d42039e"},
"glob_ex": {:hex, :glob_ex, "0.1.7", "eae6b6377147fb712ac45b360e6dbba00346689a87f996672fe07e97d70597b1", [:mix], [], "hexpm", "decc1c21c0c73df3c9c994412716345c1692477b9470e337f628a7e08da0da6a"},
"hpax": {:hex, :hpax, "0.2.0", "5a58219adcb75977b2edce5eb22051de9362f08236220c9e859a47111c194ff5", [:mix], [], "hexpm", "bea06558cdae85bed075e6c036993d43cd54d447f76d8190a8db0dc5893fa2f1"},
"igniter": {:hex, :igniter, "0.1.7", "dd5674a6ec2ec1728a690d4915a5a430ba213b45cad86f5710cf9076e6274d0e", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:req, "~> 0.4", [hex: :req, repo: "hexpm", optional: false]}, {:rewrite, "~> 0.9", [hex: :rewrite, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.3", [hex: :sourceror, repo: "hexpm", optional: false]}, {:spitfire, ">= 0.1.1 and < 1.0.0-0", [hex: :spitfire, repo: "hexpm", optional: false]}], "hexpm", "08d95b91fee280be305887b128dcf10a4ff55a8ab72a452489b1529e8644a8c8"},
"igniter": {:hex, :igniter, "0.2.1", "a11026b484eb6cd5fa13a84fc14f770411e38ba7824a3bb45142fba02dc1a9a3", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:req, "~> 0.4", [hex: :req, repo: "hexpm", optional: false]}, {:rewrite, "~> 0.9", [hex: :rewrite, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.3", [hex: :sourceror, repo: "hexpm", optional: false]}, {:spitfire, ">= 0.1.3 and < 1.0.0-0", [hex: :spitfire, repo: "hexpm", optional: false]}], "hexpm", "3521fdf18baa6e6045a6765f1725ae24fac0d9deb435b484f762066a8f534d01"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"libgraph": {:hex, :libgraph, "0.16.0", "3936f3eca6ef826e08880230f806bfea13193e49bf153f93edcf0239d4fd1d07", [:mix], [], "hexpm", "41ca92240e8a4138c30a7e06466acc709b0cbb795c643e9e17174a178982d6bf"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
@ -43,7 +43,7 @@
"sobelow": {:hex, :sobelow, "0.13.0", "218afe9075904793f5c64b8837cc356e493d88fddde126a463839351870b8d1e", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "cd6e9026b85fc35d7529da14f95e85a078d9dd1907a9097b3ba6ac7ebbe34a0d"},
"sourceror": {:hex, :sourceror, "1.3.0", "70ab9e8bf6df085a1effba4b49ad621b7153b065f69ef6cdb82e6088f2026029", [:mix], [], "hexpm", "1794c3ceeca4eb3f9437261721e4d9cbf846d7c64c7aee4f64062b18d5ce1eac"},
"spark": {:hex, :spark, "2.2.3", "d41c45f0d7e8289499bf104173543be41ee1d8d213cb7503e9328b30751f2f13", [:mix], [{:igniter, ">= 0.1.7 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.2", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "401dec157c8b88634c27f241f4e374476081c577f51d2b08512f2c681c25a852"},
"spitfire": {:hex, :spitfire, "0.1.2", "49b85d59c170d671e7e49649f62f6fe0771743a61bc42bd7a203f98f322d99e2", [:mix], [], "hexpm", "21f04cf02df601d75e1551e393ee5c927e3986fbb7598f3e59f71d4ca544fd9b"},
"spitfire": {:hex, :spitfire, "0.1.3", "7ea0f544005dfbe48e615ed90250c9a271bfe126914012023fd5e4b6b82b7ec7", [:mix], [], "hexpm", "d53b5107bcff526a05c5bb54c95e77b36834550affd5830c9f58760e8c543657"},
"splode": {:hex, :splode, "0.2.4", "71046334c39605095ca4bed5d008372e56454060997da14f9868534c17b84b53", [:mix], [], "hexpm", "ca3b95f0d8d4b482b5357954fec857abd0fa3ea509d623334c1328e7382044c2"},
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
"stream_data": {:hex, :stream_data, "1.1.1", "fd515ca95619cca83ba08b20f5e814aaf1e5ebff114659dc9731f966c9226246", [:mix], [], "hexpm", "45d0cd46bd06738463fd53f22b70042dbb58c384bb99ef4e7576e7bb7d3b8c8c"},

View file

@ -1409,7 +1409,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
assert [file] = Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
assert File.read!(file) =~
~S[references(:users, column: :secondary_id, with: [org_id: :org_id\], match: :full, name: "user_things1_user_id_fkey", type: :uuid, prefix: "public")]
~S{references(:users, column: :secondary_id, with: [org_id: :org_id], match: :full, name: "user_things1_user_id_fkey", type: :uuid, prefix: "public")}
assert File.read!(file) =~
~S[references(:users, column: :id, name: "user_things2_user_id_fkey", type: :uuid, prefix: "public")]