From 515387371d3ae8defe87445b82ee7b62d4576ac7 Mon Sep 17 00:00:00 2001 From: "construct.d" Date: Mon, 23 Mar 2026 20:21:17 +0000 Subject: [PATCH 1/3] Add database connection check to install and update tasks --- lib/mix/tasks/phoenix_kit.install.ex | 4 ++ lib/mix/tasks/phoenix_kit.update.ex | 4 ++ .../install/db_connection_check.ex | 47 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 lib/phoenix_kit/install/db_connection_check.ex diff --git a/lib/mix/tasks/phoenix_kit.install.ex b/lib/mix/tasks/phoenix_kit.install.ex index be555df80..2c36bf6b2 100644 --- a/lib/mix/tasks/phoenix_kit.install.ex +++ b/lib/mix/tasks/phoenix_kit.install.ex @@ -52,6 +52,7 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do BasicConfiguration, BrowserPipelineIntegration, CssIntegration, + DbConnectionCheck, DemoFiles, EndpointIntegration, LayoutConfig, @@ -171,6 +172,9 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do # Second pass: Configuration exists, safe to start app and complete installation Process.put(:phoenix_kit_config_status, :ok) + # Simple database check - must succeed to continue + DbConnectionCheck.check!() + # Run standard igniter process result = super(argv) diff --git a/lib/mix/tasks/phoenix_kit.update.ex b/lib/mix/tasks/phoenix_kit.update.ex index 221d421dc..d57d36509 100644 --- a/lib/mix/tasks/phoenix_kit.update.ex +++ b/lib/mix/tasks/phoenix_kit.update.ex @@ -88,6 +88,7 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do BasicConfiguration, Common, CssIntegration, + DbConnectionCheck, IgniterHelpers, ObanConfig, RateLimiterConfig @@ -202,6 +203,9 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do # Store config status in Process dictionary for igniter/1 to read Process.put(:phoenix_kit_config_status, :ok) + # Simple database check - must succeed to continue + DbConnectionCheck.check!() + # Cap the Ecto pool to 2 connections so we don't saturate PgBouncer # when the production app is already running. # diff --git a/lib/phoenix_kit/install/db_connection_check.ex b/lib/phoenix_kit/install/db_connection_check.ex new file mode 100644 index 000000000..23b10c0b6 --- /dev/null +++ b/lib/phoenix_kit/install/db_connection_check.ex @@ -0,0 +1,47 @@ +defmodule PhoenixKit.Install.DbConnectionCheck do + @moduledoc """ + Simple database connection check for PhoenixKit installation. + """ + alias PhoenixKit.Config + + @doc """ + Check if database is reachable. Returns true if connected, false otherwise. + """ + def check? do + case Config.get(:repo) do + {:ok, repo} when is_atom(repo) -> + check_repo?(repo) + + _ -> + false + end + end + + @doc """ + Check DB connection and exit with error if not connected. + """ + def check! do + unless check?() do + Mix.shell().error(""" + ❌ Cannot connect to database. + + Please ensure: + 1. PostgreSQL is running + 2. Database exists (run: mix ecto.create) + 3. Configuration in config/dev.exs is correct + """) + + exit({:shutdown, 1}) + end + end + + defp check_repo?(repo) do + with true <- Code.ensure_loaded?(repo), + true <- function_exported?(repo, :__adapter__, 0), + {:ok, %{rows: [[1]]}} <- repo.query("SELECT 1", [], log: false) do + true + else + _ -> false + end + end +end From 6f28089e6937d283061b51a1ffb33eb85ba5e2f8 Mon Sep 17 00:00:00 2001 From: "construct.d" Date: Mon, 23 Mar 2026 20:34:16 +0000 Subject: [PATCH 2/3] Fix connection check --- lib/mix/tasks/phoenix_kit.install.ex | 6 +++--- lib/mix/tasks/phoenix_kit.status.ex | 13 +++++++++++++ lib/mix/tasks/phoenix_kit.update.ex | 7 ++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/mix/tasks/phoenix_kit.install.ex b/lib/mix/tasks/phoenix_kit.install.ex index 2c36bf6b2..e590f413d 100644 --- a/lib/mix/tasks/phoenix_kit.install.ex +++ b/lib/mix/tasks/phoenix_kit.install.ex @@ -172,12 +172,12 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do # Second pass: Configuration exists, safe to start app and complete installation Process.put(:phoenix_kit_config_status, :ok) - # Simple database check - must succeed to continue - DbConnectionCheck.check!() - # Run standard igniter process result = super(argv) + # Simple database check - must succeed to continue + DbConnectionCheck.check!() + # After igniter is done, handle interactive migration MigrationStrategy.handle_interactive_migration_after_config(elem(opts, 1)) diff --git a/lib/mix/tasks/phoenix_kit.status.ex b/lib/mix/tasks/phoenix_kit.status.ex index bfb1243db..69c2668a1 100644 --- a/lib/mix/tasks/phoenix_kit.status.ex +++ b/lib/mix/tasks/phoenix_kit.status.ex @@ -42,6 +42,7 @@ defmodule Mix.Tasks.PhoenixKit.Status do alias PhoenixKit.Config alias PhoenixKit.Install.Common + alias PhoenixKit.Install.DbConnectionCheck alias PhoenixKit.Migrations.Postgres @impl Mix.Task @@ -70,6 +71,18 @@ defmodule Mix.Tasks.PhoenixKit.Status do {:ok, _} = Application.ensure_all_started(:ecto_sql) {:ok, _} = Application.ensure_all_started(:phoenix_kit) + # Ensure repo is started before DB check + case get_repo_with_fallback() do + nil -> + :ok + + repo -> + ensure_repo_started(repo) + end + + # Simple database check - must succeed to continue + DbConnectionCheck.check!() + {opts, _argv, _errors} = OptionParser.parse(argv, switches: @switches, aliases: @aliases) prefix = opts[:prefix] || "public" diff --git a/lib/mix/tasks/phoenix_kit.update.ex b/lib/mix/tasks/phoenix_kit.update.ex index d57d36509..295a71a6a 100644 --- a/lib/mix/tasks/phoenix_kit.update.ex +++ b/lib/mix/tasks/phoenix_kit.update.ex @@ -203,9 +203,6 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do # Store config status in Process dictionary for igniter/1 to read Process.put(:phoenix_kit_config_status, :ok) - # Simple database check - must succeed to continue - DbConnectionCheck.check!() - # Cap the Ecto pool to 2 connections so we don't saturate PgBouncer # when the production app is already running. # @@ -225,6 +222,10 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do Application.put_env(:phoenix_kit, :update_mode, true) Mix.Task.run("app.start") + + # Simple database check - must succeed to continue + DbConnectionCheck.check!() + result = super(argv) post_igniter_tasks(elem(opts, 0)) From 2bd4ec7323dc13bd3841f6a23042f3a5cd75e1b3 Mon Sep 17 00:00:00 2001 From: "construct.d" Date: Mon, 23 Mar 2026 20:56:36 +0000 Subject: [PATCH 3/3] Fix warning --- lib/phoenix_kit/install/db_connection_check.ex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/phoenix_kit/install/db_connection_check.ex b/lib/phoenix_kit/install/db_connection_check.ex index 23b10c0b6..f17646cc4 100644 --- a/lib/phoenix_kit/install/db_connection_check.ex +++ b/lib/phoenix_kit/install/db_connection_check.ex @@ -4,6 +4,8 @@ defmodule PhoenixKit.Install.DbConnectionCheck do """ alias PhoenixKit.Config + @dialyzer {:nowarn_function, check!: 0} + @doc """ Check if database is reachable. Returns true if connected, false otherwise. """ @@ -20,6 +22,7 @@ defmodule PhoenixKit.Install.DbConnectionCheck do @doc """ Check DB connection and exit with error if not connected. """ + @spec check!() :: no_return() def check! do unless check?() do Mix.shell().error("""