예제 #1
0
        public void Clone_replicates_instance_and_adds_column_clones_to_cache()
        {
            var column0 = new Column("C0", typeof(int));
            var column1 = new Column("C1", typeof(int));
            var referencedColumn0 = new Column("RC0", typeof(int));
            var referencedColumn1 = new Column("RC1", typeof(int));
            var foreignKey 
                = new ForeignKey(
                    "FK", 
                    new[] { column0, column1 },
                    new[] { referencedColumn0, referencedColumn1 }, 
                    cascadeDelete: true);

            var cloneContext = new CloneContext();
            var clone = foreignKey.Clone(cloneContext);

            Assert.NotSame(foreignKey, clone);
            Assert.Equal("FK", clone.Name);
            Assert.Equal(2, clone.Columns.Count);
            Assert.NotSame(column0, clone.Columns[0]);
            Assert.NotSame(column1, clone.Columns[1]);
            Assert.Equal("C0", clone.Columns[0].Name);
            Assert.Equal("C1", clone.Columns[1].Name);
            Assert.Equal(2, clone.ReferencedColumns.Count);
            Assert.NotSame(referencedColumn0, clone.ReferencedColumns[0]);
            Assert.NotSame(referencedColumn1, clone.ReferencedColumns[1]);
            Assert.Equal("RC0", clone.ReferencedColumns[0].Name);
            Assert.Equal("RC1", clone.ReferencedColumns[1].Name);
            Assert.True(clone.CascadeDelete);

            Assert.Same(clone.Columns[0], cloneContext.GetOrAdd(column0, () => null));
            Assert.Same(clone.Columns[1], cloneContext.GetOrAdd(column1, () => null));
            Assert.Same(clone.ReferencedColumns[0], cloneContext.GetOrAdd(referencedColumn0, () => null));
            Assert.Same(clone.ReferencedColumns[1], cloneContext.GetOrAdd(referencedColumn1, () => null));
        }
예제 #2
0
        public void Copy_replicates_source()
        {
            var column1 
                = new Column("Foo", typeof(string))
                    {
                        DataType = "T",
                        IsNullable = false,
                        DefaultValue = "V",
                        DefaultSql = "Sql",
                        ValueGenerationStrategy = ValueGenerationOnSave.WhenInsertingAndUpdating,
                        IsTimestamp = true,
                        MaxLength = 4,
                        Precision = 3,
                        Scale = 2,
                        IsFixedLength = true,
                        IsUnicode = true
                    };
            var column2 = new Column("Bar", typeof(int));

            column2.Copy(column1);

            Assert.Equal("Foo", column2.Name);
            Assert.Same(typeof(string), column2.ClrType);
            Assert.Equal("T", column2.DataType);
            Assert.Equal("V", column2.DefaultValue);
            Assert.Equal("Sql", column2.DefaultSql);
            Assert.Equal(ValueGenerationOnSave.WhenInsertingAndUpdating, column2.ValueGenerationStrategy);
            Assert.True(column2.IsTimestamp);
            Assert.Equal(4, column2.MaxLength.Value);
            Assert.Equal(3, column2.Precision.Value);
            Assert.Equal(2, column2.Scale.Value);
            Assert.True(column2.IsFixedLength.Value);
            Assert.True(column2.IsUnicode.Value);
        }
예제 #3
0
        public void Create_and_initialize_foreign_key()
        {
            var table = new Table("dbo.MyTable");
            var column0 = new Column("Foo", "int");
            var column1 = new Column("Bar", "int");

            table.AddColumn(column0);
            table.AddColumn(column1);

            var referencedTable = new Table("dbo.MyReferencedTable");
            var referencedColumn0 = new Column("ReferencedFoo", "int");
            var referencedColumn1 = new Column("ReferencedBar", "int");

            referencedTable.AddColumn(referencedColumn0);
            referencedTable.AddColumn(referencedColumn1);

            var foreignKey = new ForeignKey(
                "MyForeignKey",
                new[] { column0, column1 },
                new[] { referencedColumn0, referencedColumn1 },
                cascadeDelete: true);

            Assert.Equal("MyForeignKey", foreignKey.Name);
            Assert.IsAssignableFrom<IReadOnlyList<Column>>(foreignKey.Columns);
            Assert.Equal(2, foreignKey.Columns.Count);
            Assert.Same(column0, foreignKey.Columns[0]);
            Assert.Same(column1, foreignKey.Columns[1]);
            Assert.Same(table, foreignKey.Table);
            Assert.IsAssignableFrom<IReadOnlyList<Column>>(foreignKey.ReferencedColumns);
            Assert.Equal(2, foreignKey.ReferencedColumns.Count);
            Assert.Same(referencedColumn0, foreignKey.ReferencedColumns[0]);
            Assert.Same(referencedColumn1, foreignKey.ReferencedColumns[1]);
            Assert.Same(referencedTable, foreignKey.ReferencedTable);
            Assert.True(foreignKey.CascadeDelete);
        }
예제 #4
0
        public AddColumnOperation(SchemaQualifiedName tableName, [NotNull] Column column)
        {
            Check.NotNull(column, "column");

            _tableName = tableName;
            _column = column;
        }
        public void Create_and_initialize_operation()
        {
            var column = new Column("Foo", "int");
            var addColumnOperation = new AddColumnOperation("dbo.MyTable", column);

            Assert.Equal("dbo.MyTable", addColumnOperation.TableName);
            Assert.Same(column, addColumnOperation.Column);
            Assert.False(addColumnOperation.IsDestructiveChange);
        }
        public void Create_and_initialize_operation()
        {
            var newColumn = new Column("Foo", "int") { IsNullable = true };
            var alterColumnOperation = new AlterColumnOperation(
                "dbo.MyTable", newColumn, isDestructiveChange: true);

            Assert.Equal("dbo.MyTable", alterColumnOperation.TableName);
            Assert.Same(newColumn, alterColumnOperation.NewColumn);
            Assert.True(alterColumnOperation.IsDestructiveChange);
        }
예제 #7
0
        public void Can_set_name()
        {
            var column = new Column("Foo", typeof(int));

            Assert.Equal("Foo", column.Name);

            column.Name = "Bar";

            Assert.Equal("Bar", column.Name);
        }
        public void Dispatches_visitor()
        {
            var newColumn = new Column("Foo", "int") { IsNullable = true };
            var alterColumnOperation = new AlterColumnOperation(
                "dbo.MyTable", newColumn, isDestructiveChange: true);
            var mockVisitor = new Mock<MigrationOperationSqlGenerator>(new RelationalTypeMapper());
            var builder = new Mock<IndentedStringBuilder>();
            alterColumnOperation.GenerateSql(mockVisitor.Object, builder.Object);

            mockVisitor.Verify(g => g.Generate(alterColumnOperation, builder.Object), Times.Once());
        }
        public AlterColumnOperation(
            SchemaQualifiedName tableName,
            [NotNull] Column newColumn,
            bool isDestructiveChange)
        {
            Check.NotNull(newColumn, "newColumn");

            _tableName = tableName;
            _newColumn = newColumn;
            _isDestructiveChange = isDestructiveChange;
        }
예제 #10
0
        public void AddColumn_adds_specified_column()
        {
            var table = new Table("dbo.MyTable");

            Assert.Equal(0, table.Columns.Count);

            var column = new Column("Foo", "int");
            table.AddColumn(column);

            Assert.Equal(1, table.Columns.Count);
            Assert.Same(table, column.Table);
            Assert.Same(column, table.Columns[0]);
        }
예제 #11
0
        public void Create_and_initialize_table()
        {
            var column0 = new Column("Foo", "int");
            var column1 = new Column("Bar", "int");
            var table = new Table("dbo.MyTable", new[] { column0, column1 });

            Assert.Equal("dbo.MyTable", table.Name);
            Assert.IsAssignableFrom<IReadOnlyList<Column>>(table.Columns);
            Assert.Equal(2, table.Columns.Count);
            Assert.Same(column0, table.Columns[0]);
            Assert.Same(column1, table.Columns[1]);
            Assert.Same(table, column0.Table);
            Assert.Same(table, column1.Table);
        }
예제 #12
0
        public void Columns_gets_read_only_list_of_columns()
        {
            var table = new Table("dbo.MyTable");
            var column0 = new Column("Foo", "int");
            var column1 = new Column("Bar", "int");

            table.AddColumn(column0);
            table.AddColumn(column1);

            Assert.IsAssignableFrom<IReadOnlyList<Column>>(table.Columns);
            Assert.Equal(2, table.Columns.Count);
            Assert.Same(column0, table.Columns[0]);
            Assert.Same(column1, table.Columns[1]);
        }
예제 #13
0
        public void Create_and_initialize_index()
        {
            var column0 = new Column("Foo", "int");
            var column1 = new Column("Bar", "int");
            var table = new Table("dbo.MyTable", new[] { column0, column1 });
            var index = new Index("MyIndex", new[] { column1 }, isUnique: true, isClustered: true);

            Assert.Equal("MyIndex", index.Name);
            Assert.IsAssignableFrom<IReadOnlyList<Column>>(table.Columns);
            Assert.Equal(1, index.Columns.Count);
            Assert.Same(column1, index.Columns[0]);
            Assert.True(index.IsUnique);
            Assert.True(index.IsClustered);
        }
예제 #14
0
        public void Clone_gets_column_clones_from_cache()
        {
            var column0 = new Column("Foo", typeof(int));
            var column1 = new Column("Bar", typeof(int));
            var index = new Index("IX", new[] { column0, column1 }, isUnique: true, isClustered: true);

            var cloneContext = new CloneContext();
            var columnClone0 = column0.Clone(cloneContext);
            var columnClone1 = column1.Clone(cloneContext);
            var clone = index.Clone(cloneContext);

            Assert.NotSame(index, clone);
            Assert.Equal(2, clone.Columns.Count);
            Assert.Same(columnClone0, clone.Columns[0]);
            Assert.Same(columnClone1, clone.Columns[1]);
        }
예제 #15
0
        public void Clone_gets_column_clones_from_cache()
        {
            var column0 = new Column("Foo", typeof(int));
            var column1 = new Column("Bar", typeof(int));
            var primaryKey = new PrimaryKey("PK", new[] { column0, column1 }, isClustered: false);
            
            var cloneContext = new CloneContext();
            var columnClone0 = column0.Clone(cloneContext);
            var columnClone1 = column1.Clone(cloneContext);
            var clone = primaryKey.Clone(cloneContext);

            Assert.NotSame(primaryKey, clone);
            Assert.Equal(2, clone.Columns.Count);
            Assert.Same(columnClone0, clone.Columns[0]);
            Assert.Same(columnClone1, clone.Columns[1]);
        }
예제 #16
0
        public void Create_and_initialize_primary_key()
        {
            var table = new Table("dbo.MyTable");
            var column0 = new Column("Foo", "int");
            var column1 = new Column("Bar", "int");
            table.AddColumn(column0);
            table.AddColumn(column1);
            var primaryKey = new PrimaryKey(
                "MyPrimaryKey", new[] { column0, column1, }, isClustered: true);

            Assert.Equal("MyPrimaryKey", primaryKey.Name);
            Assert.IsAssignableFrom<IReadOnlyList<Column>>(table.Columns);
            Assert.Equal(2, primaryKey.Columns.Count);
            Assert.Same(column0, primaryKey.Columns[0]);
            Assert.Same(column1, primaryKey.Columns[1]);
            Assert.Same(table, primaryKey.Table);
            Assert.True(primaryKey.IsClustered);
        }
예제 #17
0
        public void Clone_replicates_instance_and_adds_column_clones_to_cache()
        {
            var column0 = new Column("Foo", typeof(int));
            var column1 = new Column("Bar", typeof(int));
            var primaryKey = new PrimaryKey("PK", new[] { column0, column1 }, isClustered: false);

            var cloneContext = new CloneContext();
            var clone = primaryKey.Clone(cloneContext);

            Assert.NotSame(primaryKey, clone);
            Assert.Equal("PK", clone.Name);
            Assert.Equal(2, clone.Columns.Count);
            Assert.NotSame(column0, clone.Columns[0]);
            Assert.NotSame(column1, clone.Columns[1]);
            Assert.Equal("Foo", clone.Columns[0].Name);
            Assert.Equal("Bar", clone.Columns[1].Name);
            Assert.False(clone.IsClustered);

            Assert.Same(clone.Columns[0], cloneContext.GetOrAdd(column0, () => null));
            Assert.Same(clone.Columns[1], cloneContext.GetOrAdd(column1, () => null));
        }
        public void FilterOperations_removes_add_foreign_key_when_corresponding_create_table()
        {
            var friendId = new Column("FriendId", typeof(long));
            var id = new Column("Id", typeof(long));
            var pony = new Table("Pony", new[] { id, friendId });
            pony.AddForeignKey(new ForeignKey("BFFK", new[] { friendId }, new[] { id }));
            var createTable = new CreateTableOperation(pony);
            var addForeignKey = new AddForeignKeyOperation(
                "Pony",
                "BFFK",
                new[] { "FriendId" },
                "Pony",
                new[] { "Id" },
                false);
            var operations = new MigrationOperation[] { createTable, addForeignKey };
            var generator = CreateGenerator();

            var result = generator.FilterOperations(operations);

            Assert.Equal(new[] { createTable }, result);
        }
        public void Generate_when_create_table_operation()
        {
            Column foo, bar;
            var table = new Table("dbo.MyTable",
                new[]
                    {
                        foo = new Column("Foo", "int") { IsNullable = false, DefaultValue = 5 },
                        bar = new Column("Bar", "int") { IsNullable = true }
                    })
                {
                    PrimaryKey = new PrimaryKey("MyPK", new[] { foo, bar }, isClustered: false)
                };

            Assert.Equal(
                @"CREATE TABLE [dbo].[MyTable] (
    [Foo] int NOT NULL DEFAULT 5,
    [Bar] int,
    CONSTRAINT [MyPK] PRIMARY KEY NONCLUSTERED ([Foo], [Bar])
)",
                Generate(
                    new CreateTableOperation(table)).Sql);
        }
예제 #20
0
        public void Create_and_initialize_column()
        {
            var column = new Column("Foo", "int")
                { IsNullable = true, DefaultValue = 5 };

            Assert.Equal("Foo", column.Name);
            Assert.Null(column.ClrType);
            Assert.Equal("int", column.DataType);
            Assert.True(column.IsNullable);
            Assert.Equal(5, column.DefaultValue);
            Assert.Null(column.DefaultSql);

            column = new Column("Bar", typeof(int), null)
                { IsNullable = false, DefaultSql = "GETDATE()" };

            Assert.Equal("Bar", column.Name);
            Assert.Same(typeof(int), column.ClrType);
            Assert.Null(column.DataType);
            Assert.False(column.IsNullable);
            Assert.Null(column.DefaultValue);
            Assert.Equal("GETDATE()", column.DefaultSql);
        }
예제 #21
0
        public void Clone_replicates_instance_and_adds_column_clones_to_cache()
        {
            var column0 = new Column("Foo", typeof(int));
            var column1 = new Column("Bar", typeof(int));
            var index = new Index("IX", new[] { column0, column1 }, isUnique: true, isClustered: true);

            var cloneContext = new CloneContext();
            var clone = index.Clone(cloneContext);

            Assert.NotSame(index, clone);
            Assert.Equal("IX", clone.Name);
            Assert.Equal(2, clone.Columns.Count);
            Assert.NotSame(column0, clone.Columns[0]);
            Assert.NotSame(column1, clone.Columns[1]);
            Assert.Equal("Foo", clone.Columns[0].Name);
            Assert.Equal("Bar", clone.Columns[1].Name);
            Assert.True(clone.IsUnique);
            Assert.True(clone.IsClustered);

            Assert.Same(clone.Columns[0], cloneContext.GetOrAdd(column0, () => null));
            Assert.Same(clone.Columns[1], cloneContext.GetOrAdd(column1, () => null));
        }
        public void Generate_when_create_table_operation_with_Identity_key()
        {
            Column foo, bar;
            var table = new Table(
                "dbo.MyTable",
                new[]
                    {
                        foo = new Column("Foo", "int") { IsNullable = false, ValueGenerationStrategy = ValueGenerationOnSave.WhenInserting },
                        bar = new Column("Bar", "int") { IsNullable = true }
                    })
                {
                    PrimaryKey = new PrimaryKey("MyPK", new[] { foo }, isClustered: false)
                };

            Assert.Equal(
                @"CREATE TABLE [dbo].[MyTable] (
    [Foo] int NOT NULL IDENTITY,
    [Bar] int,
    CONSTRAINT [MyPK] PRIMARY KEY NONCLUSTERED ([Foo])
)",
                Generate(
                    new CreateTableOperation(table)).Sql);
        }
        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_table_operation()
        {
            Column foo, bar;
            var table = new Table(
                "dbo.MyTable",
                new[]
                    {
                        foo = new Column("Foo", "int") { IsNullable = false, DefaultValue = 5 },
                        bar = new Column("Bar", "int") { IsNullable = true }
                    })
                {
                    PrimaryKey = new PrimaryKey("MyPK", new[] { foo, bar }, isClustered: false)
                };

            Assert.Equal(
                @"CREATE TABLE ""dbo"".""MyTable"" (
    ""Foo"" int NOT NULL DEFAULT 5,
    ""Bar"" int,
    CONSTRAINT ""MyPK"" PRIMARY KEY (""Foo"", ""Bar"")
)",
                Generate(
                    new CreateTableOperation(table)).Sql);
        }
예제 #25
0
        private static Column BuildColumn(Table table, IProperty property)
        {
            var column =
                new Column(property.ColumnName(), property.PropertyType, property.ColumnType())
                    {
                        IsNullable = property.IsNullable,
                        DefaultValue = property.ColumnDefaultValue(),
                        DefaultSql = property.ColumnDefaultSql(),
                        ValueGenerationStrategy = property.ValueGenerationOnSave,
                        IsTimestamp = property.PropertyType == typeof(byte[]) && property.IsConcurrencyToken
                    };

            table.AddColumn(column);

            return column;
        }
예제 #26
0
        public void Clone_replicates_instance()
        {
            var cloneContext = new CloneContext();
            var column
                = new Column("Foo", typeof(string))
                {
                    DataType = "T",
                    IsNullable = false,
                    DefaultValue = "V",
                    DefaultSql = "Sql",
                    ValueGenerationStrategy = ValueGenerationOnSave.WhenInsertingAndUpdating,
                    IsTimestamp = true,
                    MaxLength = 4,
                    Precision = 3,
                    Scale = 2,
                    IsFixedLength = true,
                    IsUnicode = true
                };
            var clone = column.Clone(cloneContext);

            Assert.NotSame(column, clone);
            Assert.Equal("Foo", clone.Name);
            Assert.Same(typeof(string), clone.ClrType);
            Assert.Equal("T", clone.DataType);
            Assert.Equal("V", clone.DefaultValue);
            Assert.Equal("Sql", clone.DefaultSql);
            Assert.Equal(ValueGenerationOnSave.WhenInsertingAndUpdating, clone.ValueGenerationStrategy);
            Assert.True(clone.IsTimestamp);
            Assert.Equal(4, clone.MaxLength.Value);
            Assert.Equal(3, clone.Precision.Value);
            Assert.Equal(2, clone.Scale.Value);
            Assert.True(clone.IsFixedLength.Value);
            Assert.True(clone.IsUnicode.Value);
        }
예제 #27
0
        public void Clone_adds_new_clone_to_cache()
        {
            var cloneContext = new CloneContext();
            var column = new Column("Foo", typeof(string));
            var clone = column.Clone(cloneContext);

            Assert.NotSame(column, clone);
            Assert.Same(clone, cloneContext.GetOrAdd(column, () => null));
        }
예제 #28
0
        public void Clone_gets_existing_clone_from_cache()
        {
            var cloneContext = new CloneContext();
            var column = new Column("Foo", typeof(string));
            var clone = new Column("Foo", typeof(string));

            Assert.Same(clone, cloneContext.GetOrAdd(column, () => clone));
            Assert.Same(clone, column.Clone(cloneContext));
        }
        public void Generate_with_create_table_generates_fks()
        {
            var pegasusId = new Column("Id", typeof(long));
            new Table("Pegasus", new[] { pegasusId });
            var friend1Id = new Column("Friend1Id", typeof(long));
            var friend2Id = new Column("Friend2Id", typeof(long));
            var friendship = new Table("Friendship", new[] { friend1Id, friend2Id })
                {
                    PrimaryKey = new PrimaryKey("PegasusPK", new[] { friend1Id, friend2Id })
                };
            friendship.AddForeignKey(new ForeignKey("FriendshipFK1", new[] { friend1Id }, new[] { pegasusId }));
            friendship.AddForeignKey(new ForeignKey("FriendshipFK2", new[] { friend2Id }, new[] { pegasusId }));
            var operation = new CreateTableOperation(friendship);

            var sql = Generate(operation);

            Assert.Equal(
                @"CREATE TABLE ""Friendship"" (
    ""Friend1Id"" INTEGER,
    ""Friend2Id"" INTEGER,
    CONSTRAINT ""PegasusPK"" PRIMARY KEY (""Friend1Id"", ""Friend2Id""),
    CONSTRAINT ""FriendshipFK1"" FOREIGN KEY (""Friend1Id"") REFERENCES ""Pegasus"" (""Id""),
    CONSTRAINT ""FriendshipFK2"" FOREIGN KEY (""Friend2Id"") REFERENCES ""Pegasus"" (""Id"")
)",
                sql);
        }
        public void Generate_when_alter_column_operation()
        {
            var newColumn = new Column("Foo", typeof(int)) { IsNullable = false, DefaultValue = 5 };

            Assert.Equal(
                @"AlterColumn(""dbo.MyTable"", ""Foo"", c => c.Int(nullable: false, defaultValue: 5))",
                CSharpMigrationCodeGenerator.Generate(new AlterColumnOperation("dbo.MyTable", newColumn, isDestructiveChange: true)));
        }