diff --git a/lib/mix/tasks/ash_sqlite.install.ex b/lib/mix/tasks/ash_sqlite.install.ex index 1d2eaa2..9d508f5 100644 --- a/lib/mix/tasks/ash_sqlite.install.ex +++ b/lib/mix/tasks/ash_sqlite.install.ex @@ -2,6 +2,7 @@ defmodule Mix.Tasks.AshSqlite.Install do @moduledoc "Installs AshSqlite. Should be run with `mix igniter.install ash_sqlite`" @shortdoc @moduledoc require Igniter.Code.Common + require Igniter.Code.Function use Igniter.Mix.Task def igniter(igniter, _argv) do @@ -41,14 +42,22 @@ defmodule Mix.Tasks.AshSqlite.Install do 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)}, #{inspect(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, :database], otp_app) do + if Igniter.Project.Config.configures_key?(zipper, otp_app, [repo, :url]) do zipper else patterns = [ @@ -68,19 +77,61 @@ defmodule Mix.Tasks.AshSqlite.Install do |> Igniter.Code.Common.move_to_cursor_match_in_scope(patterns) |> case do {:ok, zipper} -> - zipper - |> Igniter.Project.Config.modify_configuration_code( - [repo, :pool_size], - otp_app, - quote do - String.to_integer(System.get_env("POOL_SIZE") || "10") - end - ) + case Igniter.Code.Function.move_to_function_call_in_current_scope( + zipper, + :=, + 2, + fn call -> + Igniter.Code.Function.argument_matches_pattern?( + call, + 0, + {:database_url, _, ctx} when is_atom(ctx) + ) + end + ) do + {:ok, _zipper} -> + zipper + |> Igniter.Project.Config.modify_configuration_code( + [repo, :url], + otp_app, + {:database_url, [], nil} + ) + |> Igniter.Project.Config.modify_configuration_code( + [repo, :pool_size], + otp_app, + Sourceror.parse_string!(""" + String.to_integer(System.get_env("POOL_SIZE") || "10") + """) + ) + |> then(&{:ok, &1}) + + _ -> + 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 - config #{inspect(otp_app)}, #{inspect(repo)}, + 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 """) diff --git a/mix.exs b/mix.exs index ec9ac1d..bb78f70 100644 --- a/mix.exs +++ b/mix.exs @@ -128,7 +128,7 @@ defmodule AshSqlite.MixProject do {:jason, "~> 1.0"}, {:ash, ash_version("~> 3.1 and >= 3.1.7")}, {:ash_sql, ash_sql_version("~> 0.2 and >= 0.2.20")}, - {:igniter, "~> 0.3"}, + {:igniter, "~> 0.3 and >= 0.3.11"}, {:simple_sat, ">= 0.0.0", only: [:dev, :test]}, {:git_ops, "~> 2.5", only: [:dev, :test]}, {:ex_doc, "~> 0.22", only: [:dev, :test], runtime: false}, diff --git a/mix.lock b/mix.lock index 789c845..293098a 100644 --- a/mix.lock +++ b/mix.lock @@ -23,12 +23,12 @@ "finch": {:hex, :finch, "0.18.0", "944ac7d34d0bd2ac8998f79f7a811b21d87d911e77a786bc5810adb75632ada4", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "69f5045b042e531e53edc2574f15e25e735b522c37e2ddb766e15b979e03aa65"}, "git_cli": {:hex, :git_cli, "0.3.0", "a5422f9b95c99483385b976f5d43f7e8233283a47cda13533d7c16131cb14df5", [:mix], [], "hexpm", "78cb952f4c86a41f4d3511f1d3ecb28edb268e3a7df278de2faa1bd4672eaf9b"}, "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"}, + "glob_ex": {:hex, :glob_ex, "0.1.8", "f7ef872877ca2ae7a792ab1f9ff73d9c16bf46ecb028603a8a3c5283016adc07", [:mix], [], "hexpm", "9e39d01729419a60a937c9260a43981440c43aa4cadd1fa6672fecd58241c464"}, "hpax": {:hex, :hpax, "1.0.0", "28dcf54509fe2152a3d040e4e3df5b265dcb6cb532029ecbacf4ce52caea3fd2", [:mix], [], "hexpm", "7f1314731d711e2ca5fdc7fd361296593fc2542570b3105595bb0bc6d0fad601"}, - "igniter": {:hex, :igniter, "0.3.9", "2a3c80e3d5a0f3758670eaa7658fe6334633dab3fd9bca9aae69802f8282a0b3", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:inflex, "~> 2.0", [hex: :inflex, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_options, "~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:owl, "~> 0.9", [hex: :owl, repo: "hexpm", optional: false]}, {:rewrite, "~> 0.9", [hex: :rewrite, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.4", [hex: :sourceror, repo: "hexpm", optional: false]}, {:spitfire, ">= 0.1.3 and < 1.0.0-0", [hex: :spitfire, repo: "hexpm", optional: false]}, {:ucwidth, "~> 0.2", [hex: :ucwidth, repo: "hexpm", optional: false]}], "hexpm", "2a5b8618a0aef8e5a545d05d389ba20fc5b0b4b8a6c45cf4f900890c263c7fdc"}, + "igniter": {:hex, :igniter, "0.3.11", "1039c38eaf4e3c677712d097e18be96231479c3e9d7d8fd9c04397b1f6d3601d", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:inflex, "~> 2.0", [hex: :inflex, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_options, "~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:owl, "~> 0.9", [hex: :owl, repo: "hexpm", optional: false]}, {:rewrite, "~> 0.9", [hex: :rewrite, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.4", [hex: :sourceror, repo: "hexpm", optional: false]}, {:spitfire, ">= 0.1.3 and < 1.0.0-0", [hex: :spitfire, repo: "hexpm", optional: false]}, {:ucwidth, "~> 0.2", [hex: :ucwidth, repo: "hexpm", optional: false]}], "hexpm", "1381104faaa5c51b2cc540f185e72d3bbab0ea3eb57348c50af4440c7e469491"}, "inflex": {:hex, :inflex, "2.1.0", "a365cf0821a9dacb65067abd95008ca1b0bb7dcdd85ae59965deef2aa062924c", [:mix], [], "hexpm", "14c17d05db4ee9b6d319b0bff1bdf22aa389a25398d1952c7a0b5f3d93162dd8"}, "iterex": {:hex, :iterex, "0.1.2", "58f9b9b9a22a55cbfc7b5234a9c9c63eaac26d276b3db80936c0e1c60355a5a6", [:mix], [], "hexpm", "2e103b8bcc81757a9af121f6dc0df312c9a17220f302b1193ef720460d03029d"}, - "jason": {:hex, :jason, "1.4.3", "d3f984eeb96fe53b85d20e0b049f03e57d075b5acda3ac8d465c969a2536c17b", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "9a90e868927f7c777689baa16d86f4d0e086d968db5c05d917ccff6d443e58a3"}, + "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"}, "libgraph": {:hex, :libgraph, "0.16.0", "3936f3eca6ef826e08880230f806bfea13193e49bf153f93edcf0239d4fd1d07", [:mix], [], "hexpm", "41ca92240e8a4138c30a7e06466acc709b0cbb795c643e9e17174a178982d6bf"}, "makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"}, "makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},