@@ -66,6 +66,53 @@ defmodule AshPostgres.MigrationGenerator.Operation do
66
66
def reference_type ( % { type: type } , _ ) do
67
67
type
68
68
end
69
+
70
+ def with_match ( reference , source_attribute \\ nil )
71
+
72
+ def with_match (
73
+ % {
74
+ primary_key?: false ,
75
+ destination_attribute: reference_attribute ,
76
+ multitenancy: % { strategy: :attribute , attribute: destination_attribute }
77
+ } = reference ,
78
+ source_attribute
79
+ )
80
+ when not is_nil ( source_attribute ) and reference_attribute != destination_attribute do
81
+ with_targets =
82
+ [ { as_atom ( source_attribute ) , as_atom ( destination_attribute ) } ]
83
+ |> Enum . into ( reference . match_with || % { } )
84
+ |> with_targets ( )
85
+
86
+ # We can only have match: :full here, this gets validated by a Transformer
87
+ join ( [ with_targets , "match: :full" ] )
88
+ end
89
+
90
+ def with_match ( reference , _ ) do
91
+ with_targets = with_targets ( reference . match_with )
92
+ match_type = match_type ( reference . match_type )
93
+
94
+ if with_targets != nil or match_type != nil do
95
+ join ( [ with_targets , match_type ] )
96
+ else
97
+ nil
98
+ end
99
+ end
100
+
101
+ def with_targets ( targets ) when is_map ( targets ) do
102
+ targets_string =
103
+ targets
104
+ |> Enum . map_join ( ", " , fn { source , destination } -> "#{ source } : :#{ destination } " end )
105
+
106
+ "with: [#{ targets_string } ]"
107
+ end
108
+
109
+ def with_targets ( _ ) , do: nil
110
+
111
+ def match_type ( type ) when type in [ :simple , :partial , :full ] do
112
+ "match: :#{ type } "
113
+ end
114
+
115
+ def match_type ( _ ) , do: nil
69
116
end
70
117
71
118
defmodule CreateTable do
@@ -88,14 +135,11 @@ defmodule AshPostgres.MigrationGenerator.Operation do
88
135
table: table ,
89
136
destination_attribute: reference_attribute ,
90
137
schema: destination_schema ,
91
- multitenancy: % { strategy: :attribute , attribute: destination_attribute }
138
+ multitenancy: % { strategy: :attribute }
92
139
} = reference
93
140
} = attribute
94
141
} ) do
95
- with_match =
96
- if ! reference . primary_key? && destination_attribute != reference_attribute do
97
- "with: [#{ as_atom ( source_attribute ) } : :#{ as_atom ( destination_attribute ) } ], match: :full"
98
- end
142
+ with_match = with_match ( reference , source_attribute )
99
143
100
144
size =
101
145
if attribute [ :size ] do
@@ -136,6 +180,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do
136
180
} = reference
137
181
} = attribute
138
182
} ) do
183
+ with_match = with_match ( reference )
184
+
139
185
size =
140
186
if attribute [ :size ] do
141
187
"size: #{ attribute [ :size ] } "
@@ -146,6 +192,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
146
192
"references(:#{ as_atom ( table ) } " ,
147
193
[
148
194
"column: #{ inspect ( destination_attribute ) } " ,
195
+ with_match ,
149
196
option ( "prefix" , destination_schema ) ,
150
197
"name: #{ inspect ( reference . name ) } " ,
151
198
"type: #{ inspect ( reference_type ( attribute , reference ) ) } " ,
@@ -198,6 +245,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do
198
245
} = reference
199
246
} = attribute
200
247
} ) do
248
+ with_match = with_match ( reference )
249
+
201
250
size =
202
251
if attribute [ :size ] do
203
252
"size: #{ attribute [ :size ] } "
@@ -208,6 +257,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
208
257
"references(:#{ as_atom ( table ) } " ,
209
258
[
210
259
"column: #{ inspect ( destination_attribute ) } " ,
260
+ with_match ,
211
261
"name: #{ inspect ( reference . name ) } " ,
212
262
"type: #{ inspect ( reference_type ( attribute , reference ) ) } " ,
213
263
"prefix: prefix()" ,
@@ -236,6 +286,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do
236
286
} = reference
237
287
} = attribute
238
288
} ) do
289
+ with_match = with_match ( reference )
290
+
239
291
size =
240
292
if attribute [ :size ] do
241
293
"size: #{ attribute [ :size ] } "
@@ -251,6 +303,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
251
303
"references(:#{ as_atom ( table ) } " ,
252
304
[
253
305
"column: #{ inspect ( destination_attribute ) } " ,
306
+ with_match ,
254
307
"name: #{ inspect ( reference . name ) } " ,
255
308
"type: #{ inspect ( reference_type ( attribute , reference ) ) } " ,
256
309
option ( "prefix" , destination_schema ) ,
@@ -277,6 +330,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do
277
330
} = reference
278
331
} = attribute
279
332
} ) do
333
+ with_match = with_match ( reference )
334
+
280
335
size =
281
336
if attribute [ :size ] do
282
337
"size: #{ attribute [ :size ] } "
@@ -287,6 +342,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
287
342
"references(:#{ as_atom ( table ) } " ,
288
343
[
289
344
"column: #{ inspect ( destination_attribute ) } " ,
345
+ with_match ,
290
346
"name: #{ inspect ( reference . name ) } " ,
291
347
"type: #{ inspect ( reference_type ( attribute , reference ) ) } " ,
292
348
option ( "prefix" , destination_schema ) ,
@@ -449,13 +505,16 @@ defmodule AshPostgres.MigrationGenerator.Operation do
449
505
} = attribute ,
450
506
_schema
451
507
) do
508
+ with_match = with_match ( reference )
509
+
452
510
size =
453
511
if attribute [ :size ] do
454
512
"size: #{ attribute [ :size ] } "
455
513
end
456
514
457
515
join ( [
458
516
"references(:#{ as_atom ( table ) } , column: #{ inspect ( destination_attribute ) } " ,
517
+ with_match ,
459
518
"name: #{ inspect ( reference . name ) } " ,
460
519
"type: #{ inspect ( reference_type ( attribute , reference ) ) } " ,
461
520
size ,
@@ -471,7 +530,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
471
530
% {
472
531
references:
473
532
% {
474
- multitenancy: % { strategy: :attribute , attribute: destination_attribute } ,
533
+ multitenancy: % { strategy: :attribute } ,
475
534
table: table ,
476
535
schema: destination_schema ,
477
536
destination_attribute: reference_attribute
@@ -484,10 +543,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
484
543
destination_schema
485
544
end
486
545
487
- with_match =
488
- if ! reference . primary_key? && destination_attribute != reference_attribute do
489
- "with: [#{ as_atom ( source_attribute ) } : :#{ as_atom ( destination_attribute ) } ], match: :full"
490
- end
546
+ with_match = with_match ( reference , source_attribute )
491
547
492
548
size =
493
549
if attribute [ :size ] do
@@ -519,6 +575,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do
519
575
} = attribute ,
520
576
schema
521
577
) do
578
+ with_match = with_match ( reference )
579
+
522
580
size =
523
581
if attribute [ :size ] do
524
582
"size: #{ attribute [ :size ] } "
@@ -531,6 +589,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
531
589
532
590
join ( [
533
591
"references(:#{ as_atom ( table ) } , column: #{ inspect ( destination_attribute ) } " ,
592
+ with_match ,
534
593
"name: #{ inspect ( reference . name ) } " ,
535
594
"type: #{ inspect ( reference_type ( attribute , reference ) ) } " ,
536
595
size ,
@@ -553,6 +612,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do
553
612
} = attribute ,
554
613
schema
555
614
) do
615
+ with_match = with_match ( reference )
616
+
556
617
destination_schema =
557
618
if schema != destination_schema do
558
619
destination_schema
@@ -565,6 +626,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
565
626
566
627
join ( [
567
628
"references(:#{ as_atom ( table ) } , column: #{ inspect ( destination_attribute ) } " ,
629
+ with_match ,
568
630
"name: #{ inspect ( reference . name ) } " ,
569
631
"type: #{ inspect ( reference_type ( attribute , reference ) ) } " ,
570
632
size ,
0 commit comments