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

Commited1fe9b

Browse files
committed
fix: document/update migration path logic
1 parent929e3b7 commited1fe9b

File tree

4 files changed

+65
-62
lines changed

4 files changed

+65
-62
lines changed

‎lib/data_layer.ex‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ defmodule AshPostgres.DataLayer do
2323
table "table_name"
2424
end
2525
```
26+
27+
### Generating Migrations
28+
29+
See the documentation for `Mix.Tasks.AshPostgres.GenerateMigrations` for how to generate
30+
migrations from your resources
2631
"""
2732
@postgres%Ash.Dsl.Section{
2833
name::postgres,

‎lib/migration_generator/migration_generator.ex‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ defmodule AshPostgres.MigrationGenerator do
266266
opts.migration_path
267267
else
268268
"priv/"
269-
|>Path.join(repo_name)
270-
|>Path.join("migrations")
271269
end
270+
|>Path.join(repo_name)
271+
|>Path.join("migrations")
272272

273273
count=
274274
migration_path

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
11
defmoduleMix.Tasks.AshPostgres.GenerateMigrationsdo
2-
@description"Generates migrations, and stores a snapshot of your resources"
3-
@moduledoc@description
2+
@moduledoc"""
3+
Generates migrations, and stores a snapshot of your resources.
4+
5+
Options:
6+
7+
* `apis` - a comma separated list of API modules, for which migrations will be generated
8+
* `snapshot_path` - a custom path to store the snapshots, defaults to "priv/resource_snapshots"
9+
* `migration_path` - a custom path to store the migrations, defaults to "priv".
10+
Migrations are stored in a folder for each repo, so `priv/repo_name/migrations`
11+
12+
Flags:
13+
14+
* `quiet` - messages for file creations will not be printed
15+
* `format` - files that are created will be formatted with the code formatter, defaults to true
16+
17+
18+
#### Conflicts/Multiple Resources
19+
20+
The migration generator can support multiple schemas using the same table.
21+
It will raise on conflicts that it can't resolve, like the same field with different
22+
types. It will prompt to resolve conflicts that can be resolved with human input.
23+
For example, if you remove an attribute and add an attribute, it will ask you if you are renaming
24+
the column in question. If not, it will remove one column and add the other.
25+
26+
Additionally, it lowers things to the database where possible:
27+
28+
#### Defaults
29+
There are three anonymous functions that will translate to database-specific defaults currently:
30+
31+
* `&Ash.uuid/0` - Only if `uuid-ossp` is in your `c:AshPostgres.Repo.installed_extensions()`
32+
* `&Ecto.UUID.generate/0` - Only if `uuid-ossp` is in your `c:AshPostgres.Repo.installed_extensions()`
33+
* `&DateTime.utc_now/0`
34+
35+
Non-function default values will be dumped to their native type and inspected. This may not work for some types,
36+
and may require manual intervention/patches to the migration generator code.
37+
38+
#### Identities
39+
40+
Identities will cause the migration generator to generate unique constraints. If multiple
41+
resources target the same table, you will be asked to select the primary key, and any others
42+
will be added as unique constraints.
43+
"""
444
useMix.Task
545

6-
@shortdoc@description
46+
@shortdoc"Generates migrations, and stores a snapshot of your resources"
747
defrun(args)do
848
{opts,_}=
949
OptionParser.parse!(args,

‎test/migration_generator_test.exs‎

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,8 @@ defmodule AshPostgres.MigrationGeneratorTest do
4747
describe"creating initial snapshots"do
4848
setupdo
4949
on_exit(fn->
50-
"test_snapshots_path/**/*.json"
51-
|>Path.wildcard()
52-
|>Enum.each(&File.rm!/1)
53-
54-
"test_snapshots_path/*"
55-
|>Path.wildcard()
56-
|>Enum.each(&File.rmdir!/1)
57-
58-
"test_migration_path/**/*.exs"
59-
|>Path.wildcard()
60-
|>Enum.each(&File.rm!/1)
61-
62-
"test_migration_path/*"
63-
|>Path.wildcard()
64-
|>Enum.each(&File.rmdir!/1)
65-
66-
ifFile.exists?("test_snapshots_path")do
67-
File.rmdir("test_snapshots_path")
68-
end
69-
70-
ifFile.exists?("test_migration_path")do
71-
File.rmdir("test_migration_path")
72-
end
50+
File.rm_rf!("test_snapshots_path")
51+
File.rm_rf!("test_migration_path")
7352
end)
7453

7554
defpostsdo
@@ -113,27 +92,27 @@ defmodule AshPostgres.MigrationGeneratorTest do
11392
end
11493

11594
test"the migration creates the table"do
116-
assert[file]=Path.wildcard("test_migration_path/*_migrate_resources*.exs")
95+
assert[file]=Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
11796

11897
assertFile.read!(file)=~"create table(:posts, primary_key: false) do"
11998
end
12099

121100
test"the migration adds the id, with its default"do
122-
assert[file]=Path.wildcard("test_migration_path/*_migrate_resources*.exs")
101+
assert[file]=Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
123102

124103
assertFile.read!(file)=~
125104
~S[add :id, :binary_id, null: true, default: fragment("uuid_generate_v4()"), primary_key: true]
126105
end
127106

128107
test"the migration adds other attributes"do
129-
assert[file]=Path.wildcard("test_migration_path/*_migrate_resources*.exs")
108+
assert[file]=Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
130109

131110
assertFile.read!(file)=~
132111
~S[add :title, :text, null: true, default: nil, primary_key: false]
133112
end
134113

135114
test"the migration creates unique_indexes based on the identities of the resource"do
136-
assert[file]=Path.wildcard("test_migration_path/*_migrate_resources*.exs")
115+
assert[file]=Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
137116

138117
assertFile.read!(file)=~
139118
~S{create unique_index(:posts, [:title], name: "posts_title_unique_index")}
@@ -143,29 +122,8 @@ defmodule AshPostgres.MigrationGeneratorTest do
143122
describe"creating follow up migrations"do
144123
setupdo
145124
on_exit(fn->
146-
"test_snapshots_path/**/*.json"
147-
|>Path.wildcard()
148-
|>Enum.each(&File.rm!/1)
149-
150-
"test_snapshots_path/*"
151-
|>Path.wildcard()
152-
|>Enum.each(&File.rmdir!/1)
153-
154-
"test_migration_path/**/*.exs"
155-
|>Path.wildcard()
156-
|>Enum.each(&File.rm!/1)
157-
158-
"test_migration_path/*"
159-
|>Path.wildcard()
160-
|>Enum.each(&File.rmdir!/1)
161-
162-
ifFile.exists?("test_snapshots_path")do
163-
File.rmdir("test_snapshots_path")
164-
end
165-
166-
ifFile.exists?("test_migration_path")do
167-
File.rmdir("test_migration_path")
168-
end
125+
File.rm_rf!("test_snapshots_path")
126+
File.rm_rf!("test_migration_path")
169127
end)
170128

171129
defpostsdo
@@ -220,7 +178,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
220178
)
221179

222180
assert[_file1,file2]=
223-
Enum.sort(Path.wildcard("test_migration_path/*_migrate_resources*.exs"))
181+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
224182

225183
assertFile.read!(file2)=~
226184
~S[add :name, :text, null: false, default: nil, primary_key: false]
@@ -246,7 +204,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
246204
)
247205

248206
assert[_file1,file2]=
249-
Enum.sort(Path.wildcard("test_migration_path/*_migrate_resources*.exs"))
207+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
250208

251209
assertFile.read!(file2)=~~S[rename table(:posts), :title, to: :name]
252210
end
@@ -271,7 +229,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
271229
)
272230

273231
assert[_file1,file2]=
274-
Enum.sort(Path.wildcard("test_migration_path/*_migrate_resources*.exs"))
232+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
275233

276234
assertFile.read!(file2)=~
277235
~S[add :name, :text, null: false, default: nil, primary_key: false]
@@ -299,7 +257,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
299257
)
300258

301259
assert[_file1,file2]=
302-
Enum.sort(Path.wildcard("test_migration_path/*_migrate_resources*.exs"))
260+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
303261

304262
assertFile.read!(file2)=~~S[rename table(:posts), :title, to: :subject]
305263
end
@@ -325,7 +283,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
325283
)
326284

327285
assert[_file1,file2]=
328-
Enum.sort(Path.wildcard("test_migration_path/*_migrate_resources*.exs"))
286+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
329287

330288
assertFile.read!(file2)=~
331289
~S[add :subject, :text, null: false, default: nil, primary_key: false]
@@ -350,7 +308,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
350308
)
351309

352310
assert[_file1,file2]=
353-
Enum.sort(Path.wildcard("test_migration_path/*_migrate_resources*.exs"))
311+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
354312

355313
assertFile.read!(file2)=~
356314
~S[add :guid, :binary_id, null: true, default: fragment("uuid_generate_v4()"), primary_key: true]
@@ -382,7 +340,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
382340
)
383341

384342
assert[_file1,file2]=
385-
Enum.sort(Path.wildcard("test_migration_path/*_migrate_resources*.exs"))
343+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
386344

387345
assertFile.read!(file2)=~
388346
~S[add :foobar, :text, null: true, default: nil, primary_key: false]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp