Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7581d37

Browse files
committed
WIP
1 parent7285b33 commit7581d37

File tree

10 files changed

+1911
-10
lines changed

10 files changed

+1911
-10
lines changed

‎lib/igniter.ex‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ defmodule AshPostgres.Igniter do
1818
"""
1919
end
2020

21+
deftable(igniter,resource)do
22+
igniter
23+
|>Spark.Igniter.get_option(resource,[:postgres,:table])
24+
|>casedo
25+
{igniter,{:ok,value}}whenis_binary(value)oris_nil(value)->
26+
{:ok,igniter,value}
27+
28+
_->
29+
:error
30+
end
31+
end
32+
33+
defrepo(igniter,resource)do
34+
igniter
35+
|>Spark.Igniter.get_option(resource,[:postgres,:repo])
36+
|>casedo
37+
{igniter,{:ok,value}}whenis_atom(value)->
38+
{:ok,igniter,value}
39+
40+
_->
41+
:error
42+
end
43+
end
44+
2145
defadd_postgres_extension(igniter,repo_name,extension)do
2246
Igniter.Code.Module.find_and_update_module!(igniter,repo_name,fnzipper->
2347
caseIgniter.Code.Function.move_to_def(zipper,:installed_extensions,0)do

‎lib/migration_generator/migration_generator.ex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule AshPostgres.MigrationGenerator do
1818
format:true,
1919
dry_run:false,
2020
check:false,
21+
snapshots_only:false,
2122
dont_drop_columns:false
2223

2324
defgenerate(domains,opts\\[])do
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
defmoduleMix.Tasks.AshPostgres.Gen.Resourcesdo
2+
useIgniter.Mix.Task
3+
4+
@shortdoc"Generates or updates resources based on a database schema"
5+
6+
@doc"""
7+
#{@shortdoc}
8+
9+
## Options
10+
11+
- `repo`, `r` - The repo or repos to generate resources for, comma separated. Can be specified multiple times. Defaults to all repos.
12+
- `tables`, `t` - The tables to generate resources for, comma separated. Can be specified multiple times. Defaults to all tables non-`_*` tables
13+
- `skip-tables`, `s` - The tables to skip generating resources for, comma separated. Can be specified multiple times.
14+
- `snapshots-only`, `n` - Only generate snapshots for the generated resources, and not migraitons.
15+
- `domains` , 'd` - The domain to generate resources inside of. See the section on domains for more.
16+
"""
17+
18+
@implIgniter.Mix.Task
19+
definfo(_argv,_parent)do
20+
%Igniter.Mix.Task.Info{
21+
positional:[],
22+
schema:[
23+
repo::keep,
24+
tables::keep,
25+
skip_tables::keep,
26+
snapshots_only::boolean,
27+
domain::keep
28+
],
29+
aliases:[
30+
t::tables,
31+
r::repo,
32+
d::domain,
33+
s::skip_tables,
34+
n::snapshots_only
35+
]
36+
}
37+
end
38+
39+
@implIgniter.Mix.Task
40+
defigniter(igniter,argv)do
41+
Mix.Task.run("compile")
42+
43+
options=options!(argv)
44+
45+
repos=
46+
options[:repo]||
47+
Mix.Project.config()[:app]
48+
|>Application.get_env(:ecto_repos,[])
49+
50+
casereposdo
51+
[]->
52+
igniter
53+
|>Igniter.add_warning("No ecto repos configured.")
54+
55+
repos->
56+
Mix.shell().info("Generating resources from#{inspect(repos)}")
57+
58+
prompt=
59+
"""
60+
61+
Would you like to generate migrations for the current structure? (recommended)
62+
63+
If#{IO.ANSI.green()}yes#{IO.ANSI.reset()}:
64+
We will generate migrations based on the generated resources.
65+
You should then change your database name in your config, and
66+
run `mix ash.setup`.
67+
68+
If you already have ecto migrations you'd like to use, run
69+
this command with `--snapshots-only`, in which case only resource
70+
snapshots will be generated.
71+
#{IO.ANSI.green()}
72+
Going forward, your resources will be the source of truth.#{IO.ANSI.reset()}
73+
#{IO.ANSI.red()}
74+
*WARNING*
75+
76+
If you run `mix ash.reset` after this command without updating
77+
your config, you will be *deleting the database you just used to
78+
generate these resources*!#{IO.ANSI.reset()}
79+
80+
If#{IO.ANSI.red()}no#{IO.ANSI.reset()}:
81+
82+
We will not generate any migrations. This means you have migrations already that
83+
can get you from zero to the current starting point.
84+
#{IO.ANSI.yellow()}
85+
You will have to hand-write migrations from this point on.#{IO.ANSI.reset()}
86+
"""
87+
88+
options=
89+
ifMix.shell().yes?(prompt)do
90+
Keyword.put(options,:no_migrations,false)
91+
else
92+
Keyword.put(options,:no_migrations,true)
93+
end
94+
95+
migration_opts=
96+
ifoptions[:snapshots_only]do
97+
["--snapshots-only"]
98+
else
99+
[]
100+
end
101+
102+
igniter
103+
|>AshPostgres.ResourceGenerator.generate(repos,options)
104+
|>then(fnigniter->
105+
ifoptions[:no_migrations]do
106+
igniter
107+
else
108+
Igniter.add_task(igniter,"ash_postgres.generate_migrations",migration_opts)
109+
end
110+
end)
111+
end
112+
end
113+
end

‎lib/mix/tasks/ash_postgres.generate_migrations.ex‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do
2121
* `no-format` - files that are created will not be formatted with the code formatter
2222
* `dry-run` - no files are created, instead the new migration is printed
2323
* `check` - no files are created, returns an exit(1) code if the current snapshots and resources don't fit
24+
* `snapshots-only` - no migrations are generated, only snapshots are stored
2425
2526
#### Snapshots
2627
@@ -90,6 +91,7 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do
9091
migration_path::string,
9192
tenant_migration_path::string,
9293
quiet::boolean,
94+
snapshots_only::boolean,
9395
name::string,
9496
no_format::boolean,
9597
dry_run::boolean,
@@ -100,7 +102,7 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do
100102

101103
domains=AshPostgres.Mix.Helpers.domains!(opts,args,false)
102104

103-
ifEnum.empty?(domains)do
105+
ifEnum.empty?(domains)&&!opts[:snapshots_only]do
104106
IO.warn("""
105107
No domains found, so no resource-related migrations will be generated.
106108
Pass the `--domains` option or configure `config :your_app, ash_domains: [...]`
@@ -113,7 +115,7 @@ defmodule Mix.Tasks.AshPostgres.GenerateMigrations do
113115
|>Keyword.delete(:no_format)
114116
|>Keyword.put_new(:name,name)
115117

116-
if!opts[:name]&&!opts[:dry_run]&&!opts[:check]do
118+
if!opts[:name]&&!opts[:dry_run]&&!opts[:check]&&!opts[:snapshots_only]do
117119
IO.warn("""
118120
Name must be provided when generating migrations, unless `--dry-run` or `--check` is also provided.
119121
Using an autogenerated name will be deprecated in a future release.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp