@@ -38,61 +38,81 @@ defmodule AshPostgres.Mix.Helpers do
38
38
end
39
39
40
40
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 )
43
44
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 ( )
48
92
|> case do
49
93
[ ] ->
50
94
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 ) }
52
96
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.
55
98
56
- resources ->
57
- resources
58
- end
99
+ The following resources were found with `data_layer: AshPostgres.DataLayer`:
59
100
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
+ """
74
103
75
- The following resources were found with `data_layer: AshPostgres.DataLayer`:
104
+ repos ->
105
+ repos
106
+ end
76
107
77
- #{ Enum . map_join ( resources , " \n " , & "* #{ inspect ( & 1 ) } " ) }
78
- """
108
+ true ->
109
+ ensure_load ( args )
79
110
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 )
96
116
end
97
117
end
98
118
@@ -117,12 +137,7 @@ defmodule AshPostgres.Mix.Helpers do
117
137
end
118
138
119
139
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 )
126
141
127
142
case Code . ensure_compiled ( domain ) do
128
143
{ :module , _ } ->
@@ -139,6 +154,15 @@ defmodule AshPostgres.Mix.Helpers do
139
154
end
140
155
end
141
156
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
+
142
166
def tenants ( repo , opts ) do
143
167
tenants = repo . all_tenants ( )
144
168
0 commit comments