Skip to content

Commit 800900b

Browse files
authored
improvement: propagate -r flag to Ecto (#521)
* Propagate r flag to ecto * Make repos helper function return opts repos when provided
1 parent ea546a6 commit 800900b

File tree

5 files changed

+91
-59
lines changed

5 files changed

+91
-59
lines changed

lib/mix/helpers.ex

Lines changed: 75 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -38,61 +38,81 @@ defmodule AshPostgres.Mix.Helpers do
3838
end
3939

4040
def repos!(opts, args) do
41-
if opts[:domains] && opts[:domains] != "" do
42-
domains = domains!(opts, args)
41+
cond do
42+
opts[:repo] && opts[:repo] != "" ->
43+
ensure_load(args)
4344

44-
resources =
45-
domains
46-
|> Enum.flat_map(&Ash.Domain.Info.resources/1)
47-
|> Enum.filter(&(Ash.DataLayer.data_layer(&1) == AshPostgres.DataLayer))
45+
opts[:repo]
46+
|> Kernel.||("")
47+
|> String.split(",")
48+
|> Enum.flat_map(fn
49+
"" ->
50+
[]
51+
52+
repo ->
53+
[Module.concat([repo])]
54+
end)
55+
|> Enum.map(fn repo ->
56+
case Code.ensure_compiled(repo) do
57+
{:module, _} ->
58+
repo
59+
60+
{:error, error} ->
61+
Mix.raise("Could not load #{inspect(repo)}, error: #{inspect(error)}. ")
62+
end
63+
end)
64+
65+
opts[:domains] && opts[:domains] != "" ->
66+
domains = domains!(opts, args)
67+
68+
resources =
69+
domains
70+
|> Enum.flat_map(&Ash.Domain.Info.resources/1)
71+
|> Enum.filter(&(Ash.DataLayer.data_layer(&1) == AshPostgres.DataLayer))
72+
|> case do
73+
[] ->
74+
raise """
75+
No resources with `data_layer: AshPostgres.DataLayer` found in the domains #{Enum.map_join(domains, ",", &inspect/1)}.
76+
77+
Must be able to find at least one resource with `data_layer: AshPostgres.DataLayer`.
78+
"""
79+
80+
resources ->
81+
resources
82+
end
83+
84+
resources
85+
|> Enum.flat_map(
86+
&[
87+
AshPostgres.DataLayer.Info.repo(&1, :read),
88+
AshPostgres.DataLayer.Info.repo(&1, :mutate)
89+
]
90+
)
91+
|> Enum.uniq()
4892
|> case do
4993
[] ->
5094
raise """
51-
No resources with `data_layer: AshPostgres.DataLayer` found in the domains #{Enum.map_join(domains, ",", &inspect/1)}.
95+
No repos could be found configured on the resources in the domains: #{Enum.map_join(domains, ",", &inspect/1)}
5296
53-
Must be able to find at least one resource with `data_layer: AshPostgres.DataLayer`.
54-
"""
97+
At least one resource must have a repo configured.
5598
56-
resources ->
57-
resources
58-
end
99+
The following resources were found with `data_layer: AshPostgres.DataLayer`:
59100
60-
resources
61-
|> Enum.flat_map(
62-
&[
63-
AshPostgres.DataLayer.Info.repo(&1, :read),
64-
AshPostgres.DataLayer.Info.repo(&1, :mutate)
65-
]
66-
)
67-
|> Enum.uniq()
68-
|> case do
69-
[] ->
70-
raise """
71-
No repos could be found configured on the resources in the domains: #{Enum.map_join(domains, ",", &inspect/1)}
72-
73-
At least one resource must have a repo configured.
101+
#{Enum.map_join(resources, "\n", &"* #{inspect(&1)}")}
102+
"""
74103

75-
The following resources were found with `data_layer: AshPostgres.DataLayer`:
104+
repos ->
105+
repos
106+
end
76107

77-
#{Enum.map_join(resources, "\n", &"* #{inspect(&1)}")}
78-
"""
108+
true ->
109+
ensure_load(args)
79110

80-
repos ->
81-
repos
82-
end
83-
else
84-
if Code.ensure_loaded?(Mix.Tasks.App.Config) do
85-
Mix.Task.run("app.config", args)
86-
else
87-
Mix.Task.run("loadpaths", args)
88-
"--no-compile" not in args && Mix.Task.run("compile", args)
89-
end
90-
91-
Mix.Project.config()[:app]
92-
|> Application.get_env(:ecto_repos, [])
93-
|> Enum.filter(fn repo ->
94-
Spark.implements_behaviour?(repo, AshPostgres.Repo)
95-
end)
111+
Mix.Project.config()[:app]
112+
|> Application.get_env(:ecto_repos, [])
113+
|> Enum.filter(fn repo ->
114+
Spark.implements_behaviour?(repo, AshPostgres.Repo)
115+
end)
96116
end
97117
end
98118

@@ -117,12 +137,7 @@ defmodule AshPostgres.Mix.Helpers do
117137
end
118138

119139
defp ensure_compiled(domain, args) do
120-
if Code.ensure_loaded?(Mix.Tasks.App.Config) do
121-
Mix.Task.run("app.config", args)
122-
else
123-
Mix.Task.run("loadpaths", args)
124-
"--no-compile" not in args && Mix.Task.run("compile", args)
125-
end
140+
ensure_load(args)
126141

127142
case Code.ensure_compiled(domain) do
128143
{:module, _} ->
@@ -139,6 +154,15 @@ defmodule AshPostgres.Mix.Helpers do
139154
end
140155
end
141156

157+
defp ensure_load(args) do
158+
if Code.ensure_loaded?(Mix.Tasks.App.Config) do
159+
Mix.Task.run("app.config", args)
160+
else
161+
Mix.Task.run("loadpaths", args)
162+
"--no-compile" not in args && Mix.Task.run("compile", args)
163+
end
164+
end
165+
142166
def tenants(repo, opts) do
143167
tenants = repo.all_tenants()
144168

lib/mix/tasks/ash_postgres.create.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ defmodule Mix.Tasks.AshPostgres.Create do
77
quiet: :boolean,
88
domains: :string,
99
no_compile: :boolean,
10-
no_deps_check: :boolean
10+
no_deps_check: :boolean,
11+
repo: :string,
12+
r: :string
1113
]
1214

1315
@aliases [
14-
q: :quiet
16+
q: :quiet,
17+
r: :repo
1518
]
1619

1720
@moduledoc """

lib/mix/tasks/ash_postgres.drop.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ defmodule Mix.Tasks.AshPostgres.Drop do
66

77
@aliases [
88
f: :force,
9-
q: :quiet
9+
q: :quiet,
10+
r: :repo
1011
]
1112

1213
@switches [
@@ -15,7 +16,8 @@ defmodule Mix.Tasks.AshPostgres.Drop do
1516
quiet: :boolean,
1617
domains: :string,
1718
no_compile: :boolean,
18-
no_deps_check: :boolean
19+
no_deps_check: :boolean,
20+
repo: :string
1921
]
2022

2123
@moduledoc """

lib/mix/tasks/ash_postgres.migrate.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ defmodule Mix.Tasks.AshPostgres.Migrate do
77
@shortdoc "Runs the repository migrations for all repositories in the provided (or congigured) domains"
88

99
@aliases [
10-
n: :step
10+
n: :step,
11+
r: :repo
1112
]
1213

1314
@switches [
@@ -26,7 +27,8 @@ defmodule Mix.Tasks.AshPostgres.Migrate do
2627
no_deps_check: :boolean,
2728
migrations_path: :keep,
2829
only_tenants: :string,
29-
except_tenants: :string
30+
except_tenants: :string,
31+
repo: :string
3032
]
3133

3234
@moduledoc """

lib/mix/tasks/ash_postgres.rollback.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ defmodule Mix.Tasks.AshPostgres.Rollback do
5959
log_migrations_sql: :boolean,
6060
log_migrator_sql: :boolean,
6161
only_tenants: :string,
62-
except_tenants: :string
62+
except_tenants: :string,
63+
repo: :string
6364
],
64-
aliases: [n: :step, v: :to]
65+
aliases: [n: :step, v: :to, r: :repo]
6566
)
6667

6768
repos = AshPostgres.Mix.Helpers.repos!(opts, args)

0 commit comments

Comments
 (0)