@@ -127,6 +127,14 @@ defmodule AshPostgres.MigrationGenerator.Operation do
127
127
end
128
128
129
129
def match_type ( _ ) , do: nil
130
+
131
+ def index_keys ( keys , all_tenants? , multitenancy ) do
132
+ if multitenancy . strategy == :attribute and not all_tenants? do
133
+ [ multitenancy . attribute | keys ]
134
+ else
135
+ keys
136
+ end
137
+ end
130
138
end
131
139
132
140
defmodule CreateTable do
@@ -851,18 +859,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
851
859
schema: schema ,
852
860
multitenancy: multitenancy
853
861
} ) do
854
- keys =
855
- if all_tenants? do
856
- keys
857
- else
858
- case multitenancy . strategy do
859
- :attribute ->
860
- [ multitenancy . attribute | keys ]
861
-
862
- _ ->
863
- keys
864
- end
865
- end
862
+ keys = index_keys ( keys , all_tenants? , multitenancy )
866
863
867
864
index_name = index_name || "#{ table } _#{ name } _index"
868
865
@@ -888,19 +885,12 @@ defmodule AshPostgres.MigrationGenerator.Operation do
888
885
end
889
886
890
887
def down ( % {
891
- identity: % { name: name , keys: keys , index_name: index_name } ,
888
+ identity: % { name: name , keys: keys , index_name: index_name , all_tenants?: all_tenants? } ,
892
889
table: table ,
893
890
schema: schema ,
894
891
multitenancy: multitenancy
895
892
} ) do
896
- keys =
897
- case multitenancy . strategy do
898
- :attribute ->
899
- [ multitenancy . attribute | keys ]
900
-
901
- _ ->
902
- keys
903
- end
893
+ keys = index_keys ( keys , all_tenants? , multitenancy )
904
894
905
895
index_name = index_name || "#{ table } _#{ name } _index"
906
896
@@ -962,12 +952,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
962
952
base_filter: base_filter ,
963
953
multitenancy: multitenancy
964
954
} ) do
965
- keys =
966
- if ! index . all_tenants? and multitenancy . strategy == :attribute do
967
- [ multitenancy . attribute | index . fields ]
968
- else
969
- index . fields
970
- end
955
+ keys = index_keys ( index . fields , index . all_tenants? , multitenancy )
971
956
972
957
index =
973
958
case { index . where , base_filter } do
@@ -997,12 +982,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
997
982
end
998
983
999
984
def down ( % { schema: schema , index: index , table: table , multitenancy: multitenancy } ) do
1000
- keys =
1001
- if ! index . all_tenants? and multitenancy . strategy == :attribute do
1002
- [ multitenancy . attribute | index . fields ]
1003
- else
1004
- index . fields
1005
- end
985
+ keys = index_keys ( index . fields , index . all_tenants? , multitenancy )
1006
986
1007
987
opts =
1008
988
join ( [
@@ -1167,7 +1147,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
1167
1147
@ moduledoc false
1168
1148
defstruct [ :schema , :table , no_phase: true ]
1169
1149
1170
- def up ( % { schema: schema , table: table , multitenancy : multitenancy } ) do
1150
+ def up ( % { schema: schema , table: table , old_multitenancy : multitenancy } ) do
1171
1151
cond do
1172
1152
multitenancy . strategy == :context ->
1173
1153
"drop constraint(#{ inspect ( table ) } , \" #{ table } _pkey\" , prefix: prefix())"
@@ -1305,48 +1285,12 @@ defmodule AshPostgres.MigrationGenerator.Operation do
1305
1285
1306
1286
import Helper
1307
1287
1308
- def up ( % {
1309
- identity: % { name: name , keys: keys , index_name: index_name } ,
1310
- table: table ,
1311
- schema: schema ,
1312
- old_multitenancy: multitenancy
1313
- } ) do
1314
- keys =
1315
- case multitenancy . strategy do
1316
- :attribute ->
1317
- [ multitenancy . attribute | keys ]
1318
-
1319
- _ ->
1320
- keys
1321
- end
1322
-
1323
- index_name = index_name || "#{ table } _#{ name } _index"
1324
-
1325
- "drop_if_exists unique_index(:#{ as_atom ( table ) } , [#{ Enum . map_join ( keys , ", " , & inspect / 1 ) } ], #{ join ( [ "name: \" #{ index_name } \" " , option ( :prefix , schema ) ] ) } )"
1288
+ def up ( operation ) do
1289
+ AddUniqueIndex . down ( % { operation | multitenancy: operation . old_multitenancy } )
1326
1290
end
1327
1291
1328
- def down ( % {
1329
- identity: % { name: name , keys: keys , base_filter: base_filter , index_name: index_name } ,
1330
- table: table ,
1331
- schema: schema ,
1332
- multitenancy: multitenancy
1333
- } ) do
1334
- keys =
1335
- case multitenancy . strategy do
1336
- :attribute ->
1337
- [ multitenancy . attribute | keys ]
1338
-
1339
- _ ->
1340
- keys
1341
- end
1342
-
1343
- index_name = index_name || "#{ table } _#{ name } _index"
1344
-
1345
- if base_filter do
1346
- "create unique_index(:#{ as_atom ( table ) } , [#{ Enum . map_join ( keys , ", " , & inspect / 1 ) } ], where: \" #{ base_filter } \" , #{ join ( [ "name: \" #{ index_name } \" " , option ( :prefix , schema ) ] ) } )"
1347
- else
1348
- "create unique_index(:#{ as_atom ( table ) } , [#{ Enum . map_join ( keys , ", " , & inspect / 1 ) } ], #{ join ( [ "name: \" #{ index_name } \" " , option ( :prefix , schema ) ] ) } )"
1349
- end
1292
+ def down ( operation ) do
1293
+ AddUniqueIndex . up ( % { operation | multitenancy: operation . old_multitenancy } )
1350
1294
end
1351
1295
end
1352
1296
0 commit comments