diff --git a/lib/igniter.ex b/lib/igniter.ex index 75b636e6..2e00bb7b 100644 --- a/lib/igniter.ex +++ b/lib/igniter.ex @@ -3,8 +3,8 @@ if Code.ensure_loaded?(Igniter) do @moduledoc "Codemods and utilities for working with AshPostgres & Igniter" @doc false - def default_repo_contents(otp_app, name, opts \\ []) do - min_pg_version = get_min_pg_version(name, opts) + def default_repo_contents(otp_app, opts \\ []) do + min_pg_version = opts[:min_pg_version] || Version.parse!("16.0.0") """ use AshPostgres.Repo, otp_app: #{inspect(otp_app)} @@ -88,12 +88,25 @@ if Code.ensure_loaded?(Igniter) do otp_app = Igniter.Project.Application.app_name(igniter) igniter = - Igniter.Project.Module.create_module( - igniter, - repo, - default_repo_contents(otp_app, repo, opts), - opts - ) + if opts[:min_pg_version] do + igniter + else + Igniter.add_notice(igniter, """ + A `min_pg_version/0` function has been defined + in `#{inspect(repo)}` as `16.0.0`. + + You may wish to update this configuration. It should + be set to the lowest version that your application + expects to be run against. + """) + end + + Igniter.Project.Module.create_module( + igniter, + repo, + default_repo_contents(otp_app, opts), + opts + ) {igniter, repo} else diff --git a/lib/mix/tasks/ash_postgres.install.ex b/lib/mix/tasks/ash_postgres.install.ex index d59e31dd..d8942193 100644 --- a/lib/mix/tasks/ash_postgres.install.ex +++ b/lib/mix/tasks/ash_postgres.install.ex @@ -362,11 +362,40 @@ if Code.ensure_loaded?(Igniter) do end ) else + min_pg_version = get_min_pg_version() + + notice = + if min_pg_version do + """ + A `min_pg_version/0` function has been defined + in `#{inspect(repo)}` as `#{min_pg_version}`. + + This was based on running `postgres -V`. + + You may wish to update this configuration. It should + be set to the lowest version that your application + expects to be run against. + """ + else + """ + A `min_pg_version/0` function has been defined in + `#{inspect(repo)}` automatically. + + You may wish to update this configuration. It should + be set to the lowest version that your application + expects to be run against. + """ + end + Igniter.Project.Module.create_module( igniter, repo, - AshPostgres.Igniter.default_repo_contents(otp_app, repo, opts) + AshPostgres.Igniter.default_repo_contents( + otp_app, + Keyword.put(opts, :min_pg_version, min_pg_version) + ) ) + |> Igniter.add_notice(notice) end |> Igniter.Project.Module.find_and_update_module!( repo, @@ -382,6 +411,28 @@ if Code.ensure_loaded?(Igniter) do ) end + def get_min_pg_version do + case System.cmd("postgres", ["-V"]) do + {"postgres (PostgreSQL) " <> version_and_text, 0} -> + version_and_text + |> String.split(~r/\s+/, parts: 2, trim: true) + |> Enum.at(0) + |> String.split(".", trim: true) + |> case do + [major, minor, patch | _] -> Version.parse!("#{major}.#{minor}.#{patch}") + [major, minor] -> Version.parse!("#{major}.#{minor}.0") + [major] -> Version.parse!("#{major}.0.0") + _ -> nil + end + + _ -> + nil + end + rescue + _ -> + nil + 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),