예제 #1
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);
        }
        public void Create_and_initialize_operation()
        {
            var table = new Table("dbo.MyTable", new[] { new Column("Id", "int") });
            var createTableOperation = new CreateTableOperation(table);

            Assert.Same(table, createTableOperation.Table);
            Assert.False(createTableOperation.IsDestructiveChange);
        }
        public void Dispatches_visitor()
        {
            var table = new Table("dbo.MyTable", new[] { new Column("Id", "int") });
            var createTableOperation = new CreateTableOperation(table);
            var mockVisitor = new Mock<MigrationOperationSqlGenerator>(new RelationalTypeMapper());
            var builder = new Mock<IndentedStringBuilder>();
            createTableOperation.GenerateSql(mockVisitor.Object, builder.Object);

            mockVisitor.Verify(g => g.Generate(createTableOperation, builder.Object), Times.Once());
        }
예제 #4
0
        public void Can_set_name()
        {
            var table = new Table("dbo.Table");

            Assert.Equal("dbo.Table", table.Name);

            table.Name = "dbo.RenamedTable";

            Assert.Equal("dbo.RenamedTable", table.Name);
        }
예제 #5
0
        public void Can_set_schema()
        {
            var table = new Table("dbo.Table");

            Assert.Equal("dbo.Table", table.Name);

            table.Name = "renamedSchema.Table";

            Assert.Equal("renamedSchema.Table", table.Name);
        }
예제 #6
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]);
        }
예제 #7
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]);
        }
예제 #8
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);
        }
예제 #9
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);
        }
예제 #10
0
        public void Tables_gets_read_only_list_of_tables()
        {
            var database = new DatabaseModel();
            var table0 = new Table("dbo.MyTable0");
            var table1 = new Table("dbo.MyTable1");

            database.AddTable(table0);
            database.AddTable(table1);

            Assert.IsAssignableFrom<IReadOnlyList<Table>>(database.Tables);
            Assert.Equal(2, database.Tables.Count);
            Assert.Same(table0, database.Tables[0]);
            Assert.Same(table1, database.Tables[1]);
        }
예제 #11
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);
        }
        public void Generate_when_create_table_operation_without_primary_key()
        {
            var table = new Table("dbo.MyTable",
                new[]
                    {
                        new Column("Foo", typeof(int)) { IsNullable = false, DefaultValue = 5 },
                        new Column("Bar", typeof(int))
                    });

            Assert.Equal(
                @"CreateTable(""dbo.MyTable"",
    c => new
        {
            Foo = c.Int(nullable: false, defaultValue: 5),
            Bar = c.Int()
        })",
                CSharpMigrationCodeGenerator.Generate(new CreateTableOperation(table)));
        }
        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);
        }
        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()
        {
            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);
        }
        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)));
        }
예제 #18
0
        public void GetTable_finds_table_by_name()
        {
            var database = new DatabaseModel();
            var table0 = new Table("dbo.MyTable0");
            var table1 = new Table("dbo.MyTable1");

            database.AddTable(table0);
            database.AddTable(table1);

            Assert.Same(table0, database.GetTable("dbo.MyTable0"));
            Assert.Same(table1, database.GetTable("dbo.MyTable1"));
        }
예제 #19
0
        public void Clone_replicates_tables()
        {            
            var databaseModel = new DatabaseModel();
            var sequence0 = new Sequence("dbo.S0");
            var sequence1 = new Sequence("dbo.S1");
            var table0 = new Table("dbo.T0");
            var table1 = new Table("dbo.T1");

            databaseModel.AddSequence(sequence0);
            databaseModel.AddSequence(sequence1);
            databaseModel.AddTable(table0);
            databaseModel.AddTable(table1);

            var clone = databaseModel.Clone();

            Assert.NotSame(databaseModel, clone);
            Assert.Equal(2, clone.Sequences.Count);
            Assert.NotSame(sequence0, clone.Sequences[0]);
            Assert.NotSame(sequence1, clone.Sequences[1]);
            Assert.Equal("dbo.S0", clone.Sequences[0].Name);
            Assert.Equal("dbo.S1", clone.Sequences[1].Name);
            Assert.Equal(2, clone.Tables.Count);
            Assert.NotSame(table0, clone.Tables[0]);
            Assert.NotSame(table1, clone.Tables[1]);
            Assert.Equal("dbo.T0", clone.Tables[0].Name);
            Assert.Equal("dbo.T1", clone.Tables[1].Name);
        }
예제 #20
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;
        }
예제 #21
0
        public void RemoveTable_removes_specified_table()
        {
            var database = new DatabaseModel();
            var table0 = new Table("dbo.MyTable0");
            var table1 = new Table("dbo.MyTable1");

            database.AddTable(table0);
            database.AddTable(table1);

            Assert.Equal(2, database.Tables.Count);

            database.RemoveTable("dbo.MyTable1");

            Assert.Equal(1, database.Tables.Count);
            Assert.Same(table0, database.Tables[0]);
        }
예제 #22
0
        public CreateTableOperation([NotNull] Table table)
        {
            Check.NotNull(table, "table");

            _table = table;
        }
예제 #23
0
        private static Table BuildTable(DatabaseModel database, IEntityType entityType)
        {
            var table = new Table(GetSchemaQualifiedName(entityType));

            database.AddTable(table);

            return table;
        }
예제 #24
0
        protected internal virtual Table Clone(CloneContext cloneContext)
        {
            var clone = new Table(Name, Columns.Select(c => c.Clone(cloneContext)));

            if (PrimaryKey != null)
            {
                clone._primaryKey = PrimaryKey.Clone(cloneContext);
            }

            clone._foreignKeys.AddRange(ForeignKeys.Select(fk => fk.Clone(cloneContext)));
            clone._indexes.AddRange(Indexes.Select(ix => ix.Clone(cloneContext)));

            return clone;
        }
        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 GenerateDataType_for_byte_array_concurrency_token()
        {
            var column = new Column("Username", typeof(byte[])) { IsTimestamp = true };
            var table = new Table("dbo.Users");
            table.AddColumn(column);

            Assert.Equal("rowversion", GenerateDataType(column));
        }
        public void GenerateDataType_for_byte_array_key()
        {
            var column = new Column("Username", typeof(byte[]));
            var table = new Table("dbo.Users") { PrimaryKey = new PrimaryKey("PK_Users", new[] { column }) };
            table.AddColumn(column);

            Assert.Equal("varbinary(128)", GenerateDataType(column));
        }
        public void GenerateDataType_for_string_key()
        {
            var column = new Column("Username", typeof(string));
            var table = new Table("dbo.Users");
            table.PrimaryKey = new PrimaryKey("PK_Users", new List<Column>() { column }.AsReadOnly());
            table.AddColumn(column);

            Assert.Equal("nvarchar(128)", GenerateDataType(column));
        }
예제 #29
0
        public void AddTable_adds_specified_table()
        {
            var database = new DatabaseModel();

            Assert.Equal(0, database.Tables.Count);

            var table = new Table("dbo.MyTable");
            database.AddTable(table);

            Assert.Equal(1, database.Tables.Count);
            Assert.Same(table, database.Tables[0]);
        }
 private static Column CreateColumn(Type clrType)
 {
     var column = new Column("Username", clrType);
     var table = new Table("dbo.Users");
     table.AddColumn(column);
     return column;
 }