-
Notifications
You must be signed in to change notification settings - Fork 25
/
FSharpMigrationOperationGenerator.fs
743 lines (648 loc) · 32.2 KB
/
FSharpMigrationOperationGenerator.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
namespace EntityFrameworkCore.FSharp.Migrations.Design
open System
open System.Collections.Generic
open Microsoft.EntityFrameworkCore.Migrations.Operations
open Microsoft.EntityFrameworkCore.Internal
open EntityFrameworkCore.FSharp.SharedTypeExtensions
open EntityFrameworkCore.FSharp.IndentedStringBuilderUtilities
open EntityFrameworkCore.FSharp.Internal
open Microsoft.EntityFrameworkCore.Infrastructure
open Microsoft.EntityFrameworkCore.Migrations
open Microsoft.EntityFrameworkCore.Design
open Microsoft.EntityFrameworkCore.Migrations.Design
type FSharpMigrationOperationGenerator (code : ICSharpHelper) =
let toOnedimensionalArray firstDimension (a : obj[,]) =
Array.init a.Length (fun i -> if firstDimension then a.[i, 0] else a.[0, i])
let sanitiseName name =
if FSharpUtilities.isKeyword name then sprintf "``%s``" name else name
let writeName nameValue sb =
sb |> appendLine (sprintf "name = %s" (code.UnknownLiteral nameValue))
let writeParameter name value sb =
sb |> appendLine (sprintf ",%s = %s" (sanitiseName name) (code.UnknownLiteral value))
let writeParameterIfTrue trueOrFalse name value sb =
if trueOrFalse then
sb |> writeParameter name value
else
sb
let writeOptionalParameter (name:string) value (sb:IndentedStringBuilder) =
sb
|> writeParameterIfTrue (value |> notNull) name (sanitiseName value)
let writeNullableParameterIfValue name (nullableParameter: Nullable<_>) sb =
if nullableParameter.HasValue then
let value = nullableParameter |> code.UnknownLiteral
let fmt = sprintf ", %s = Nullable(%s)" (sanitiseName name) value
sb |> append fmt
else
sb
let annotations (annotations: Annotation seq) (sb:IndentedStringBuilder) =
let lines =
annotations
|> Seq.map (fun a -> sprintf ".Annotation(%s, %s)" (code.Literal a.Name) (code.UnknownLiteral a.Value))
sb |> appendLines lines true
let oldAnnotations (annotations: Annotation seq) (sb:IndentedStringBuilder) =
let lines =
annotations
|> Seq.map (fun a -> sprintf ".OldAnnotation(%s, %s)" (code.Literal a.Name) (code.UnknownLiteral a.Value))
sb |> appendLines lines true
let generateMigrationOperation (op:MigrationOperation) (sb:IndentedStringBuilder) :IndentedStringBuilder =
invalidOp ((op.GetType()) |> DesignStrings.UnknownOperation)
let generateAddColumnOperation (op:AddColumnOperation) (sb:IndentedStringBuilder) =
sb
|> append ".AddColumn<"
|> append (op.ClrType |> unwrapOptionType |> code.Reference)
|> appendLine ">("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> writeOptionalParameter "type" op.ColumnType
|> writeNullableParameterIfValue "unicode" op.IsUnicode
|> writeNullableParameterIfValue "maxLength" op.MaxLength
|> writeNullableParameterIfValue "fixedLength" op.IsFixedLength
|> writeParameterIfTrue op.IsRowVersion "rowVersion" true
|> writeParameter "nullable" op.IsNullable
|>
if not(isNull op.DefaultValueSql) then
writeParameter "defaultValueSql" op.DefaultValueSql
elif not(isNull op.ComputedColumnSql) then
writeParameter "computedColumnSql" op.ComputedColumnSql
elif not(isNull op.DefaultValue) then
writeParameter "defaultValue" op.DefaultValue
else
id
|> append ")"
|> appendIfTrue (op.ClrType |> isOptionType) (sprintf ".SetValueConverter(OptionConverter<%s> ())" (op.ClrType |> unwrapOptionType |> code.Reference))
|> annotations (op.GetAnnotations())
|> unindent
let generateAddForeignKeyOperation (op:AddForeignKeyOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".AddForeignKey("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> writeParameterIfTrue (op.Columns.Length = 1) "column" op.Columns.[0]
|> writeParameterIfTrue (op.Columns.Length <> 1) "columns" op.Columns
|> writeOptionalParameter "principalSchema" op.PrincipalSchema
|> writeParameter "principalTable" op.PrincipalTable
|> writeParameterIfTrue (op.PrincipalColumns.Length = 1) "principalColumn" op.PrincipalColumns.[0]
|> writeParameterIfTrue (op.PrincipalColumns.Length <> 1) "principalColumns" op.PrincipalColumns
|> writeParameterIfTrue (op.OnUpdate <> ReferentialAction.NoAction) "onUpdate" op.OnUpdate
|> writeParameterIfTrue (op.OnDelete <> ReferentialAction.NoAction) "onDelete" op.OnDelete
|> unindent
|> append ")"
|> annotations (op.GetAnnotations())
let generateAddPrimaryKeyOperation (op:AddPrimaryKeyOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".AddPrimaryKey("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> writeParameterIfTrue (op.Columns.Length = 1) "column" op.Columns.[0]
|> writeParameterIfTrue (op.Columns.Length <> 1) "columns" op.Columns
|> unindent
|> append ")"
|> annotations (op.GetAnnotations())
let generateAddUniqueConstraintOperation (op:AddUniqueConstraintOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".AddUniqueConstraint("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> writeParameterIfTrue (op.Columns.Length = 1) "column" op.Columns.[0]
|> writeParameterIfTrue (op.Columns.Length <> 1) "columns" op.Columns
|> unindent
|> append ")"
|> annotations (op.GetAnnotations())
let generateAlterColumnOperation (op:AlterColumnOperation) (sb:IndentedStringBuilder) =
sb
|> append ".AlterColumn<"
|> append (op.ClrType |> code.Reference)
|> appendLine ">("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> writeOptionalParameter "type" op.ColumnType
|> writeNullableParameterIfValue "unicode" op.IsUnicode
|> writeNullableParameterIfValue "maxLength" op.MaxLength
|> writeParameterIfTrue op.IsRowVersion "rowVersion" true
|> writeParameter "nullable" op.IsNullable
|>
if op.DefaultValueSql |> notNull then
writeParameter "defaultValueSql" op.DefaultValueSql
elif op.ComputedColumnSql |> notNull then
writeParameter "computedColumnSql" op.ComputedColumnSql
elif op.DefaultValue |> notNull then
writeParameter "defaultValue" op.DefaultValue
else
id
|>
if op.OldColumn.ClrType |> isNull |> not then
(fun sb -> sb
|> append (sprintf ",oldClrType = typedefof<%s>" (op.OldColumn.ClrType |> code.Reference))
|> appendEmptyLine)
else
id
|> writeOptionalParameter "oldType" op.OldColumn.ColumnType
|> writeNullableParameterIfValue "oldUnicode" op.OldColumn.IsUnicode
|> writeNullableParameterIfValue "oldMaxLength" op.OldColumn.MaxLength
|> writeParameterIfTrue op.OldColumn.IsRowVersion "oldRowVersion" true
|> writeParameter "oldNullable" op.OldColumn.IsNullable
|>
if op.OldColumn.DefaultValueSql |> notNull then
writeParameter "oldDefaultValueSql" op.OldColumn.DefaultValueSql
elif op.OldColumn.ComputedColumnSql |> notNull then
writeParameter "oldComputedColumnSql" op.OldColumn.ComputedColumnSql
elif op.OldColumn.DefaultValue |> notNull then
writeParameter "oldDefaultValue" op.OldColumn.DefaultValue
else
id
|> append ")"
|> appendIfTrue (op.ClrType |> isOptionType) (sprintf ".SetValueConverter(OptionConverter<%s> ())" (op.ClrType |> unwrapOptionType |> code.Reference))
|> annotations (op.GetAnnotations())
|> oldAnnotations (op.OldColumn.GetAnnotations())
|> unindent
let generateAlterDatabaseOperation (op:AlterDatabaseOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".AlterDatabase()"
|> indent
|> annotations (op.GetAnnotations())
|> oldAnnotations (op.OldDatabase.GetAnnotations())
|> unindent
let generateAlterSequenceOperation (op:AlterSequenceOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".AlterSequence("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameterIfTrue (op.IncrementBy <> 1) "incrementBy" op.IncrementBy
|> writeNullableParameterIfValue "minValue " op.MinValue
|> writeNullableParameterIfValue "maxValue " op.MaxValue
|> writeParameterIfTrue op.IsCyclic "cyclic" "true"
|> writeParameterIfTrue (op.OldSequence.IncrementBy <> 1) "oldIncrementBy" op.OldSequence.IncrementBy
|> writeNullableParameterIfValue "oldMinValue " op.OldSequence.MinValue
|> writeNullableParameterIfValue "oldMaxValue " op.OldSequence.MaxValue
|> writeParameterIfTrue op.OldSequence.IsCyclic "oldCyclic" "true"
|> append ")"
|> annotations (op.GetAnnotations())
|> oldAnnotations (op.OldSequence.GetAnnotations())
|> unindent
let generateAlterTableOperation (op:AlterTableOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".AlterTable("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> append ")"
|> annotations (op.GetAnnotations())
|> oldAnnotations (op.OldTable.GetAnnotations())
|> unindent
let generateCreateIndexOperation (op:CreateIndexOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".CreateIndex("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> writeParameterIfTrue (op.Columns.Length = 1) "column" op.Columns.[0]
|> writeParameterIfTrue (op.Columns.Length <> 1) "columns" op.Columns
|> writeParameterIfTrue op.IsUnique "unique" true
|> writeOptionalParameter "filter" op.Filter
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateEnsureSchemaOperation (op:EnsureSchemaOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".EnsureSchema("
|> indent
|> writeName op.Name
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateCreateSequenceOperation (op:CreateSequenceOperation) (sb:IndentedStringBuilder) =
sb
|> append ".CreateSequence"
|>
if op.ClrType <> typedefof<Int64> then
append (sprintf "<%s>" (op.ClrType |> code.Reference))
else
id
|> appendLine "("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameterIfTrue (op.StartValue <> 1L) "startValue" op.StartValue
|> writeParameterIfTrue (op.IncrementBy <> 1) "incrementBy" op.IncrementBy
|> writeNullableParameterIfValue "minValue " op.MinValue
|> writeNullableParameterIfValue "maxValue " op.MaxValue
|> writeParameterIfTrue op.IsCyclic "cyclic" "true"
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateCreateTableOperation (op:CreateTableOperation) (sb:IndentedStringBuilder) =
let map = Dictionary<string, string>()
let writeColumn (c:AddColumnOperation) =
let propertyName = c.Name |> code.Identifier
map.Add(c.Name, propertyName)
sb
|> appendLine (sprintf "%s =" propertyName)
|> indent
|> append "table.Column<"
|> append (code.Reference c.ClrType)
|> appendLine ">("
|> indent
|> appendLine (sprintf "nullable = %s" (code.Literal c.IsNullable))
|> writeParameterIfTrue (c.Name <> propertyName) "name" c.Name
|> writeParameterIfTrue (c.ColumnType |> notNull) "type" c.ColumnType
|> writeNullableParameterIfValue "unicode" c.IsUnicode
|> writeNullableParameterIfValue "maxLength" c.MaxLength
|> writeParameterIfTrue (c.IsRowVersion) "rowVersion" c.IsRowVersion
|>
if c.DefaultValueSql |> notNull then
appendLine (sprintf ", defaultValueSql = %s" (code.Literal c.DefaultValueSql))
elif c.ComputedColumnSql |> notNull then
appendLine (sprintf ", computedColumnSql = %s" (code.Literal c.ComputedColumnSql))
elif c.DefaultValue |> notNull then
appendLine (sprintf ", defaultValue = %s" (code.UnknownLiteral c.DefaultValue))
else
id
|> unindent
|> append ")"
|> appendIfTrue (c.ClrType |> isOptionType) (sprintf ".SetValueConverter(OptionConverter<%s> ())" (c.ClrType |> unwrapOptionType |> code.Reference))
|> indent
|> annotations (c.GetAnnotations())
|> unindent
|> unindent
|> appendEmptyLine
|> ignore
let writeColumns sb =
sb
|> appendLine ",columns = (fun table -> "
|> appendLine "{|"
|> indent
|> ignore
op.Columns |> Seq.filter notNull |> Seq.iter writeColumn
sb
|> unindent
|> appendLine "|})"
let writeUniqueConstraint (uc:AddUniqueConstraintOperation) =
sb
|> append "table.UniqueConstraint("
|> append (uc.Name |> code.Literal)
|> append ", "
|> append (uc.Columns |> Seq.map(fun c -> map.[c]) |> Seq.toList |> code.Lambda)
|> append ")"
|> indent
|> annotations (op.PrimaryKey.GetAnnotations())
|> appendLine " |> ignore"
|> unindent
|> ignore
let writeForeignKeyConstraint (fk:AddForeignKeyOperation) =
sb
|> appendLine "table.ForeignKey("
|> indent
|> append "name = " |> append (fk.Name |> code.Literal) |> appendLine ","
|> append (if fk.Columns.Length = 1 then "column = " else "columns = ")
|> appendLine (fk.Columns |> Seq.map(fun c -> map.[c]) |> Seq.toList |> code.Lambda)
|> writeParameterIfTrue (fk.PrincipalSchema |> notNull) "principalSchema" fk.PrincipalSchema
|> writeParameter "principalTable" fk.PrincipalTable
|> writeParameterIfTrue (fk.PrincipalColumns.Length = 1) "principalColumn" fk.PrincipalColumns.[0]
|> writeParameterIfTrue (fk.PrincipalColumns.Length <> 1) "principalColumns" fk.PrincipalColumns
|> writeParameterIfTrue (fk.OnUpdate <> ReferentialAction.NoAction) "onUpdate" fk.OnUpdate
|> writeParameterIfTrue (fk.OnDelete <> ReferentialAction.NoAction) "onDelete" fk.OnDelete
|> append ")"
|> annotations (fk.GetAnnotations())
|> appendLine " |> ignore"
|> appendEmptyLine
|> unindent
|> ignore
()
let writeConstraints sb =
let hasConstraints =
notNull op.PrimaryKey && (op.UniqueConstraints |> Seq.isEmpty |> not) && (op.ForeignKeys |> Seq.isEmpty |> not)
if hasConstraints then
sb |> append "," |> appendLine "constraints ="
|> indent
|> appendLine "(fun table -> "
|> indent
|> ignore
if notNull op.PrimaryKey then
let pkName = op.PrimaryKey.Name |> code.Literal
let pkColumns = op.PrimaryKey.Columns |> Seq.map(fun c -> map.[c]) |> Seq.toList |> code.Lambda
sb
|> append (sprintf "table.PrimaryKey(%s, %s)" pkName pkColumns)
|> annotations (op.PrimaryKey.GetAnnotations())
|> appendLine " |> ignore"
|> ignore
op.UniqueConstraints |> Seq.iter writeUniqueConstraint
op.ForeignKeys |> Seq.iter writeForeignKeyConstraint
sb
|> unindent
|> appendLine ") "
|> unindent
else
sb
sb
|> appendLine ".CreateTable("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeColumns
|> writeConstraints
|> unindent
|> append ")"
|> annotations (op.GetAnnotations())
let generateDropColumnOperation (op:DropColumnOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".DropColumn("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateDropForeignKeyOperation (op:DropForeignKeyOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".DropForeignKey("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateDropIndexOperation (op:DropIndexOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".DropIndex("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateDropPrimaryKeyOperation (op:DropPrimaryKeyOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".DropPrimaryKey("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateDropSchemaOperation (op:DropSchemaOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".DropSchema("
|> indent
|> writeName op.Name
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateDropSequenceOperation (op:DropSequenceOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".DropSequence("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateDropTableOperation (op:DropTableOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".DropTable("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateDropUniqueConstraintOperation (op:DropUniqueConstraintOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".DropUniqueConstraint("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateRenameColumnOperation (op:RenameColumnOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".RenameColumn("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> writeParameter "newName" op.NewName
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateRenameIndexOperation (op:RenameIndexOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".RenameIndex("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "table" op.Table
|> writeParameter "newName" op.NewName
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateRenameSequenceOperation (op:RenameSequenceOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".RenameSequence("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "newName" op.NewName
|> writeParameter "newSchema" op.NewSchema
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateRenameTableOperation (op:RenameTableOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".RenameTable("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "newName" op.NewName
|> writeParameter "newSchema" op.NewSchema
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateRestartSequenceOperation (op:RestartSequenceOperation) (sb:IndentedStringBuilder) =
sb
|> appendLine ".RestartSequence("
|> indent
|> writeName op.Name
|> writeOptionalParameter "schema" op.Schema
|> writeParameter "startValue" op.StartValue
|> append ")"
|> annotations (op.GetAnnotations())
|> unindent
let generateSqlOperation (op:SqlOperation) (sb:IndentedStringBuilder) =
sb
|> append (sprintf ".Sql(%s)" (op.Sql |> code.Literal))
|> indent
|> annotations (op.GetAnnotations())
|> unindent
let generateInsertDataOperation (op:InsertDataOperation) (sb:IndentedStringBuilder) =
let parameters =
seq {
if notNull op.Schema then
yield sprintf "schema = %s," (op.Schema |> code.Literal)
yield sprintf "table = %s," (op.Table |> code.Literal)
if op.Columns.Length = 1 then
yield sprintf "column = %s," (op.Columns.[0] |> code.Literal)
else
yield sprintf "columns = %s," (op.Columns |> code.Literal)
let length0 = op.Values.GetLength(0)
let length1 = op.Values.GetLength(1)
let valuesArray =
if length0 = 1 && length1 = 1 then
sprintf "value = %s :> obj" (op.Values.[0,0] |> code.UnknownLiteral)
elif length0 = 1 then
sprintf "values = %s" (op.Values |> toOnedimensionalArray false |> code.Literal)
elif length1 = 1 then
let arr = op.Values |> toOnedimensionalArray true
let lines = code.Literal(arr, true)
sprintf "values = %s" lines
else
sprintf "values = %s" (op.Values |> code.Literal)
// Every item in the array needs to be downcast to obj
// Possibly a tidier way of doing this?
yield (valuesArray
.Replace(";", " :> obj;")
.Replace(" |]", ":> obj |]")
.Replace("null :> obj", "null"))
}
sb
|> appendLine ".InsertData("
|> indent
|> appendLines parameters false
|> unindent
|> append ")"
let generateDeleteDataOperation (op:DeleteDataOperation) (sb:IndentedStringBuilder) =
let parameters =
seq {
if notNull op.Schema then
yield sprintf "schema = %s, " (op.Schema |> code.Literal)
yield sprintf "table = %s, " (op.Table |> code.Literal)
if op.KeyColumns.Length = 1 then
yield sprintf "keyColumn = %s, " (op.KeyColumns.[0] |> code.Literal)
else
yield sprintf "keyColumns = %s, " (op.KeyColumns |> code.Literal)
let length0 = op.KeyValues.GetLength(0)
let length1 = op.KeyValues.GetLength(1)
if length0 = 1 && length1 = 1 then
yield sprintf "keyValue = %s" (op.KeyValues.[0,0] |> code.UnknownLiteral)
elif length0 = 1 then
yield sprintf "keyValues = %s" (op.KeyValues |> toOnedimensionalArray false |> code.Literal)
elif length1 = 1 then
let arr = op.KeyValues |> toOnedimensionalArray true
let lines = code.Literal(arr, true)
yield sprintf "keyValues = %s" lines
else
yield sprintf "keyValues = %s" (op.KeyValues |> code.Literal)
}
sb
|> appendLine ".DeleteData("
|> indent
|> appendLines parameters false
|> unindent
|> appendLine ")"
let generateUpdateDataOperation (op:UpdateDataOperation) (sb:IndentedStringBuilder) =
let parameters =
seq {
if notNull op.Schema then
yield sprintf "schema = %s, " (op.Schema |> code.Literal)
yield sprintf "table = %s, " (op.Table |> code.Literal)
if op.KeyColumns.Length = 1 then
yield sprintf "keyColumn = %s, " (op.KeyColumns.[0] |> code.Literal)
else
yield sprintf "keyColumns = %s, " (op.KeyColumns |> code.Literal)
let length0 = op.KeyValues.GetLength(0)
let length1 = op.KeyValues.GetLength(1)
if length0 = 1 && length1 = 1 then
yield sprintf "keyValue = %s" (op.KeyValues.[0,0] |> code.UnknownLiteral)
elif length0 = 1 then
yield sprintf "keyValues = %s" (op.KeyValues |> toOnedimensionalArray false |> code.Literal)
elif length1 = 1 then
let arr = op.KeyValues |> toOnedimensionalArray true
let lines = code.Literal(arr, true)
yield sprintf "keyValues = %s" lines
else
yield sprintf "keyValues = %s" (op.KeyValues |> code.Literal)
if op.Columns.Length = 1 then
yield sprintf "column = %s, " (op.Columns.[0] |> code.Literal)
else
yield sprintf "columns = %s, " (op.Columns |> code.Literal)
let length0 = op.Values.GetLength(0)
let length1 = op.Values.GetLength(1)
if length0 = 1 && length1 = 1 then
yield sprintf "value = %s" (op.Values.[0,0] |> code.UnknownLiteral)
elif length0 = 1 then
yield sprintf "values = %s" (op.Values |> toOnedimensionalArray false |> code.Literal)
elif length1 = 1 then
let arr = op.Values |> toOnedimensionalArray true
let lines = code.Literal(arr, true)
yield sprintf "values = %s" lines
else
yield sprintf "values = %s" (op.Values |> code.Literal)
}
sb
|> appendLine ".UpdateData("
|> indent
|> appendLines parameters false
|> unindent
|> appendLine ")"
let generateOperation (op:MigrationOperation) =
match op with
| :? AddColumnOperation as op' -> op' |> generateAddColumnOperation
| :? AddForeignKeyOperation as op' -> op' |> generateAddForeignKeyOperation
| :? AddPrimaryKeyOperation as op' -> op' |> generateAddPrimaryKeyOperation
| :? AddUniqueConstraintOperation as op' -> op' |> generateAddUniqueConstraintOperation
| :? AlterColumnOperation as op' -> op' |> generateAlterColumnOperation
| :? AlterDatabaseOperation as op' -> op' |> generateAlterDatabaseOperation
| :? AlterSequenceOperation as op' -> op' |> generateAlterSequenceOperation
| :? AlterTableOperation as op' -> op' |> generateAlterTableOperation
| :? CreateIndexOperation as op' -> op' |> generateCreateIndexOperation
| :? EnsureSchemaOperation as op' -> op' |> generateEnsureSchemaOperation
| :? CreateSequenceOperation as op' -> op' |> generateCreateSequenceOperation
| :? CreateTableOperation as op' -> op' |> generateCreateTableOperation
| :? DropColumnOperation as op' -> op' |> generateDropColumnOperation
| :? DropForeignKeyOperation as op' -> op' |> generateDropForeignKeyOperation
| :? DropIndexOperation as op' -> op' |> generateDropIndexOperation
| :? DropPrimaryKeyOperation as op' -> op' |> generateDropPrimaryKeyOperation
| :? DropSchemaOperation as op' -> op' |> generateDropSchemaOperation
| :? DropSequenceOperation as op' -> op' |> generateDropSequenceOperation
| :? DropTableOperation as op' -> op' |> generateDropTableOperation
| :? DropUniqueConstraintOperation as op' -> op' |> generateDropUniqueConstraintOperation
| :? RenameColumnOperation as op' -> op' |> generateRenameColumnOperation
| :? RenameIndexOperation as op' -> op' |> generateRenameIndexOperation
| :? RenameSequenceOperation as op' -> op' |> generateRenameSequenceOperation
| :? RenameTableOperation as op' -> op' |> generateRenameTableOperation
| :? RestartSequenceOperation as op' -> op' |> generateRestartSequenceOperation
| :? SqlOperation as op' -> op' |> generateSqlOperation
| :? InsertDataOperation as op' -> op' |> generateInsertDataOperation
| :? DeleteDataOperation as op' -> op' |> generateDeleteDataOperation
| :? UpdateDataOperation as op' -> op' |> generateUpdateDataOperation
| _ -> op |> generateMigrationOperation // The failure case
let generate (builderName:string) (operations: MigrationOperation seq) (sb:IndentedStringBuilder) =
operations
|> Seq.iter(fun op ->
sb
|> append builderName
|> generateOperation op
|> appendLine " |> ignore"
|> appendEmptyLine
|> ignore
)
interface ICSharpMigrationOperationGenerator with
member this.Generate(builderName, operations, builder) =
generate builderName operations builder