ash-project / ash_sqlite

The SQLite data layer for Ash Framework.
https://hexdocs.pm/ash_sqlite
MIT License
14 stars 9 forks source link

`Ash.Igniter.codegen/1` fails when there are no configured domains. #83

Closed jimsynz closed 2 days ago

jimsynz commented 2 days ago

This would seem like it would happen quite often given that this task is run by both the ash_sqlite and ash_postgres installers.

jimsynz commented 2 days ago

Here's an example starting from zilch:

$ mix igniter.new wat
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/wat.ex
* creating test
* creating test/test_helper.exs
* creating test/wat_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd wat
    mix test

Run "mix help" for more commands.

$ mix deps.get
Resolving Hex dependencies...
Resolution completed in 0.097s
New:
  glob_ex 0.1.8
  igniter 0.3.40
  jason 1.4.4
  rewrite 0.10.5
  sourceror 1.6.0
  spitfire 0.1.3
* Getting igniter (Hex package)
mix i* Getting glob_ex (Hex package)
* Getting jason (Hex package)
* Getting rewrite (Hex package)
* Getting sourceror (Hex package)
* Getting spitfire (Hex package)
You have added/upgraded packages you could sponsor, run `mix hex.sponsor` to learn more

$ mix igniter.install ash
==> sourceror
Compiling 11 files (.ex)
Generated sourceror app
==> spitfire
Compiling 2 files (.erl)
Compiling 3 files (.ex)
Generated spitfire app
==> jason
Compiling 10 files (.ex)
Generated jason app
==> glob_ex
Compiling 4 files (.ex)
Generated glob_ex app
==> rewrite
Compiling 10 files (.ex)
Generated rewrite app
==> igniter
Compiling 34 files (.ex)
    warning: redefining module Mix.Tasks.Igniter.Install (current version loaded from /Users/jmshrtn/.asdf/installs/elixir/1.17.0/.mix/archives/igniter-0.3.39/igniter-0.3.39/ebin/Elixir.Mix.Tasks.Igniter.Install.beam)
    │
  1 │ defmodule Mix.Tasks.Igniter.Install do
    │ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ lib/mix/tasks/igniter.install.ex:1: Mix.Tasks.Igniter.Install (module)

Generated igniter app
==> wat
Compiling 1 file (.ex)
Generated wat app
fatal: not a git repository (or any of the parent directories): .git
Update: mix.exs

     ...|
23 23   |  defp deps do
24 24   |    [
   25 + |      {:ash, "~> 3.0"},
25 26   |      {:igniter, "~> 0.2"}
26 27   |      # {:dep_from_hexpm, "~> 0.3.0"},
     ...|

These dependencies should be installed before continuing. Modify mix.exs and install? [Yn] Y
running mix deps.get
Resolving Hex dependencies...
Resolution completed in 0.107s
New:
  ash 3.4.20
  decimal 2.1.1
  ecto 3.12.3
  ets 0.9.0
  iterex 0.1.2
  libgraph 0.16.0
  owl 0.11.0
  reactor 0.10.0
  spark 2.2.29
  splode 0.2.4
  stream_data 1.1.1
  telemetry 1.3.0
Unchanged:
  glob_ex 0.1.8
  igniter 0.3.40
  jason 1.4.4
  rewrite 0.10.5
  sourceror 1.6.0
  spitfire 0.1.3
* Getting ash (Hex package)
* Getting decimal (Hex package)
* Getting ecto (Hex package)
* Getting ets (Hex package)
* Getting owl (Hex package)
* Getting reactor (Hex package)
* Getting spark (Hex package)
* Getting splode (Hex package)
* Getting stream_data (Hex package)
* Getting telemetry (Hex package)
* Getting iterex (Hex package)
* Getting libgraph (Hex package)
You have added/upgraded packages you could sponsor, run `mix hex.sponsor` to learn more
    warning: redefining module Wat.MixProject (current version defined in memory)
    │
  1 │ defmodule Wat.MixProject do
    │ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ mix.exs:1: Wat.MixProject (module)

==> igniter
Compiling 34 files (.ex)
==> stream_data
Compiling 3 files (.ex)
Generated stream_data app
==> decimal
Compiling 4 files (.ex)
Generated decimal app
==> libgraph
Compiling 15 files (.ex)
Generated libgraph app
===> Analyzing applications...
===> Compiling telemetry
==> jason
Compiling 10 files (.ex)
==> splode
Compiling 5 files (.ex)
Generated splode app
==> ecto
Compiling 56 files (.ex)
Generated ecto app
==> ets
Compiling 7 files (.ex)
Generated ets app
==> iterex
Compiling 48 files (.ex)
Generated iterex app
==> spark
Compiling 36 files (.ex)
Generated spark app
==> reactor
Compiling 86 files (.ex)
Generated reactor app
==> owl
Compiling 19 files (.ex)
Generated owl app
==> ash
Compiling 497 files (.ex)
Generated ash app
==> wat

The following installer was found and executed: `ash.install`:
Update: .formatter.exs

1 1   |# Used by "mix format"
2 2   |[
3   - |  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
  3 + |  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
  4 + |  plugins: [Spark.Formatter],
  5 + |  import_deps: [:ash]
4 6   |]
5 7   |

Create: config/config.exs

1  | import Config
2  |
3  | config :ash,
4  |   include_embedded_source_by_default?: false,
5  |   default_page_type: :keyset,
6  |   policies: [no_filter_static_forbidden_reads?: false]
7  |
8  | config :spark,
9  |   formatter: [
10 |     remove_parens?: true,
11 |     "Ash.Resource": [
12 |       section_order: [
13 |         :resource,
14 |         :code_interface,
15 |         :actions,
16 |         :policies,
17 |         :pub_sub,
18 |         :preparations,
19 |         :changes,
20 |         :validations,
21 |         :multitenancy,
22 |         :attributes,
23 |         :relationships,
24 |         :calculations,
25 |         :aggregates,
26 |         :identities
27 |       ]
28 |     ],
29 |     "Ash.Domain": [section_order: [:resources, :policies, :authorization, :domain, :execution]]
30 |   ]
31 |

Proceed with changes? [Yn] Y

$ mix igniter.install ash_sqlite
===> Analyzing applications...
===> Compiling telemetry
==> jason
Compiling 10 files (.ex)
Generated jason app
==> igniter
Compiling 34 files (.ex)
    warning: redefining module Mix.Tasks.Igniter.Install (current version loaded from /Users/jmshrtn/.asdf/installs/elixir/1.17.0/.mix/archives/igniter-0.3.39/igniter-0.3.39/ebin/Elixir.Mix.Tasks.Igniter.Install.beam)
    │
  1 │ defmodule Mix.Tasks.Igniter.Install do
    │ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ lib/mix/tasks/igniter.install.ex:1: Mix.Tasks.Igniter.Install (module)

Generated igniter app
==> ash
Compiling 497 files (.ex)
Generated ash app
==> wat
Generated wat app
fatal: not a git repository (or any of the parent directories): .git
Update: mix.exs

     ...|
23 23   |  defp deps do
24 24   |    [
   25 + |      {:ash_sqlite, "~> 0.2"},
25 26   |      {:ash, "~> 3.0"},
26 27   |      {:igniter, "~> 0.2"}
     ...|

These dependencies should be installed before continuing. Modify mix.exs and install? [Yn] Y
running mix deps.get
Resolving Hex dependencies...
Resolution completed in 0.133s
New:
  ash_sql 0.2.32
  ash_sqlite 0.2.0
  cc_precompiler 0.1.10
  db_connection 2.7.0
  ecto_sql 3.12.0
  ecto_sqlite3 0.17.2
  elixir_make 0.8.4
  exqlite 0.24.2
Unchanged:
  ash 3.4.20
  decimal 2.1.1
  ecto 3.12.3
  ets 0.9.0
  glob_ex 0.1.8
  igniter 0.3.40
  iterex 0.1.2
  jason 1.4.4
  libgraph 0.16.0
  owl 0.11.0
  reactor 0.10.0
  rewrite 0.10.5
  sourceror 1.6.0
  spark 2.2.29
  spitfire 0.1.3
  splode 0.2.4
  stream_data 1.1.1
  telemetry 1.3.0
* Getting ash_sqlite (Hex package)
* Getting ash_sql (Hex package)
* Getting ecto_sql (Hex package)
* Getting ecto_sqlite3 (Hex package)
* Getting exqlite (Hex package)
* Getting cc_precompiler (Hex package)
* Getting db_connection (Hex package)
* Getting elixir_make (Hex package)
    warning: redefining module Wat.MixProject (current version defined in memory)
    │
  1 │ defmodule Wat.MixProject do
    │ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ mix.exs:1: Wat.MixProject (module)

==> igniter
    warning: redefining module Mix.Tasks.Igniter.Install (current version loaded from /Users/jmshrtn/.asdf/installs/elixir/1.17.0/.mix/archives/igniter-0.3.39/igniter-0.3.39/ebin/Elixir.Mix.Tasks.Igniter.Install.beam)
    │
  1 │ defmodule Mix.Tasks.Igniter.Install do
    │ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ lib/mix/tasks/igniter.install.ex:1: Mix.Tasks.Igniter.Install (module)

===> Analyzing applications...
===> Compiling telemetry
==> db_connection
Compiling 17 files (.ex)
Generated db_connection app
==> ecto_sql
Compiling 25 files (.ex)
Generated ecto_sql app
==> ash_sql
Compiling 13 files (.ex)
Generated ash_sql app
==> elixir_make
Compiling 8 files (.ex)
Generated elixir_make app
==> cc_precompiler
Compiling 3 files (.ex)
Generated cc_precompiler app
warning: key "exqlite-nif-2.16-aarch64-linux-gnu-0.24.2.tar.gz" will be overridden in map
└─ nofile:1

warning: key "exqlite-nif-2.16-riscv64-linux-gnu-0.24.2.tar.gz" will be overridden in map
└─ nofile:1

warning: key "exqlite-nif-2.16-x86_64-linux-gnu-0.24.2.tar.gz" will be overridden in map
└─ nofile:1

warning: key "exqlite-nif-2.17-aarch64-linux-gnu-0.24.2.tar.gz" will be overridden in map
└─ nofile:1

warning: key "exqlite-nif-2.17-riscv64-linux-gnu-0.24.2.tar.gz" will be overridden in map
└─ nofile:1

warning: key "exqlite-nif-2.17-x86_64-linux-gnu-0.24.2.tar.gz" will be overridden in map
└─ nofile:1

==> exqlite
Compiling 13 files (.ex)
Generated exqlite app
==> ecto_sqlite3
Compiling 4 files (.ex)
Generated ecto_sqlite3 app
==> ash_sqlite
Compiling 27 files (.ex)
     warning: Igniter.Code.Module.proper_location/2 is deprecated. Use `Igniter.Project.Module.proper_location/3`
     │
 247 │       path: Igniter.Code.Module.proper_location(module_name, "test/support")
     │                                 ~
     │
     └─ lib/mix/tasks/ash_sqlite.install.ex:247:33: Mix.Tasks.AshSqlite.Install.setup_data_case/1

Generated ash_sqlite app
==> wat

The following installer was found and executed: `ash_sqlite.install`:
Update: .formatter.exs

   ...|
3 3   |  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
4 4   |  plugins: [Spark.Formatter],
5   - |  import_deps: [:ash]
  5 + |  import_deps: [:ash_sqlite, :ash]
6 6   |]
7 7   |

Update: config/config.exs

 1  1   |import Config
 2  2   |
    3 + |config :wat, ecto_repos: [Wat.Repo]
    4 + |
 3  5   |config :ash,
 4  6   |  include_embedded_source_by_default?: false,
     ...|
30 32   |  ]
31 33   |
   34 + |import_config "#{config_env()}.exs"
   35 + |

Create: config/dev.exs

1 | import Config
2 |
3 | config :wat, Wat.Repo,
4 |   database: "../path/to/your.db",
5 |   port: 5432,
6 |   show_sensitive_data_on_connection_error: true,
7 |   pool_size: 10
8 |

Create: config/prod.exs

1 | import Config
2 |

Create: config/runtime.exs

1  | import Config
2  |
3  | if config_env() == :prod do
4  |   database_url =
5  |     System.get_env("DATABASE_URL") ||
6  |       raise """
7  |       environment variable DATABASE_URL is missing.
8  |       For example: ecto://USER:PASS@HOST/DATABASE
9  |       """
10 |
11 |   config :wat, Wat.Repo,
12 |     url: database_url,
13 |     pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")
14 | end
15 |

Create: config/test.exs

1 | import Config
2 |
3 | config :logger, level: :warning
4 | config :ash, disable_async?: true
5 |
6 | config :wat, Wat.Repo,
7 |   database: Path.join(__DIR__, "../path/to/your#{System.get_env("MIX_TEST_PARTITION")}.db"),
8 |   pool: Ecto.Adapters.SQL.Sandbox,
9 |   pool_size: 10
10 |

Create: lib/wat/application.ex

1  | defmodule Wat.Application do
2  |   @moduledoc false
3  |
4  |   use Application
5  |
6  |   @impl true
7  |   def start(_type, _args) do
8  |     children = [Wat.Repo]
9  |
10 |     opts = [strategy: :one_for_one, name: Wat.Supervisor]
11 |     Supervisor.start_link(children, opts)
12 |   end
13 | end
14 |

Create: lib/wat/repo.ex

1 | defmodule Wat.Repo do
2 |   use AshSqlite.Repo, otp_app: :wat
3 | end
4 |

Update: mix.exs

     ...|
 9  9   |      start_permanent: Mix.env() == :prod,
10 10   |      consolidate_protocols: Mix.env() != :dev,
11    - |      deps: deps()
   11 + |      deps: deps(),
   12 + |      elixirc_paths: elixirc_paths(Mix.env())
12 13   |    ]
13 14   |  end
     ...|
16 17   |  def application do
17 18   |    [
18    - |      extra_applications: [:logger]
   19 + |      extra_applications: [:logger],
   20 + |      mod: {Wat.Application, []}
19 21   |    ]
20 22   |  end
     ...|
30 32   |    ]
31 33   |  end
   34 + |
   35 + |  defp elixirc_paths(:test), do: elixirc_paths(:dev) ++ ["test/support"]
   36 + |  defp elixirc_paths(_), do: ["lib"]
32 37   |end
33 38   |

Create: test/support/data_case.ex

1  | defmodule Wat.DataCase do
2  |   @moduledoc """
3  |   This module defines the setup for tests requiring
4  |   access to the application's data layer.
5  |
6  |   You may define functions here to be used as helpers in
7  |   your tests.
8  |
9  |   Finally, if the test case interacts with the database,
10 |   we enable the SQL sandbox, so changes done to the database
11 |   are reverted at the end of every test. If you are using
12 |   PostgreSQL, you can even run database tests asynchronously
13 |   by setting `use AshHq.DataCase, async: true`, although
14 |   this option is not recommended for other databases.
15 |   """
16 |
17 |   use ExUnit.CaseTemplate
18 |
19 |   using do
20 |     quote do
21 |       alias Wat.Repo
22 |
23 |       import Ecto
24 |       import Ecto.Changeset
25 |       import Ecto.Query
26 |       import Wat.DataCase
27 |     end
28 |   end
29 |
30 |   setup tags do
31 |     pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Wat.Repo, shared: not tags[:async])
32 |     on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
33 |     :ok
34 |   end
35 | end
36 |

The following tasks will be run after the above changes:

* ash.codegen initialize

Proceed with changes? [Yn] Y
Resolving Hex dependencies...
Resolution completed in 0.069s
Unchanged:
  ash 3.4.20
  ash_sql 0.2.32
  ash_sqlite 0.2.0
  cc_precompiler 0.1.10
  db_connection 2.7.0
  decimal 2.1.1
  ecto 3.12.3
  ecto_sql 3.12.0
  ecto_sqlite3 0.17.2
  elixir_make 0.8.4
  ets 0.9.0
  exqlite 0.24.2
  glob_ex 0.1.8
  igniter 0.3.40
  iterex 0.1.2
  jason 1.4.4
  libgraph 0.16.0
  owl 0.11.0
  reactor 0.10.0
  rewrite 0.10.5
  sourceror 1.6.0
  spark 2.2.29
  spitfire 0.1.3
  splode 0.2.4
  stream_data 1.1.1
  telemetry 1.3.0
All dependencies are up to date
Compiling 3 files (.ex)
Generated wat app
Getting extensions in current project...
Running codegen for AshSqlite.DataLayer...
** (RuntimeError) must supply the --domains argument, or set `config :my_app, ash_domains: [...]` in config
    (ash_sqlite 0.2.0) lib/mix/helpers.ex:33: AshSqlite.Mix.Helpers.domains!/2
    (ash_sqlite 0.2.0) lib/mix/tasks/ash_sqlite.generate_migrations.ex:86: Mix.Tasks.AshSqlite.GenerateMigrations.run/1
    (mix 1.17.0) lib/mix/task.ex:495: anonymous fn/3 in Mix.Task.run_task/5
    (elixir 1.17.0) lib/enum.ex:1703: Enum."-map/2-lists^map/1-1-"/2
    (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/jmshrtn/.asdf/installs/elixir/1.17.0/bin/mix:2: (file)