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_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);
        }
示例#3
0
        public void Clone_replicates_instance_and_uses_cache()
        {
            var column00 = new Column("C00", typeof(int));
            var column01 = new Column("C01", typeof(string));
            var column10 = new Column("C10", typeof(int));
            var column11 = new Column("C11", typeof(string));
            var table0 = new Table("dbo.T0", new[] { column00, column01 });
            var table1 = new Table("dbo.T1", new[] { column10, column11 });
            var primaryKey0 = new PrimaryKey("PK0", new[] { column00 });
            var primaryKey1 = new PrimaryKey("PK1", new[] { column10 });
            var foreignKey0 = new ForeignKey("FK0", new[] { column01 }, new[] { column10 });
            var foreignKey1 = new ForeignKey("FK1", new[] { column11 }, new[] { column00 });
            var index0 = new Index("IX0", new[] { column00 });
            var index1 = new Index("IX1", new[] { column10 });

            table0.PrimaryKey = primaryKey0;
            table1.PrimaryKey = primaryKey1;
            table0.AddForeignKey(foreignKey0);
            table1.AddForeignKey(foreignKey1);
            table0.AddIndex(index0);
            table1.AddIndex(index1);

            var cloneContext = new CloneContext();
            var clone0 = table0.Clone(cloneContext);

            Assert.NotSame(table0, clone0);
            Assert.Equal("dbo.T0", clone0.Name);

            Assert.Equal(2, clone0.Columns.Count);
            Assert.NotSame(column00, clone0.Columns[0]);
            Assert.NotSame(column01, clone0.Columns[1]);
            Assert.Equal("C00", clone0.Columns[0].Name);
            Assert.Equal("C01", clone0.Columns[1].Name);
            Assert.Equal(typeof(int), clone0.Columns[0].ClrType);
            Assert.Equal(typeof(string), clone0.Columns[1].ClrType);

            Assert.NotNull(clone0.PrimaryKey);
            Assert.NotSame(primaryKey0, clone0.PrimaryKey);
            Assert.Equal("PK0", clone0.PrimaryKey.Name);
            Assert.Equal(1, clone0.PrimaryKey.Columns.Count);
            Assert.Same(clone0.Columns[0], clone0.PrimaryKey.Columns[0]);

            Assert.Equal(1, clone0.ForeignKeys.Count);
            Assert.NotSame(foreignKey0, clone0.ForeignKeys[0]);
            Assert.Equal("FK0", clone0.ForeignKeys[0].Name);
            Assert.Equal(1, clone0.ForeignKeys[0].Columns.Count);
            Assert.Equal(1, clone0.ForeignKeys[0].ReferencedColumns.Count);
            Assert.Same(clone0.Columns[1], clone0.ForeignKeys[0].Columns[0]);
            Assert.Same(cloneContext.GetOrAdd(column10, () => null), clone0.ForeignKeys[0].ReferencedColumns[0]);

            Assert.Equal(1, clone0.Indexes.Count);
            Assert.NotSame(index0, clone0.Indexes[0]);
            Assert.Equal("IX0", clone0.Indexes[0].Name);
            Assert.Equal(1, clone0.Indexes[0].Columns.Count);
            Assert.Same(clone0.Columns[0], clone0.Indexes[0].Columns[0]);

            Assert.Equal(3, cloneContext.ItemCount);
            Assert.Same(clone0.Columns[0], cloneContext.GetOrAdd(column00, () => null));
            Assert.Same(clone0.Columns[1], cloneContext.GetOrAdd(column01, () => null));
            Assert.NotNull(cloneContext.GetOrAdd(column10, () => null));

            var clone1 = table1.Clone(cloneContext);

            Assert.NotSame(table1, clone1);
            Assert.Equal("dbo.T1", clone1.Name);

            Assert.Equal(2, clone1.Columns.Count);
            Assert.NotSame(column10, clone1.Columns[0]);
            Assert.NotSame(column11, clone1.Columns[1]);
            Assert.Equal("C10", clone1.Columns[0].Name);
            Assert.Equal("C11", clone1.Columns[1].Name);
            Assert.Equal(typeof(int), clone1.Columns[0].ClrType);
            Assert.Equal(typeof(string), clone1.Columns[1].ClrType);

            Assert.NotNull(clone1.PrimaryKey);
            Assert.NotSame(primaryKey1, clone1.PrimaryKey);
            Assert.Equal("PK1", clone1.PrimaryKey.Name);
            Assert.Equal(1, clone1.PrimaryKey.Columns.Count);
            Assert.Same(clone1.Columns[0], clone1.PrimaryKey.Columns[0]);

            Assert.Equal(1, clone1.ForeignKeys.Count);
            Assert.NotSame(foreignKey1, clone1.ForeignKeys[0]);
            Assert.Equal("FK1", clone1.ForeignKeys[0].Name);
            Assert.Equal(1, clone1.ForeignKeys[0].Columns.Count);
            Assert.Equal(1, clone1.ForeignKeys[0].ReferencedColumns.Count);
            Assert.Same(clone1.Columns[1], clone1.ForeignKeys[0].Columns[0]);
            Assert.Same(clone0.Columns[0], clone1.ForeignKeys[0].ReferencedColumns[0]);
            Assert.Same(clone1.Columns[0], clone0.ForeignKeys[0].ReferencedColumns[0]);

            Assert.Equal(1, clone1.Indexes.Count);
            Assert.NotSame(index1, clone1.Indexes[0]);
            Assert.Equal("IX1", clone1.Indexes[0].Name);
            Assert.Equal(1, clone1.Indexes[0].Columns.Count);
            Assert.Same(clone1.Columns[0], clone1.Indexes[0].Columns[0]);

            Assert.Equal(4, cloneContext.ItemCount);
            Assert.Same(clone0.Columns[0], cloneContext.GetOrAdd(column00, () => null));
            Assert.Same(clone0.Columns[1], cloneContext.GetOrAdd(column01, () => null));
            Assert.Same(clone1.Columns[0], cloneContext.GetOrAdd(column10, () => null));
            Assert.Same(clone1.Columns[1], cloneContext.GetOrAdd(column11, () => null));
        }
示例#4
0
        public void GetForeignKey_finds_foreign_key_by_name()
        {
            var column00 = new Column("C00", "int");
            var column01 = new Column("C01", "int");
            var table0 = new Table("dbo.T0", new[] { column00, column01 });
            var column10 = new Column("C10", "int");
            var column11 = new Column("C11", "int");
            var table1 = new Table("dbo.T0", new[] { column10, column11 });
            var foreignKey0 = new ForeignKey("FK0", new[] { column00 }, new[] { column10 });
            var foreignKey1 = new ForeignKey("FK1", new[] { column01 }, new[] { column11 });

            table0.AddForeignKey(foreignKey0);
            table0.AddForeignKey(foreignKey1);

            Assert.Same(foreignKey0, table0.GetForeignKey("FK0"));
            Assert.Same(foreignKey1, table0.GetForeignKey("FK1"));
        }
示例#5
0
        public void RemoveForeignKey_removes_specified_foreign_key()
        {
            var column00 = new Column("C00", "int");
            var column01 = new Column("C01", "int");
            var table0 = new Table("dbo.T0", new[] { column00, column01 });
            var column10 = new Column("C10", "int");
            var column11 = new Column("C11", "int");
            var table1 = new Table("dbo.T0", new[] { column10, column11 });
            var foreignKey0 = new ForeignKey("FK0", new[] { column00 }, new[] { column10 });
            var foreignKey1 = new ForeignKey("FK1", new[] { column01 }, new[] { column11 });

            table0.AddForeignKey(foreignKey0);
            table0.AddForeignKey(foreignKey1);

            Assert.Equal(2, table0.ForeignKeys.Count);

            table0.RemoveForeignKey("FK1");

            Assert.Equal(1, table0.ForeignKeys.Count);
            Assert.Same(foreignKey0, table0.ForeignKeys[0]);
        }
示例#6
0
        public void ForeignKeys_gets_read_only_list_of_foreign_keys()
        {
            var column00 = new Column("C00", "int");
            var column01 = new Column("C01", "int");
            var table0 = new Table("dbo.T0", new[] { column00, column01 });
            var column10 = new Column("C10", "int");
            var column11 = new Column("C11", "int");
            var table1 = new Table("dbo.T0", new[] { column10, column11 });
            var foreignKey0 = new ForeignKey("FK0", new[] { column00 }, new[] { column10 });
            var foreignKey1 = new ForeignKey("FK1", new[] { column01 }, new[] { column11 });

            table0.AddForeignKey(foreignKey0);
            table0.AddForeignKey(foreignKey1);

            Assert.IsAssignableFrom<IReadOnlyList<ForeignKey>>(table0.ForeignKeys);
            Assert.Equal(2, table0.ForeignKeys.Count);
            Assert.Same(foreignKey0, table0.ForeignKeys[0]);
            Assert.Same(foreignKey1, table0.ForeignKeys[1]);
        }