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());
        }
        public void FilterOperations_preserves_add_foreign_key_when_create_table_but_no_fk()
        {
            var createTable = new CreateTableOperation(
                new Table(
                    "Pony",
                    new[] { new Column("Id", typeof(long)), new Column("FriendId", typeof(long)) }));
            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 MigrationOperation[] { createTable, addForeignKey }, result);
        }
        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);
        }
        protected override void GenerateTableConstraints(
            CreateTableOperation createTableOperation,
            IndentedStringBuilder stringBuilder)
        {
            Check.NotNull(createTableOperation, "createTableOperation");
            Check.NotNull(stringBuilder, "stringBuilder");

            foreach (var foreignKey in createTableOperation.Table.ForeignKeys)
            {
                stringBuilder.AppendLine(",");
                GenerateForeignKey(
                    new AddForeignKeyOperation(
                        foreignKey.Table.Name,
                        foreignKey.Name,
                        foreignKey.Columns.Select(c => c.Name).ToArray(),
                        foreignKey.ReferencedTable.Name,
                        foreignKey.ReferencedColumns.Select(c => c.Name).ToArray(),
                        foreignKey.CascadeDelete),
                    stringBuilder);
            }
        }
        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);
        }