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