ash-project / ash_postgres

The PostgreSQL data layer for Ash Framework
https://hexdocs.pm/ash_postgres
MIT License
134 stars 73 forks source link

Error when attempting to install ash_postgres using Igniter #345

Closed sevenseacat closed 2 months ago

sevenseacat commented 2 months ago

Describe the bug

I'm testing out AshPostgres/Igniter in preparation for the imminent 3.1 release, and have run into an issue when attempting to install AshPostgres into a brand new Phoenix app.

To Reproduce

Running mix igniter.install ash_postgres@github:ash-project/ash_postgres errors with the following:

.: ~/P/t/cat : main (0eeaedd) : 7:39pm :.
$ mix igniter.install ash_postgres@github:ash-project/ash_postgres

Fetch Required Dependencies:
Update: mix.exs

     ...|
33 33   |  defp deps do
34 34   |    [
   35 + |      {:ash_postgres, [github: "ash-project/ash_postgres", override: true]},
35 36   |      {:spark, "~> 2.0"},
36 37   |      {:picosat_elixir, "~> 0.2"},
     ...|

The following dependencies should be installed before continuing. Modify mix.exs and install? [Yn] y
running mix deps.get
* Updating ash_postgres (https://github.com/ash-project/ash_postgres.git)
origin/HEAD set to main
Resolving Hex dependencies...
Resolution completed in 0.208s
New:
  ash_sql 0.2.10
Unchanged:
  bandit 1.5.5
  castore 1.0.8
  comparable 1.0.0
  db_connection 2.7.0
  decimal 2.1.1
  dns_cluster 0.1.3
  ecto 3.11.2
  ecto_sql 3.11.3
  elixir_make 0.8.4
  esbuild 0.8.1
  ets 0.9.0
  expo 0.5.2
  file_system 1.0.0
  finch 0.18.0
  floki 0.36.2
  gettext 0.24.0
  glob_ex 0.1.7
  hpax 0.2.0
  jason 1.4.3
  libgraph 0.16.0
  mime 2.0.6
  mint 1.6.2
  nimble_options 1.1.1
  nimble_pool 1.1.0
  phoenix 1.7.14
  phoenix_ecto 4.6.2
  phoenix_html 4.1.1
  phoenix_live_dashboard 0.8.4
  phoenix_live_reload 1.5.3
  phoenix_live_view 1.0.0-rc.6
  phoenix_pubsub 2.1.3
  phoenix_template 1.0.4
  picosat_elixir 0.2.3
  plug 1.16.1
  plug_crypto 2.1.0
  postgrex 0.18.0
  reactor 0.8.4
  req 0.5.2
  rewrite 0.10.5
  sourceror 1.4.0
  spark 2.2.7
  spitfire 0.1.3
  splode 0.2.4
  stream_data 1.1.1
  swoosh 1.16.9
  tailwind 0.2.3
  telemetry 1.2.1
  telemetry_metrics 1.0.0
  telemetry_poller 1.1.0
  thousand_island 1.3.5
  typable 0.3.0
  websock 0.5.3
  websock_adapter 0.5.6

...
<a bunch of compilation warnings omitted>
...

==> ash_postgres
Compiling 45 files (.ex)
Generated ash_postgres app
==> cat
Generated cat app
** (CaseClauseError) no case clause matching: #Sourceror.Zipper<
  #...
  {:__block__, [], [{:use, [trailing_comments: [], leading_comments: [], end_of_expression: 
[newlines: 1, line: 4, column: 36], line: 2, column: 3], [AshPostgres.Repo, [{{:__block__, 
[trailing_comments: [], leading_comments: [], format: :keyword, line: 3, column: 5], [:otp_app]}, 
{:__block__, [trailing_comments: [], leading_comments: [], line: 3, column: 14], [:cat]}}]]}, {:def, 
[trailing_comments: [], leading_comments: [], end_of_expression: [newlines: 1, line: 4, column: 4], 
do: [line: 1, column: 26], end: [line: 4, column: 1], line: 1, column: 1], [{:installed_extensions, 
[trailing_comments: [], leading_comments: [], line: 1, column: 5], nil}, [{{:__block__, 
[trailing_comments: [], leading_comments: [], line: 1, column: 26], [:do]}, {:__block__, 
[trailing_comments: [], leading_comments: [%{line: 2, text: "# Add extensions here, and the 
migration generator will install them.", column: 3, next_eol_count: 1, previous_eol_count: 1}], 
end_of_expression: [newlines: 1, line: 3, column: 20], closing: [line: 3, column: 19], line: 3, 
column: 3], [[{:__block__, [trailing_comments: [], leading_comments: [], delimiter: "\"", line: 3, 
column: 4], ["ash-functions"]}]]}}]]}]}
>
    (igniter 0.2.6) lib/igniter/code/module.ex:48: Igniter.Code.Module.find_and_update_module/3
    (igniter 0.2.6) lib/igniter/code/module.ex:9: Igniter.Code.Module.find_and_update_or_create_module/5
    (ash_postgres 2.0.12) lib/mix/tasks/ash_postgres.install.ex:13: Mix.Tasks.AshPostgres.Install.igniter/2
    (elixir 1.17.0) lib/enum.ex:2531: Enum."-reduce/3-lists^foldl/2-0-"/3
    (igniter 0.2.6) lib/igniter/util/install.ex:73: Igniter.Util.Install.install/3
    (mix 1.17.0) lib/mix/task.ex:495: anonymous fn/3 in Mix.Task.run_task/5
    (mix 1.17.0) lib/mix/cli.ex:96: Mix.CLI.run_task/2
    /Users/rebecca/.asdf/installs/elixir/1.17.0-otp-27/bin/mix:2: (file)

It looks like it might be trying to calculate what to do with the Repo module, which looks like this:

defmodule Cat.Repo do
  use Ecto.Repo,
    otp_app: :cat,
    adapter: Ecto.Adapters.Postgres
end

The sha of ash_postgres being included is d293d0f3024ccbebe495f7868daa10517f2ba2c4.

Expected behavior

Igniter correctly calculates the changes that need to be made to repo.ex, which after modification I think should be:

defmodule Cat.Repo do
  use AshPostgres.Repo, otp_app: :cat

  def installed_extensions do
    ["ash-functions"]
  end
end

Runtime