コード例 #1
0
        public void Can_get_and_set_table_and_annotations()
        {
            var operation = new AlterTableOperation("T", null);

            Assert.Equal("T", operation.Name);
            Assert.Empty(operation.Annotations);

            operation = new AlterTableOperation(
                "T", new Dictionary<string, AnnotationValues> { { "AT1", new AnnotationValues("VT1", "VT2") } });

            Assert.Equal("T", operation.Name);
            Assert.Equal("VT1", operation.Annotations["AT1"].OldValue);
            Assert.Equal("VT2", operation.Annotations["AT1"].NewValue);
        }
コード例 #2
0
        public void Inverse_should_produce_inverse_alter_operation()
        {
            var operation = new AlterTableOperation(
                "T", new Dictionary<string, AnnotationValues>
                {
                    { "AT1", new AnnotationValues("VT1A", "VT1B") },
                    { "AT2", new AnnotationValues("VT2A", "VT2B") }
                });

            operation.Columns.Add(
                new ColumnModel(PrimitiveTypeKind.Int64)
                {
                    Name = "C1",
                    Annotations = new Dictionary<string, AnnotationValues>
                    {
                        { "AC1A", new AnnotationValues("VC1A1", "VC1A2") },
                        { "AC1B", new AnnotationValues("VC1B1", "VC1B2") }
                    }
                });

            operation.Columns.Add(
                new ColumnModel(PrimitiveTypeKind.Int64)
                {
                    Name = "C2",
                    Annotations = new Dictionary<string, AnnotationValues>
                    {
                        { "AC2A", new AnnotationValues("VC2A1", "VC2A2") },
                        { "AC2B", new AnnotationValues("VC2B1", "VC2B2") }
                    }
                });

            var inverse = (AlterTableOperation)operation.Inverse;

            Assert.Equal("T", inverse.Name);

            Assert.Equal("VT1B", inverse.Annotations["AT1"].OldValue);
            Assert.Equal("VT1A", inverse.Annotations["AT1"].NewValue);
            Assert.Equal("VT2B", inverse.Annotations["AT2"].OldValue);
            Assert.Equal("VT2A", inverse.Annotations["AT2"].NewValue);

            Assert.Equal("VC1A1", inverse.Columns.Single(c => c.Name == "C1").Annotations["AC1A"].OldValue);
            Assert.Equal("VC1A2", inverse.Columns.Single(c => c.Name == "C1").Annotations["AC1A"].NewValue);
            Assert.Equal("VC1B1", inverse.Columns.Single(c => c.Name == "C1").Annotations["AC1B"].OldValue);
            Assert.Equal("VC1B2", inverse.Columns.Single(c => c.Name == "C1").Annotations["AC1B"].NewValue);
            Assert.Equal("VC2A1", inverse.Columns.Single(c => c.Name == "C2").Annotations["AC2A"].OldValue);
            Assert.Equal("VC2A2", inverse.Columns.Single(c => c.Name == "C2").Annotations["AC2A"].NewValue);
            Assert.Equal("VC2B1", inverse.Columns.Single(c => c.Name == "C2").Annotations["AC2B"].OldValue);
            Assert.Equal("VC2B2", inverse.Columns.Single(c => c.Name == "C2").Annotations["AC2B"].NewValue);
        }
コード例 #3
0
        public void Inverse_should_produce_inverse_alter_operation()
        {
            var operation = new AlterTableOperation(
                "T", new Dictionary <string, AnnotationValues>
            {
                { "AT1", new AnnotationValues("VT1A", "VT1B") },
                { "AT2", new AnnotationValues("VT2A", "VT2B") }
            });

            operation.Columns.Add(
                new ColumnModel(PrimitiveTypeKind.Int64)
            {
                Name        = "C1",
                Annotations = new Dictionary <string, AnnotationValues>
                {
                    { "AC1A", new AnnotationValues("VC1A1", "VC1A2") },
                    { "AC1B", new AnnotationValues("VC1B1", "VC1B2") }
                }
            });

            operation.Columns.Add(
                new ColumnModel(PrimitiveTypeKind.Int64)
            {
                Name        = "C2",
                Annotations = new Dictionary <string, AnnotationValues>
                {
                    { "AC2A", new AnnotationValues("VC2A1", "VC2A2") },
                    { "AC2B", new AnnotationValues("VC2B1", "VC2B2") }
                }
            });

            var inverse = (AlterTableOperation)operation.Inverse;

            Assert.Equal("T", inverse.Name);

            Assert.Equal("VT1B", inverse.Annotations["AT1"].OldValue);
            Assert.Equal("VT1A", inverse.Annotations["AT1"].NewValue);
            Assert.Equal("VT2B", inverse.Annotations["AT2"].OldValue);
            Assert.Equal("VT2A", inverse.Annotations["AT2"].NewValue);

            Assert.Equal("VC1A1", inverse.Columns.Single(c => c.Name == "C1").Annotations["AC1A"].OldValue);
            Assert.Equal("VC1A2", inverse.Columns.Single(c => c.Name == "C1").Annotations["AC1A"].NewValue);
            Assert.Equal("VC1B1", inverse.Columns.Single(c => c.Name == "C1").Annotations["AC1B"].OldValue);
            Assert.Equal("VC1B2", inverse.Columns.Single(c => c.Name == "C1").Annotations["AC1B"].NewValue);
            Assert.Equal("VC2A1", inverse.Columns.Single(c => c.Name == "C2").Annotations["AC2A"].OldValue);
            Assert.Equal("VC2A2", inverse.Columns.Single(c => c.Name == "C2").Annotations["AC2A"].NewValue);
            Assert.Equal("VC2B1", inverse.Columns.Single(c => c.Name == "C2").Annotations["AC2B"].OldValue);
            Assert.Equal("VC2B2", inverse.Columns.Single(c => c.Name == "C2").Annotations["AC2B"].NewValue);
        }
コード例 #4
0
        public void Can_get_and_set_table_and_annotations()
        {
            var operation = new AlterTableOperation("T", null);

            Assert.Equal("T", operation.Name);
            Assert.Empty(operation.Annotations);

            operation = new AlterTableOperation(
                "T", new Dictionary <string, AnnotationValues> {
                { "AT1", new AnnotationValues("VT1", "VT2") }
            });

            Assert.Equal("T", operation.Name);
            Assert.Equal("VT1", operation.Annotations["AT1"].OldValue);
            Assert.Equal("VT2", operation.Annotations["AT1"].NewValue);
        }
コード例 #5
0
 protected virtual IEnumerable<MigrationStatement> Generate(AlterTableOperation operation)
 {
     // Nothing to do since there is no inherent semantics associated with annotations
     yield break;
 }
コード例 #6
0
        /// <summary>
        /// Generates code for an <see cref="AlterTableOperation"/>.
        /// </summary>
        /// <param name="alterTableOperation">The operation for which code should be generated.</param>
        /// <param name="writer">The writer to which generated code should be written.</param>
        protected internal virtual void Generate(AlterTableOperation alterTableOperation, IndentedTextWriter writer)
        {
            Check.NotNull(alterTableOperation, "alterTableOperation");
            Check.NotNull(writer, "writer");

            writer.WriteLine("AlterTableAnnotations(");
            writer.Indent++;
            writer.Write(Quote(alterTableOperation.Name));
            writer.WriteLine(",");
            writer.WriteLine("Function(c) New With");
            writer.Indent++;
            writer.WriteLine("{");
            writer.Indent++;

            var columnCount = alterTableOperation.Columns.Count();

            alterTableOperation.Columns.Each(
                (c, i) =>
                {
                    var scrubbedName = ScrubName(c.Name);

                    writer.Write(".");
                    writer.Write(scrubbedName);
                    writer.Write(" =");
                    Generate(c, writer, !string.Equals(c.Name, scrubbedName, StringComparison.Ordinal));

                    if (i < columnCount - 1)
                    {
                        writer.Write(",");
                    }

                    writer.WriteLine();
                });

            writer.Indent--;
            writer.Write("}");
            writer.Indent--;

            if (alterTableOperation.Annotations.Any())
            {
                writer.WriteLine(",");
                writer.Write("annotations := ");
                GenerateAnnotations(alterTableOperation.Annotations, writer);
            }

            writer.Write(")");

            writer.WriteLine();
            writer.Indent--;
            writer.WriteLine();
        }
コード例 #7
0
        /// <summary>
        /// Override this method to generate SQL when the definition of a table or its attributes are changed.
        /// The default implementation of this method does nothing.
        /// </summary>
        /// <param name="alterTableOperation"> The operation describing changes to the table. </param>
        protected internal virtual void Generate(AlterTableOperation alterTableOperation)
        {
            Check.NotNull(alterTableOperation, "alterTableOperation");

            // Nothing to do since there is no inherent semantics associated with annotations
        }
コード例 #8
0
        public void Can_generate_AlterTableAnnotations_with_annotations()
        {
            var operation = new AlterTableOperation(
                "Customers",
                new Dictionary<string, AnnotationValues>
                {
                    { "AT1", new AnnotationValues(null, "VT1") },
                    {
                        CollationAttribute.AnnotationName,
                        new AnnotationValues(
                            new CollationAttribute("At a reasonable volume..."),
                            new CollationAttribute("While I'm collating..."))
                    },
                    { "AT2", new AnnotationValues(null, "VT2") }

                });

            var idColumn = new ColumnModel(PrimitiveTypeKind.Int32)
            {
                Name = "I.d",
                IsNullable = true,
                IsIdentity = true,
                Annotations =
                    new Dictionary<string, AnnotationValues>
                    {
                        { "A1", new AnnotationValues(null, "V1") },
                        { "A2", new AnnotationValues(null, "V2") }
                    }
            };
            operation.Columns.Add(idColumn);

            operation.Columns.Add(
                new ColumnModel(PrimitiveTypeKind.String)
                {
                    Name = "Name",
                    IsNullable = false,
                    Annotations =
                        new Dictionary<string, AnnotationValues>
                        {
                            {
                                CollationAttribute.AnnotationName,
                                new AnnotationValues(
                                    new CollationAttribute("At a reasonable volume..."),
                                    new CollationAttribute("While I'm collating..."))
                            }
                        }
                });

            var operations = new[] { operation };

            var generator = new CSharpMigrationCodeGenerator();
            generator.AnnotationGenerators[CollationAttribute.AnnotationName] = () => new CollationCSharpCodeGenerator();
            var generatedMigration = generator.Generate("Migration", operations, "Source", "Target", "MyNamespace", "MyMigration");

            Assert.Equal(
                @"namespace MyNamespace
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity.Infrastructure.Annotations;
    using System.Data.Entity.Migrations;
    using System.Data.Entity.TestHelpers;
    
    public partial class MyMigration : DbMigration
    {
        public override void Up()
        {
            AlterTableAnnotations(
                ""Customers"",
                c => new
                    {
                        Id = c.Int(name: ""I.d"", identity: true,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    ""A1"",
                                    new AnnotationValues(oldValue: null, newValue: ""V1"")
                                },
                                { 
                                    ""A2"",
                                    new AnnotationValues(oldValue: null, newValue: ""V2"")
                                },
                            }),
                        Name = c.String(nullable: false,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    ""Collation"",
                                    new AnnotationValues(oldValue: new CollationAttribute(""At a reasonable volume...""), newValue: new CollationAttribute(""While I'm collating...""))
                                },
                            }),
                    },
                annotations: new Dictionary<string, AnnotationValues>
                {
                    { 
                        ""AT1"",
                        new AnnotationValues(oldValue: null, newValue: ""VT1"")
                    },
                    { 
                        ""AT2"",
                        new AnnotationValues(oldValue: null, newValue: ""VT2"")
                    },
                    { 
                        ""Collation"",
                        new AnnotationValues(oldValue: new CollationAttribute(""At a reasonable volume...""), newValue: new CollationAttribute(""While I'm collating...""))
                    },
                });
            
        }
        
        public override void Down()
        {
            AlterTableAnnotations(
                ""Customers"",
                c => new
                    {
                        Id = c.Int(name: ""I.d"", identity: true,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    ""A1"",
                                    new AnnotationValues(oldValue: null, newValue: ""V1"")
                                },
                                { 
                                    ""A2"",
                                    new AnnotationValues(oldValue: null, newValue: ""V2"")
                                },
                            }),
                        Name = c.String(nullable: false,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    ""Collation"",
                                    new AnnotationValues(oldValue: new CollationAttribute(""At a reasonable volume...""), newValue: new CollationAttribute(""While I'm collating...""))
                                },
                            }),
                    },
                annotations: new Dictionary<string, AnnotationValues>
                {
                    { 
                        ""AT1"",
                        new AnnotationValues(oldValue: ""VT1"", newValue: null)
                    },
                    { 
                        ""AT2"",
                        new AnnotationValues(oldValue: ""VT2"", newValue: null)
                    },
                    { 
                        ""Collation"",
                        new AnnotationValues(oldValue: new CollationAttribute(""While I'm collating...""), newValue: new CollationAttribute(""At a reasonable volume...""))
                    },
                });
            
        }
    }
}
",
                generatedMigration.UserCode);
        }
        public void Can_generate_AlterTableAnnotations_with_annotations()
        {
            var operation = new AlterTableOperation(
                "Customers",
                new Dictionary<string, AnnotationValues>
                {
                    { "AT1", new AnnotationValues(null, "VT1") },
                    {
                        CollationAttribute.AnnotationName,
                        new AnnotationValues(
                            new CollationAttribute("At a reasonable volume..."),
                            new CollationAttribute("While I'm collating..."))
                    },
                    { "AT2", new AnnotationValues(null, "VT2") }

                });

            var idColumn = new ColumnModel(PrimitiveTypeKind.Int32)
            {
                Name = "I.d",
                IsNullable = true,
                IsIdentity = true,
                Annotations =
                    new Dictionary<string, AnnotationValues>
                    {
                        { "A1", new AnnotationValues(null, "V1") },
                        { "A2", new AnnotationValues(null, "V2") }
                    }
            };
            operation.Columns.Add(idColumn);

            operation.Columns.Add(
                new ColumnModel(PrimitiveTypeKind.String)
                {
                    Name = "Name",
                    IsNullable = false,
                    Annotations =
                        new Dictionary<string, AnnotationValues>
                        {
                            {
                                CollationAttribute.AnnotationName,
                                new AnnotationValues(
                                    new CollationAttribute("At a reasonable volume..."),
                                    new CollationAttribute("While I'm collating..."))
                            }
                        }
                });

            var operations = new[] { operation };

            var generator = new VisualBasicMigrationCodeGenerator();
            generator.AnnotationGenerators[CollationAttribute.AnnotationName] = () => new CollationCSharpCodeGenerator();
            var generatedMigration = generator.Generate("Migration", operations, "Source", "Target", "MyNamespace", "MyMigration");

            Assert.Equal(
                @"Imports System
Imports System.Collections.Generic
Imports System.Data.Entity.Infrastructure.Annotations
Imports System.Data.Entity.Migrations
Imports System.Data.Entity.TestHelpers
Imports Microsoft.VisualBasic

Namespace MyNamespace
    Public Partial Class MyMigration
        Inherits DbMigration
    
        Public Overrides Sub Up()
            AlterTableAnnotations(
                ""Customers"",
                Function(c) New With
                    {
                        .Id = c.Int(name := ""I.d"", identity := True,
                            annotations := New Dictionary(Of String, AnnotationValues)() From _
                            {
                                {
                                    ""A1"",
                                    New AnnotationValues(oldValue := Nothing, newValue := ""V1"")
                                 },
                                {
                                    ""A2"",
                                    New AnnotationValues(oldValue := Nothing, newValue := ""V2"")
                                 }
                            }),
                        .Name = c.String(nullable := False,
                            annotations := New Dictionary(Of String, AnnotationValues)() From _
                            {
                                {
                                    ""Collation"",
                                    New AnnotationValues(oldValue := new CollationAttribute(""At a reasonable volume...""), newValue := new CollationAttribute(""While I'm collating...""))
                                 }
                            })
                    },
                annotations := New Dictionary(Of String, AnnotationValues)() From _
                {
                    {
                        ""AT1"",
                        New AnnotationValues(oldValue := Nothing, newValue := ""VT1"")
                     },
                    {
                        ""AT2"",
                        New AnnotationValues(oldValue := Nothing, newValue := ""VT2"")
                     },
                    {
                        ""Collation"",
                        New AnnotationValues(oldValue := new CollationAttribute(""At a reasonable volume...""), newValue := new CollationAttribute(""While I'm collating...""))
                     }
                })
            
        End Sub
        
        Public Overrides Sub Down()
            AlterTableAnnotations(
                ""Customers"",
                Function(c) New With
                    {
                        .Id = c.Int(name := ""I.d"", identity := True,
                            annotations := New Dictionary(Of String, AnnotationValues)() From _
                            {
                                {
                                    ""A1"",
                                    New AnnotationValues(oldValue := Nothing, newValue := ""V1"")
                                 },
                                {
                                    ""A2"",
                                    New AnnotationValues(oldValue := Nothing, newValue := ""V2"")
                                 }
                            }),
                        .Name = c.String(nullable := False,
                            annotations := New Dictionary(Of String, AnnotationValues)() From _
                            {
                                {
                                    ""Collation"",
                                    New AnnotationValues(oldValue := new CollationAttribute(""At a reasonable volume...""), newValue := new CollationAttribute(""While I'm collating...""))
                                 }
                            })
                    },
                annotations := New Dictionary(Of String, AnnotationValues)() From _
                {
                    {
                        ""AT1"",
                        New AnnotationValues(oldValue := ""VT1"", newValue := Nothing)
                     },
                    {
                        ""AT2"",
                        New AnnotationValues(oldValue := ""VT2"", newValue := Nothing)
                     },
                    {
                        ""Collation"",
                        New AnnotationValues(oldValue := new CollationAttribute(""While I'm collating...""), newValue := new CollationAttribute(""At a reasonable volume...""))
                     }
                })
            
        End Sub
    End Class
End Namespace
",
                generatedMigration.UserCode);
        }
        protected override void Generate(AlterTableOperation alterTableOperation)
        {
            _tableCollation = alterTableOperation.Annotations.ContainsKey(CollationAttribute.AnnotationName)
                ? (CollationAttribute)alterTableOperation.Annotations[CollationAttribute.AnnotationName].NewValue
                : null;

            if (_tableCollation != null)
            {
                // Need to alter any column that doesn't have explictly set collation
                foreach (var column in alterTableOperation.Columns.Where(
                    c => c.ClrType == typeof(string)
                         && !c.Annotations.ContainsKey(CollationAttribute.AnnotationName)))
                {
                    Generate(new AlterColumnOperation(alterTableOperation.Name, column, false));
                }
            }

            _tableCollation = null;
        }