Skip to content

Commit

Permalink
improvement: automatically set min_pg_version where possible
Browse files Browse the repository at this point in the history
improvement: use a notice to suggest configuring `min_pg_version`

doing this instead of showing a prompt, to reduce initial sign-up friction

fixes #430
  • Loading branch information
zachdaniel committed Dec 25, 2024
1 parent 7e0c205 commit 3da10a1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
29 changes: 21 additions & 8 deletions lib/igniter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down Expand Up @@ -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
Expand Down
53 changes: 52 additions & 1 deletion lib/mix/tasks/ash_postgres.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down

0 comments on commit 3da10a1

Please sign in to comment.