public void Generate_when_create_index_operation()
 {
     Assert.Equal(
         @"CreateIndex(""dbo.MyTable"", ""MyIdx"", new[] { ""Foo"", ""Bar"" }, isUnique: false, isClustered: false)",
         CSharpMigrationCodeGenerator.Generate(new CreateIndexOperation(
                                                   "dbo.MyTable", "MyIdx", new[] { "Foo", "Bar" }, isUnique: false, isClustered: false)));
 }
 public void Generate_when_add_default_constraint_operation_with_default_value()
 {
     Assert.Equal(
         @"AddDefaultConstraint(""dbo.MyTable"", ""Foo"", DefaultConstraint.Value(5))",
         CSharpMigrationCodeGenerator.Generate(new AddDefaultConstraintOperation(
                                                   "dbo.MyTable", "Foo", 5, null)));
 }
        public void Generate_when_create_table_operation_with_one_primary_key_columns()
        {
            Column foo;
            var    table = new Table("dbo.MyTable",
                                     new[]
            {
                foo = new Column("Foo", typeof(int))
                {
                    IsNullable = false, DefaultValue = 5
                },
                new Column("Bar", typeof(int))
            })
            {
                PrimaryKey = new PrimaryKey("MyPK", new[] { foo })
            };

            Assert.Equal(
                @"CreateTable(""dbo.MyTable"",
    c => new
        {
            Foo = c.Int(nullable: false, defaultValue: 5),
            Bar = c.Int()
        })
    .PrimaryKey(""MyPK"", t => t.Foo)",
                CSharpMigrationCodeGenerator.Generate(new CreateTableOperation(table)));
        }
 public void Generate_when_create_sequence_operation()
 {
     Assert.Equal(
         @"CreateSequence(""dbo.MySequence"", ""BIGINT"", 10, 5)",
         CSharpMigrationCodeGenerator.Generate(new CreateSequenceOperation(
                                                   new Sequence("dbo.MySequence", "BIGINT", 10, 5))));
 }
Exemple #5
0
        public void UniquifyName_should_return_unique_name_when_conflict()
        {
            var codeGenerator = new CSharpMigrationCodeGenerator();

            var @namespace = "Migrations";

            var generatedMigration1
                = codeGenerator.Generate(
                      "201108162311111_Migration",
                      new MigrationOperation[] { },
                      "Source",
                      "Target",
                      @namespace,
                      "Migration");

            var generatedMigration2
                = codeGenerator.Generate(
                      "201108162311111_Migration1",
                      new MigrationOperation[] { },
                      "Source",
                      "Target",
                      @namespace,
                      "Migration1");

            var migrationAssembly
                = new MigrationAssembly(
                      new MigrationCompiler("cs")
                      .Compile(
                          @namespace,
                          generatedMigration1,
                          generatedMigration2),
                      @namespace);

            Assert.Equal("Migration2", migrationAssembly.UniquifyName("Migration"));
        }
        public void Generate_when_create_table_operation()
        {
            var model        = new Model();
            var modelBuilder = new BasicModelBuilder(model);

            modelBuilder.Entity("E",
                                b =>
            {
                b.Property <byte>("P1").ForRelational().DefaultValue((byte)BikeType.Mountain);
                b.Property <int>("P2").ForRelational().DefaultValue(5);
                b.Property <int?>("P3");
                b.Property <int>("P4").StoreComputed().ForRelational().DefaultExpression("P2 + P3");
                b.ForRelational().Table("MyTable", "dbo");
            });

            var operation = OperationFactory().CreateTableOperation(model.GetEntityType("E"));

            Assert.Equal(
                @"CreateTable(""dbo.MyTable"",
    c => new
        {
            P1 = c.Byte(nullable: false, defaultValue: 1),
            P2 = c.Int(nullable: false, defaultValue: 5),
            P3 = c.Int(),
            P4 = c.Int(nullable: false, defaultSql: ""P2 + P3"", computed: true)
        })",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public override ScaffoldedMigration Generate(string migrationId, IEnumerable <MigrationOperation> operations, string sourceModel, string targetModel, string @namespace, string className)
        {
            foreach (MigrationOperation operation in operations)
            {
                if (operation is CreateTableOperation)
                {
                    foreach (var column in ((CreateTableOperation)operation).Columns)
                    {
                        if (column.ClrType == typeof(DateTime) && column.IsNullable.HasValue && !column.IsNullable.Value && string.IsNullOrEmpty(column.DefaultValueSql))
                        {
                            column.DefaultValueSql = "GETDATE()";
                        }
                    }
                }
                else if (operation is AddColumnOperation)
                {
                    ColumnModel column = ((AddColumnOperation)operation).Column;

                    if (column.ClrType == typeof(DateTime) && column.IsNullable.HasValue && !column.IsNullable.Value && string.IsNullOrEmpty(column.DefaultValueSql))
                    {
                        column.DefaultValueSql = "GETDATE()";
                    }
                }
            }

            CSharpMigrationCodeGenerator generator = new CSharpMigrationCodeGenerator();

            return(generator.Generate(migrationId, operations, sourceModel, targetModel, @namespace, className));
        }
 public void Generate_when_add_default_constraint_operation_with_default_sql()
 {
     Assert.Equal(
         @"AddDefaultConstraint(""dbo.MyTable"", ""Foo"", DefaultConstraint.Sql(""GETDATE()""))",
         CSharpMigrationCodeGenerator.Generate(new AddDefaultConstraintOperation(
                                                   "dbo.MyTable", "Foo", null, "GETDATE()")));
 }
        public void Generate_when_create_table_operation_with_multiple_primary_key_columns()
        {
            var model        = new Model();
            var modelBuilder = new BasicModelBuilder(model);

            modelBuilder.Entity("E",
                                b =>
            {
                b.Property <int>("Foo").ForRelational().DefaultValue(5);
                b.Property <int?>("Bar");
                b.Key("Foo", "Bar").ForRelational().Name("MyPK");
                b.ForRelational().Table("MyTable", "dbo");
            });

            var operation = OperationFactory().CreateTableOperation(model.GetEntityType("E"));

            Assert.Equal(
                @"CreateTable(""dbo.MyTable"",
    c => new
        {
            Foo = c.Int(nullable: false, defaultValue: 5),
            Bar = c.Int()
        })
    .PrimaryKey(""MyPK"", t => new { t.Foo, t.Bar })",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_migration_metadata_class()
        {
            var model      = new Metadata.Model();
            var entityType = new EntityType("Entity");

            entityType.SetKey(entityType.AddProperty("Id", typeof(int)));
            model.AddEntityType(entityType);

            var migration
                = new MigrationMetadata("Name", "Timestamp")
                {
                TargetModel = model
                };

            var codeGenerator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator());
            var stringBuilder = new IndentedStringBuilder();

            codeGenerator.GenerateMigrationMetadataClass("MyNamespace", "MyClass", migration, stringBuilder);

            Assert.Equal(
                @"using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Migrations.Infrastructure;
using System;

namespace MyNamespace
{
    public partial class MyClass : IMigrationMetadata
    {
        string IMigrationMetadata.Name
        {
            get
            {
                return ""Name"";
            }
        }
        
        string IMigrationMetadata.Timestamp
        {
            get
            {
                return ""Timestamp"";
            }
        }
        
        IModel IMigrationMetadata.TargetModel
        {
            get
            {
                var builder = new ModelBuilder();
                builder.Entity(""Entity"")
                    .Properties(ps => ps.Property<int>(""Id""))
                    .Key(""Id"");
                return builder.Model;
            }
        }
    }
}",
                stringBuilder.ToString());
        }
 public void Generate_when_add_foreign_key_operation()
 {
     Assert.Equal(
         @"AddForeignKey(""dbo.MyTable"", ""MyFK"", new[] { ""Foo"", ""Bar"" }, ""dbo.MyTable2"", new[] { ""Foo2"", ""Bar2"" }, cascadeDelete: false)",
         CSharpMigrationCodeGenerator.Generate(
             new AddForeignKeyOperation("dbo.MyTable", "MyFK", new[] { "Foo", "Bar" },
                                        "dbo.MyTable2", new[] { "Foo2", "Bar2" }, cascadeDelete: false)));
 }
        public void Generate_migration_metadata_class()
        {
            var model      = new Model();
            var entityType = new EntityType("Entity");

            entityType.GetOrSetPrimaryKey(entityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true));
            model.AddEntityType(entityType);

            var migration
                = new MigrationMetadata("000000000000001_Name", typeof(MyContext))
                {
                TargetModel = model
                };

            var codeGenerator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator());
            var stringBuilder = new IndentedStringBuilder();

            codeGenerator.GenerateMigrationMetadataClass("MyNamespace", "MyClass", migration, stringBuilder);

            Assert.Equal(
                @"using Microsoft.Data.Entity.Commands.Tests.Migrations;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Migrations.Infrastructure;
using System;

namespace MyNamespace
{
    [ContextType(typeof(CSharpMigrationCodeGeneratorTest.MyContext))]
    public partial class MyClass : IMigrationMetadata
    {
        string IMigrationMetadata.MigrationId
        {
            get
            {
                return ""000000000000001_Name"";
            }
        }
        
        IModel IMigrationMetadata.TargetModel
        {
            get
            {
                var builder = new BasicModelBuilder();
                
                builder.Entity(""Entity"", b =>
                    {
                        b.Property<int>(""Id"");
                        b.Key(""Id"");
                    });
                
                return builder.Model;
            }
        }
    }
}",
                stringBuilder.ToString());
        }
        public void Generate_when_move_sequence_operation()
        {
            var operation = new MoveSequenceOperation("dbo.MySequence", "RenamedSchema");

            Assert.Equal(
                @"MoveSequence(""dbo.MySequence"", ""RenamedSchema"")",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_add_primary_key_operation()
        {
            var operation = new AddPrimaryKeyOperation("dbo.MyTable", "MyPK", new[] { "Foo", "Bar" }, isClustered: false);

            Assert.Equal(
                @"AddPrimaryKey(""dbo.MyTable"", ""MyPK"", new[] { ""Foo"", ""Bar"" }, isClustered: false)",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_drop_default_constraint_operation()
        {
            var operation = new DropDefaultConstraintOperation("dbo.MyTable", "Foo");

            Assert.Equal(
                @"DropDefaultConstraint(""dbo.MyTable"", ""Foo"")",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_create_sequence_operation_with_all_defaults()
        {
            var operation = new CreateSequenceOperation("dbo.MySequence");

            Assert.Equal(
                @"CreateSequence(""dbo.MySequence"")",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_create_sequence_operation()
        {
            var operation = new CreateSequenceOperation("dbo.MySequence", 10, 5, 1, 100, typeof(int));

            Assert.Equal(
                @"CreateSequence(""dbo.MySequence"", 10, 5, 1, 100, typeof(int))",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_copy_data_operation()
        {
            var operation = new CopyDataOperation("dbo.T1", new[] { "A", "B" }, "dbo.T2", new[] { "C", "D" });

            Assert.Equal(
                @"CopyData(""dbo.T1"", new[] { ""A"", ""B"" }, ""dbo.T2"", new[] { ""C"", ""D"" })",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_create_sequence_operation_with_some_defaults()
        {
            var operation = new CreateSequenceOperation("dbo.MySequence", Sequence.DefaultStartValue, 7);

            Assert.Equal(
                @"CreateSequence(""dbo.MySequence"", 1, 7)",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_drop_foreign_key_operation()
        {
            var operation = new DropForeignKeyOperation("dbo.MyTable", "MyFK");

            Assert.Equal(
                @"DropForeignKey(""dbo.MyTable"", ""MyFK"")",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_rename_index_key_operation()
        {
            var operation = new RenameIndexOperation("dbo.MyTable", "MyIdx", "MyNewIdx");

            Assert.Equal(
                @"RenameIndex(""dbo.MyTable"", ""MyIdx"", ""MyNewIdx"")",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_add_unique_constraint_operation()
        {
            var operation = new AddUniqueConstraintOperation("dbo.MyTable", "MyUC", new[] { "Foo", "Bar" });

            Assert.Equal(
                @"AddUniqueConstraint(""dbo.MyTable"", ""MyUC"", new[] { ""Foo"", ""Bar"" })",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_drop_unique_constraint_operation()
        {
            var operation = new DropUniqueConstraintOperation("dbo.MyTable", "MyUC");

            Assert.Equal(
                @"DropUniqueConstraint(""dbo.MyTable"", ""MyUC"")",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_drop_sequence_operation()
        {
            var operation = new DropSequenceOperation("dbo.MySequence");

            Assert.Equal(
                @"DropSequence(""dbo.MySequence"")",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_add_default_constraint_operation_with_default_sql()
        {
            var operation = new AddDefaultConstraintOperation("dbo.MyTable", "Foo", null, "GETDATE()");

            Assert.Equal(
                @"AddDefaultExpression(""dbo.MyTable"", ""Foo"", ""GETDATE()"")",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_rename_column_operation()
        {
            var operation = new RenameColumnOperation("dbo.MyTable", "Foo", "Foo2");

            Assert.Equal(
                @"RenameColumn(""dbo.MyTable"", ""Foo"", ""Foo2"")",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_drop_database_operation()
        {
            var operation = new DropDatabaseOperation("MyDatabase");

            Assert.Equal(
                @"DropDatabase(""MyDatabase"")",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_move_table_operation()
        {
            var operation = new MoveTableOperation("dbo.MyTable", "dbo2");

            Assert.Equal(
                @"MoveTable(""dbo.MyTable"", ""dbo2"")",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_when_alter_sequence_operation()
        {
            var operation = new AlterSequenceOperation("dbo.MySequence", newIncrementBy: 13);

            Assert.Equal(
                @"AlterSequence(""dbo.MySequence"", 13)",
                CSharpMigrationCodeGenerator.Generate(operation));

            GenerateAndValidateCode(operation);
        }
        public void Generate_migration_class()
        {
            var upgradeOperations
                = new[]
                {
                new AddColumnOperation("dbo.MyTable", new Column("Foo", typeof(int))),
                new AddColumnOperation("dbo.MyTable", new Column("Bar", typeof(int)))
                };

            var downgradeOperations
                = new[]
                {
                new DropColumnOperation("dbo.MyTable", "Foo"),
                new DropColumnOperation("dbo.MyTable", "Bar")
                };

            var migration
                = new MigrationInfo("000000000000001_Name")
                {
                UpgradeOperations   = upgradeOperations,
                DowngradeOperations = downgradeOperations
                };

            var codeGenerator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator());
            var stringBuilder = new IndentedStringBuilder();

            codeGenerator.GenerateMigrationClass("MyNamespace", "MyClass", migration, stringBuilder);

            Assert.Equal(
                @"using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Builders;
using Microsoft.Data.Entity.Migrations.Model;
using System;

namespace MyNamespace
{
    public partial class MyClass : Migration
    {
        public override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn(""dbo.MyTable"", ""Foo"", c => c.Int());
            
            migrationBuilder.AddColumn(""dbo.MyTable"", ""Bar"", c => c.Int());
        }
        
        public override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(""dbo.MyTable"", ""Foo"");
            
            migrationBuilder.DropColumn(""dbo.MyTable"", ""Bar"");
        }
    }
}",
                stringBuilder.ToString());
        }
        public void Generate_string_literal()
        {
            var csharpGenerator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator());

            Assert.Equal("\"foo\\\"bar\"", csharpGenerator.GenerateLiteral("foo\"bar"));
        }
        public void Generate_migration_metadata_class()
        {
            var model = new Model();
            var entityType = new EntityType("Entity");

            entityType.SetKey(entityType.AddProperty("Id", typeof(int)));
            model.AddEntityType(entityType);

            var migration
                = new MigrationMetadata("000000000000001_Name")
                    {
                        TargetModel = model
                    };

            var codeGenerator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator());
            var stringBuilder = new IndentedStringBuilder();

            codeGenerator.GenerateMigrationMetadataClass("MyNamespace", "MyClass", migration, stringBuilder);

            Assert.Equal(
                @"using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Migrations.Infrastructure;
using System;

namespace MyNamespace
{
    public partial class MyClass : IMigrationMetadata
    {
        string IMigrationMetadata.MigrationId
        {
            get
            {
                return ""000000000000001_Name"";
            }
        }
        
        IModel IMigrationMetadata.TargetModel
        {
            get
            {
                var builder = new ModelBuilder();
                
                builder.Entity(""Entity"", b =>
                    {
                        b.Property<int>(""Id"");
                        b.Key(""Id"");
                    });
                
                return builder.Model;
            }
        }
    }
}",
                stringBuilder.ToString());
        }
        public void Escape_verbatim_string()
        {
            var csharpGenerator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator());

            Assert.Equal(
@"foo""""bar
    bar""""foo", 
                csharpGenerator.EscapeVerbatimString(
@"foo""bar
    bar""foo"));
        }
        public void Generate_verbatim_string_literal()
        {
            var csharpGenerator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator());

            Assert.Equal(
@"@""foo""""bar
    bar""""foo""", 
                csharpGenerator.GenerateVerbatimStringLiteral(
@"foo""bar
    bar""foo"));
        }
        public void Generate_migration_class()
        {
            var upgradeOperations
                = new[]
                    {
                        new AddColumnOperation("dbo.MyTable", new Column("Foo", typeof(int))),
                        new AddColumnOperation("dbo.MyTable", new Column("Bar", typeof(int)))
                    };

            var downgradeOperations
                = new[]
                    {
                        new DropColumnOperation("dbo.MyTable", "Foo"),
                        new DropColumnOperation("dbo.MyTable", "Bar")
                    };

            var migration
                = new MigrationMetadata("000000000000001_Name")
                    {
                        UpgradeOperations = upgradeOperations,
                        DowngradeOperations = downgradeOperations
                    };

            var codeGenerator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator());
            var stringBuilder = new IndentedStringBuilder();

            codeGenerator.GenerateMigrationClass("MyNamespace", "MyClass", migration, stringBuilder);

            Assert.Equal(
                @"using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Builders;
using System;

namespace MyNamespace
{
    public partial class MyClass : Migration
    {
        public override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn(""dbo.MyTable"", ""Foo"", c => c.Int());
            
            migrationBuilder.AddColumn(""dbo.MyTable"", ""Bar"", c => c.Int());
        }
        
        public override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(""dbo.MyTable"", ""Foo"");
            
            migrationBuilder.DropColumn(""dbo.MyTable"", ""Bar"");
        }
    }
}",
                stringBuilder.ToString());
        }
        public void Escape_string()
        {
            var csharpGenerator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator());

            Assert.Equal("foo\\\"bar", csharpGenerator.EscapeString("foo\"bar"));
        }