@@ -137,36 +137,20 @@ defmodule AshPostgres.MigrationGenerator.Operation do
137137
138138def up ( % {
139139multitenancy: multitenancy ,
140- old_multitenancy: old_multitenancy ,
141- table: table ,
142140old_attribute: old_attribute ,
143141new_attribute: attribute
144142} ) do
145- dropped_constraints =
146- if has_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-
157143type_or_reference =
158- if has_reference? ( multitenancy , attribute ) and
144+ if AshPostgres.MigrationGenerator . has_reference? ( multitenancy , attribute ) and
159145Map . get ( old_attribute , :references ) != Map . get ( attribute , :references ) do
160146reference ( multitenancy , attribute )
161147else
162148inspect ( attribute . type )
163149end
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+ } "
170154end
171155
172156defp reference ( % { strategy: :context } , % {
@@ -206,13 +190,6 @@ defmodule AshPostgres.MigrationGenerator.Operation do
206190"references(#{ inspect ( table ) } , type:#{ inspect ( type ) } , column:#{ inspect ( destination_field ) } )"
207191end
208192
209- defp has_reference? ( multitenancy , attribute ) do
210- not is_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-
216193def down ( op ) do
217194up ( % {
218195op
@@ -224,6 +201,34 @@ defmodule AshPostgres.MigrationGenerator.Operation do
224201end
225202end
226203
204+ defmodule DropForeignKey do
205+ @ moduledoc false
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+ def up ( % { attribute: attribute , table: table , multitenancy: multitenancy , direction: :up } ) do
212+ if multitenancy && multitenancy . strategy == :context do
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+ def up ( _ ) , do: ""
220+
221+ def down ( % { attribute: attribute , table: table , multitenancy: multitenancy , direction: :down } ) do
222+ if multitenancy && multitenancy . strategy == :context do
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+ def down ( _ ) , do: ""
230+ end
231+
227232defmodule RenameAttribute do
228233@ moduledoc false
229234defstruct [ :old_attribute , :new_attribute , :table , :multitenancy , :old_multitenancy ]
@@ -241,20 +246,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do
241246@ moduledoc false
242247defstruct [ :attribute , :table , :multitenancy , :old_multitenancy ]
243248
244- def up ( % { 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- def drop_foreign_key_constraint ( % { references: nil } , _ , _table ) , do: ""
251-
252- def drop_foreign_key_constraint ( attribute , multitenancy , table ) do
253- if multitenancy do
254- "drop constraint(\" \# \{ prefix\} _#{ table } _#{ attribute . name } _fkey\" )"
255- else
256- "drop constraint(\" #{ table } _#{ attribute . name } _fkey\" )"
257- end
249+ def up ( % { attribute: attribute } ) do
250+ "remove#{ inspect ( attribute . name ) } "
258251end
259252
260253def down ( % { attribute: attribute , multitenancy: multitenancy } ) do