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

Commitf0898ae

Browse files
authored
fix: drop constraints outside of phases (ash-project#29)
1 parent6634f22 commitf0898ae

File tree

3 files changed

+89
-49
lines changed

3 files changed

+89
-49
lines changed

‎lib/migration_generator/migration_generator.ex‎

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,21 @@ defmodule AshPostgres.MigrationGenerator do
606606
nameinkeys
607607
end
608608

609+
defpafter?(%Operation.AlterAttribute{table:table},%Operation.DropForeignKey{
610+
table:table,
611+
direction::up
612+
}),
613+
do:true
614+
615+
defpafter?(
616+
%Operation.DropForeignKey{
617+
table:table,
618+
direction::down
619+
},
620+
%Operation.AlterAttribute{table:table}
621+
),
622+
do:true
623+
609624
defpafter?(%Operation.AddAttribute{table:table},%Operation.CreateTable{table:table})do
610625
true
611626
end
@@ -804,12 +819,37 @@ defmodule AshPostgres.MigrationGenerator do
804819
end)
805820

806821
alter_attribute_events=
807-
Enum.map(attributes_to_alter,fn{new_attribute,old_attribute}->
808-
%Operation.AlterAttribute{
809-
new_attribute:new_attribute,
810-
old_attribute:old_attribute,
811-
table:snapshot.table
812-
}
822+
Enum.flat_map(attributes_to_alter,fn{new_attribute,old_attribute}->
823+
ifhas_reference?(old_snapshot.multitenancy,old_attribute)and
824+
Map.get(old_attribute,:references)!=Map.get(new_attribute,:references)do
825+
[
826+
%Operation.DropForeignKey{
827+
attribute:old_attribute,
828+
table:snapshot.table,
829+
multitenancy:old_snapshot.multitenancy,
830+
direction::up
831+
},
832+
%Operation.AlterAttribute{
833+
new_attribute:new_attribute,
834+
old_attribute:old_attribute,
835+
table:snapshot.table
836+
},
837+
%Operation.DropForeignKey{
838+
attribute:new_attribute,
839+
table:snapshot.table,
840+
multitenancy:snapshot.multitenancy,
841+
direction::down
842+
}
843+
]
844+
else
845+
[
846+
%Operation.AlterAttribute{
847+
new_attribute:new_attribute,
848+
old_attribute:old_attribute,
849+
table:snapshot.table
850+
}
851+
]
852+
end
813853
end)
814854

815855
remove_attribute_events=
@@ -821,6 +861,13 @@ defmodule AshPostgres.MigrationGenerator do
821861
alter_attribute_events++remove_attribute_events++rename_attribute_events
822862
end
823863

864+
defhas_reference?(multitenancy,attribute)do
865+
notis_nil(Map.get(attribute,:references))and
866+
!(attribute.references.multitenancy&&
867+
attribute.references.multitenancy.strategy==:context&&
868+
(is_nil(multitenancy)||multitenancy.strategy==:attribute))
869+
end
870+
824871
defget_existing_snapshot(snapshot,opts)do
825872
repo_name=snapshot.repo|>Module.split()|>List.last()|>Macro.underscore()
826873

‎lib/migration_generator/operation.ex‎

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -137,36 +137,20 @@ defmodule AshPostgres.MigrationGenerator.Operation do
137137

138138
defup(%{
139139
multitenancy:multitenancy,
140-
old_multitenancy:old_multitenancy,
141-
table:table,
142140
old_attribute:old_attribute,
143141
new_attribute:attribute
144142
})do
145-
dropped_constraints=
146-
ifhas_reference?(old_multitenancy,old_attribute)and
147-
Map.get(old_attribute,:references)!=Map.get(attribute,:references)do
148-
AshPostgres.MigrationGenerator.Operation.RemoveAttribute.drop_foreign_key_constraint(
149-
old_attribute,
150-
old_multitenancy,
151-
table
152-
)
153-
else
154-
""
155-
end
156-
157143
type_or_reference=
158-
ifhas_reference?(multitenancy,attribute)and
144+
ifAshPostgres.MigrationGenerator.has_reference?(multitenancy,attribute)and
159145
Map.get(old_attribute,:references)!=Map.get(attribute,:references)do
160146
reference(multitenancy,attribute)
161147
else
162148
inspect(attribute.type)
163149
end
164150

165-
dropped_constraints<>
166-
"\n\n"<>
167-
"modify#{inspect(attribute.name)},#{type_or_reference}#{
168-
alter_opts(attribute,old_attribute)
169-
}"
151+
"modify#{inspect(attribute.name)},#{type_or_reference}#{
152+
alter_opts(attribute,old_attribute)
153+
}"
170154
end
171155

172156
defpreference(%{strategy::context},%{
@@ -206,13 +190,6 @@ defmodule AshPostgres.MigrationGenerator.Operation do
206190
"references(#{inspect(table)}, type:#{inspect(type)}, column:#{inspect(destination_field)})"
207191
end
208192

209-
defphas_reference?(multitenancy,attribute)do
210-
notis_nil(Map.get(attribute,:references))and
211-
!(attribute.references.multitenancy&&
212-
attribute.references.multitenancy.strategy==:context&&
213-
(is_nil(multitenancy)||multitenancy.strategy==:attribute))
214-
end
215-
216193
defdown(op)do
217194
up(%{
218195
op
@@ -224,6 +201,34 @@ defmodule AshPostgres.MigrationGenerator.Operation do
224201
end
225202
end
226203

204+
defmoduleDropForeignKeydo
205+
@moduledocfalse
206+
# We only run this migration in one direction, based on the input
207+
# This is because the creation of a foreign key is handled by `references/3`
208+
# We only need to drop it before altering an attribute with `references/3`
209+
defstruct[:attribute,:table,:multitenancy,:direction,no_phase:true]
210+
211+
defup(%{attribute:attribute,table:table,multitenancy:multitenancy,direction::up})do
212+
ifmultitenancy&&multitenancy.strategy==:contextdo
213+
"drop constraint(:#{table},\"\#\{prefix\}_#{table}_#{attribute.name}_fkey\")"
214+
else
215+
"drop constraint(:#{table},\"#{table}_#{attribute.name}_fkey\")"
216+
end
217+
end
218+
219+
defup(_),do:""
220+
221+
defdown(%{attribute:attribute,table:table,multitenancy:multitenancy,direction::down})do
222+
ifmultitenancy&&multitenancy.strategy==:contextdo
223+
"drop constraint(:#{table},\"\#\{prefix\}_#{table}_#{attribute.name}_fkey\")"
224+
else
225+
"drop constraint(:#{table},\"#{table}_#{attribute.name}_fkey\")"
226+
end
227+
end
228+
229+
defdown(_),do:""
230+
end
231+
227232
defmoduleRenameAttributedo
228233
@moduledocfalse
229234
defstruct[:old_attribute,:new_attribute,:table,:multitenancy,:old_multitenancy]
@@ -241,20 +246,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do
241246
@moduledocfalse
242247
defstruct[:attribute,:table,:multitenancy,:old_multitenancy]
243248

244-
defup(%{multitenancy:multitenancy,attribute:attribute,table:table})do
245-
drop_foreign_key_constraint(attribute,multitenancy,table)<>
246-
"\n\n"<>
247-
"remove#{inspect(attribute.name)}"
248-
end
249-
250-
defdrop_foreign_key_constraint(%{references:nil},_,_table),do:""
251-
252-
defdrop_foreign_key_constraint(attribute,multitenancy,table)do
253-
ifmultitenancydo
254-
"drop constraint(\"\#\{prefix\}_#{table}_#{attribute.name}_fkey\")"
255-
else
256-
"drop constraint(\"#{table}_#{attribute.name}_fkey\")"
257-
end
249+
defup(%{attribute:attribute})do
250+
"remove#{inspect(attribute.name)}"
258251
end
259252

260253
defdown(%{attribute:attribute,multitenancy:multitenancy})do

‎lib/migration_generator/phase.ex‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ defmodule AshPostgres.MigrationGenerator.Phase do
3535
Enum.map_join(operations,"\n",fnoperation->operation.__struct__.up(operation)end)
3636

3737
ifmultitenancy.strategy==:contextdo
38-
"alter table(#{inspect(table)}, prefix: prefix) do\n"<>
38+
"alter table(:#{table}, prefix: prefix) do\n"<>
3939
body<>
4040
"\nend"
4141
else
42-
"alter table(#{inspect(table)}) do\n"<>
42+
"alter table(:#{table}) do\n"<>
4343
body<>
4444
"\nend"
4545
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp