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

Commit2d0245e

Browse files
committed
chore: open up some migration APIs for experimentation purposes
1 parentcf92fe1 commit2d0245e

File tree

2 files changed

+128
-45
lines changed

2 files changed

+128
-45
lines changed

‎lib/migration_generator/migration_generator.ex‎

Lines changed: 128 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ defmodule AshPostgres.MigrationGenerator do
1414
migration_path:nil,
1515
tenant_migration_path:nil,
1616
quiet:false,
17+
current_snapshots:nil,
18+
answers:[],
19+
no_shell?:false,
1720
format:true,
1821
dry_run:false,
1922
check_generated:false,
@@ -43,7 +46,7 @@ defmodule AshPostgres.MigrationGenerator do
4346
tenant_snapshots_to_include_in_global=
4447
tenant_snapshots
4548
|>Enum.filter(&&1.multitenancy.global)
46-
|>Enum.map(&Map.put(&1,:multitenancy,%{strategy:nil,attribute:nil,global:false}))
49+
|>Enum.map(&Map.put(&1,:multitenancy,%{strategy:nil,attribute:nil,global:nil}))
4750

4851
snapshots=snapshots++tenant_snapshots_to_include_in_global
4952

@@ -57,6 +60,48 @@ defmodule AshPostgres.MigrationGenerator do
5760
create_migrations(snapshots,opts,false)
5861
end
5962

63+
@doc"""
64+
A work in progress utility for getting snapshots.
65+
66+
Does not support everything supported by the migration generator.
67+
"""
68+
deftake_snapshots(api,repo)do
69+
all_resources=Ash.Api.resources(api)
70+
71+
all_resources
72+
|>Enum.filter(&(Ash.DataLayer.data_layer(&1)==AshPostgres.DataLayer))
73+
|>Enum.filter(&(AshPostgres.repo(&1)==repo))
74+
|>Enum.flat_map(&get_snapshots(&1,all_resources))
75+
end
76+
77+
@doc"""
78+
A work in progress utility for getting operations between snapshots.
79+
80+
Does not support everything supported by the migration generator.
81+
"""
82+
defget_operations_from_snapshots(old_snapshots,new_snapshots,opts\\[])do
83+
opts=%{opts(opts)|no_shell?:true}
84+
85+
old_snapshots=Enum.map(old_snapshots,&sanitize_snapshot/1)
86+
87+
new_snapshots
88+
|>deduplicate_snapshots(opts,old_snapshots)
89+
|>fetch_operations(opts)
90+
|>Enum.flat_map(&elem(&1,1))
91+
|>Enum.uniq()
92+
|>organize_operations()
93+
end
94+
95+
defpopts(opts)do
96+
casestruct(__MODULE__,opts)do
97+
%{check_generated:true}=opts->
98+
%{opts|dry_run:true}
99+
100+
opts->
101+
opts
102+
end
103+
end
104+
60105
defpcreate_extension_migrations(repos,opts)do
61106
forrepo<-reposdo
62107
snapshot_file=Path.join(opts.snapshot_path,"extensions.json")
@@ -165,16 +210,23 @@ defmodule AshPostgres.MigrationGenerator do
165210
ifopts.check_generated,do:exit({:shutdown,1})
166211

167212
operations
168-
|>sort_operations()
169-
|>streamline()
170-
|>group_into_phases()
171-
|>comment_out_phases()
213+
|>organize_operations
172214
|>build_up_and_down()
173215
|>write_migration!(snapshots,repo,opts,tenant?)
174216
end
175217
end)
176218
end
177219

220+
defporganize_operations([]),do:[]
221+
222+
defporganize_operations(operations)do
223+
operations
224+
|>sort_operations()
225+
|>streamline()
226+
|>group_into_phases()
227+
|>comment_out_phases()
228+
end
229+
178230
defpcomment_out_phases(phases)do
179231
Enum.map(phases,fn
180232
%{operations:operations}=phase->
@@ -189,14 +241,20 @@ defmodule AshPostgres.MigrationGenerator do
189241
end)
190242
end
191243

192-
defpdeduplicate_snapshots(snapshots,opts)do
244+
defpdeduplicate_snapshots(snapshots,opts,existing_snapshots\\[])do
193245
snapshots
194246
|>Enum.group_by(fnsnapshot->
195247
snapshot.table
196248
end)
197249
|>Enum.map(fn{_table,[snapshot|_]=snapshots}->
198-
existing_snapshot=get_existing_snapshot(snapshot,opts)
199-
{primary_key,identities}=merge_primary_keys(existing_snapshot,snapshots)
250+
existing_snapshot=
251+
ifopts.no_shell?do
252+
Enum.find(existing_snapshots,&(&1.table==snapshot.table))
253+
else
254+
get_existing_snapshot(snapshot,opts)
255+
end
256+
257+
{primary_key,identities}=merge_primary_keys(existing_snapshot,snapshots,opts)
200258

201259
attributes=Enum.flat_map(snapshots,&&1.attributes)
202260

@@ -336,7 +394,7 @@ defmodule AshPostgres.MigrationGenerator do
336394
end
337395
end
338396

339-
defpmerge_primary_keys(nil,[snapshot|_]=snapshots)do
397+
defpmerge_primary_keys(nil,[snapshot|_]=snapshots,opts)do
340398
snapshots
341399
|>Enum.map(&pkey_names(&1.attributes))
342400
|>Enum.uniq()
@@ -352,16 +410,20 @@ defmodule AshPostgres.MigrationGenerator do
352410
"#{index}:#{inspect(pkey)}"
353411
end)
354412

355-
message="""
356-
Which primary key should be used for the table `#{snapshot.table}` (enter the number)?
413+
choice=
414+
ifopts.no_shell?do
415+
raise"Unimplemented: cannot resolve primary key ambiguity without shell input"
416+
else
417+
message="""
418+
Which primary key should be used for the table `#{snapshot.table}` (enter the number)?
357419
358-
#{unique_primary_key_names}
359-
"""
420+
#{unique_primary_key_names}
421+
"""
360422

361-
choice=
362-
message
363-
|>Mix.shell().prompt()
364-
|>String.to_integer()
423+
message
424+
|>Mix.shell().prompt()
425+
|>String.to_integer()
426+
end
365427

366428
identities=
367429
unique_primary_keys
@@ -387,7 +449,7 @@ defmodule AshPostgres.MigrationGenerator do
387449
end
388450
end
389451

390-
defpmerge_primary_keys(existing_snapshot,snapshots)do
452+
defpmerge_primary_keys(existing_snapshot,snapshots,opts)do
391453
pkey_names=pkey_names(existing_snapshot.attributes)
392454

393455
one_pkey_exists?=
@@ -413,7 +475,7 @@ defmodule AshPostgres.MigrationGenerator do
413475

414476
{pkey_names,identities}
415477
else
416-
merge_primary_keys(nil,snapshots)
478+
merge_primary_keys(nil,snapshots,opts)
417479
end
418480
end
419481

@@ -565,7 +627,8 @@ defmodule AshPostgres.MigrationGenerator do
565627
end
566628
end
567629

568-
defpbuild_up_and_down(phases)do
630+
@docfalse
631+
defbuild_up_and_down(phases)do
569632
up=
570633
Enum.map_join(phases,"\n",fnphase->
571634
phase
@@ -924,7 +987,7 @@ defmodule AshPostgres.MigrationGenerator do
924987
multitenancy:%{
925988
attribute:nil,
926989
strategy:nil,
927-
global:false
990+
global:nil
928991
}
929992
}
930993

@@ -994,7 +1057,7 @@ defmodule AshPostgres.MigrationGenerator do
9941057
end)
9951058

9961059
{attributes_to_add,attributes_to_remove,attributes_to_rename}=
997-
resolve_renames(snapshot.table,attributes_to_add,attributes_to_remove)
1060+
resolve_renames(snapshot.table,attributes_to_add,attributes_to_remove,opts)
9981061

9991062
attributes_to_alter=
10001063
snapshot.attributes
@@ -1130,7 +1193,7 @@ defmodule AshPostgres.MigrationGenerator do
11301193

11311194
snapshot_file
11321195
|>File.read!()
1133-
|>load_snapshot(snapshot.table)
1196+
|>load_snapshot()
11341197
end
11351198
else
11361199
get_old_snapshot(folder,snapshot)
@@ -1145,37 +1208,60 @@ defmodule AshPostgres.MigrationGenerator do
11451208
ifFile.exists?(old_snapshot_file)do
11461209
old_snapshot_file
11471210
|>File.read!()
1148-
|>load_snapshot(snapshot.table)
1211+
|>load_snapshot()
11491212
end
11501213
end
11511214

1152-
defpresolve_renames(_table,adding,[]),do:{adding,[],[]}
1215+
defpresolve_renames(_table,adding,[],_opts),do:{adding,[],[]}
11531216

1154-
defpresolve_renames(_table,[],removing),do:{[],removing,[]}
1217+
defpresolve_renames(_table,[],removing,_opts),do:{[],removing,[]}
11551218

1156-
defpresolve_renames(table,[adding],[removing])do
1157-
ifMix.shell().yes?("Are you renaming#{table}.#{removing.name} to#{table}.#{adding.name}?")do
1219+
defpresolve_renames(table,[adding],[removing],opts)do
1220+
ifrenaming_to?(table,removing.name,adding.name,opts)do
11581221
{[],[],[{adding,removing}]}
11591222
else
11601223
{[adding],[removing],[]}
11611224
end
11621225
end
11631226

1164-
defpresolve_renames(table,adding,[removing|rest])do
1227+
defpresolve_renames(table,adding,[removing|rest],opts)do
11651228
{new_adding,new_removing,new_renames}=
1166-
ifMix.shell().yes?("Are you renaming#{table}.#{removing.name}?")do
1167-
new_attribute=get_new_attribute(adding)
1229+
ifrenaming?(table,removing,opts)do
1230+
new_attribute=
1231+
ifopts.no_shell?do
1232+
raise"Unimplemented: Cannot get new_attribute without the shell!"
1233+
else
1234+
get_new_attribute(adding)
1235+
end
11681236

11691237
{adding--[new_attribute],[],[{new_attribute,removing}]}
11701238
else
11711239
{adding,[removing],[]}
11721240
end
11731241

1174-
{rest_adding,rest_removing,rest_renames}=resolve_renames(table,new_adding,rest)
1242+
{rest_adding,rest_removing,rest_renames}=resolve_renames(table,new_adding,rest,opts)
11751243

11761244
{new_adding++rest_adding,new_removing++rest_removing,rest_renames++new_renames}
11771245
end
11781246

1247+
defprenaming_to?(table,removing,adding,opts)do
1248+
ifopts.no_shell?do
1249+
raise"Unimplemented: cannot determine: Are you renaming#{table}.#{removing} to#{table}.#{
1250+
adding
1251+
}? without shell input"
1252+
else
1253+
Mix.shell().yes?("Are you renaming#{table}.#{removing} to#{table}.#{adding}?")
1254+
end
1255+
end
1256+
1257+
defprenaming?(table,removing,opts)do
1258+
ifopts.no_shell?do
1259+
raise"Unimplemented: cannot determine: Are you renaming#{table}.#{removing.name}? without shell input"
1260+
else
1261+
Mix.shell().yes?("Are you renaming#{table}.#{removing.name}?")
1262+
end
1263+
end
1264+
11791265
defpget_new_attribute(adding,tries\\3)
11801266

11811267
defpget_new_attribute(_adding,0)do
@@ -1421,28 +1507,33 @@ defmodule AshPostgres.MigrationGenerator do
14211507
end
14221508

14231509
defpsanitize_type({:array,type})do
1424-
["array",type]
1510+
["array",sanitize_type(type)]
14251511
end
14261512

14271513
defpsanitize_type(type)do
14281514
type
14291515
end
14301516

1431-
defpload_snapshot(json,table)do
1517+
defpload_snapshot(json)do
14321518
json
14331519
|>Jason.decode!(keys::atoms!)
1520+
|>sanitize_snapshot()
1521+
end
1522+
1523+
defpsanitize_snapshot(snapshot)do
1524+
snapshot
14341525
|>Map.put_new(:has_create_action,true)
14351526
|>Map.update!(:identities,fnidentities->
14361527
Enum.map(identities,&load_identity/1)
14371528
end)
14381529
|>Map.update!(:attributes,fnattributes->
1439-
Enum.map(attributes,&load_attribute(&1,table))
1530+
Enum.map(attributes,&load_attribute(&1,snapshot.table))
14401531
end)
14411532
|>Map.update!(:repo,&String.to_atom/1)
14421533
|>Map.put_new(:multitenancy,%{
14431534
attribute:nil,
14441535
strategy:nil,
1445-
global:false
1536+
global:nil
14461537
})
14471538
|>Map.update!(:multitenancy,&load_multitenancy/1)
14481539
end
@@ -1472,7 +1563,7 @@ defmodule AshPostgres.MigrationGenerator do
14721563
|>Map.put_new(:multitenancy,%{
14731564
attribute:nil,
14741565
strategy:nil,
1475-
global:false
1566+
global:nil
14761567
})
14771568
|>Map.update!(:multitenancy,&load_multitenancy/1)
14781569
end)

‎lib/migration_generator/operation.ex‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,13 @@ defmodule AshPostgres.MigrationGenerator.Operation do
114114

115115
defup(%{
116116
multitenancy:%{strategy::attribute},
117-
table:current_table,
118117
attribute:
119118
%{
120119
references:%{
121-
table:table,
122120
multitenancy:%{strategy::context}
123121
}
124122
}=attribute
125123
})do
126-
Mix.shell().info("""
127-
table `#{current_table}` with attribute multitenancy refers to table `#{table}` with schema based multitenancy.
128-
This means that it is not possible to use a foreign key. This is not necessarily a problem, just something
129-
you should be aware of
130-
""")
131-
132124
[
133125
"add#{inspect(attribute.name)}",
134126
inspect(attribute.type),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp