예제 #1
0
        public void TestScript()
        {
            var db = new Database("TEST_TEMP");
            var t1 = new Table("dbo", "t1");
            t1.Columns.Add(new Column("col1", "int", false, null));
            t1.Columns.Add(new Column("col2", "int", false, null));
            t1.Constraints.Add(new Constraint("pk_t1", "PRIMARY KEY", "col1,col2"));
            t1.FindConstraint("pk_t1").Clustered = true;

            var t2 = new Table("dbo", "t2");
            t2.Columns.Add(new Column("col1", "int", false, null));
            t2.Columns.Add(new Column("col2", "int", false, null));
            t2.Columns.Add(new Column("col3", "int", false, null));
            t2.Constraints.Add(new Constraint("pk_t2", "PRIMARY KEY", "col1"));
            t2.FindConstraint("pk_t2").Clustered = true;
            t2.Constraints.Add(new Constraint("IX_col3", "UNIQUE", "col3"));

            db.ForeignKeys.Add(new ForeignKey(t2, "fk_t2_t1", "col2,col3", t1, "col1,col2"));

            db.Tables.Add(t1);
            db.Tables.Add(t2);

            TestHelper.DropDb("TEST_TEMP");
            SqlConnection.ClearAllPools();
            TestHelper.ExecBatchSql(db.ScriptCreate(), "master");

            var db2 = new Database();
            db2.Connection = TestHelper.GetConnString("TEST_TEMP");
            db2.Load();

            TestHelper.DropDb("TEST_TEMP");

            foreach (Table t in db.Tables) {
                Assert.IsNotNull(db2.FindTable(t.Name, t.Owner));
                Assert.IsFalse(db2.FindTable(t.Name, t.Owner).Compare(t).IsDiff);
            }
        }
예제 #2
0
파일: Table.cs 프로젝트: aeagle/schemazen
        public TableDiff Compare(Table t)
        {
            var diff = new TableDiff();
            diff.Owner = t.Owner;
            diff.Name = t.Name;

            //get additions and compare mutual columns
            foreach (Column c in Columns.Items) {
                Column c2 = t.Columns.Find(c.Name);
                if (c2 == null) {
                    diff.ColumnsAdded.Add(c);
                } else {
                    //compare mutual columns
                    ColumnDiff cDiff = c.Compare(c2);
                    if (cDiff.IsDiff) {
                        diff.ColumnsDiff.Add(cDiff);
                    }
                }
            }

            //get deletions
            foreach (Column c in t.Columns.Items.Where(c => Columns.Find(c.Name) == null)) {
                diff.ColumnsDropped.Add(c);
            }

            //get added and compare mutual constraints
            foreach (Constraint c in Constraints) {
                Constraint c2 = t.FindConstraint(c.Name);
                if (c2 == null) {
                    diff.ConstraintsAdded.Add(c);
                } else {
                    if (c.Script() != c2.Script()) {
                        diff.ConstraintsChanged.Add(c);
                    }
                }
            }
            //get deleted constraints
            foreach (Constraint c in t.Constraints.Where(c => FindConstraint(c.Name) == null)) {
                diff.ConstraintsDeleted.Add(c);
            }

            return diff;
        }
예제 #3
0
        private void CompareConstraints(Table otherTable, CompareConfig compareConfig, TableDiff diff)
        {
            Action<Constraint, Constraint> checkIfConstraintChanged = (c, c2) => {
                if(!c.HasSameProperties(c2, compareConfig)) {
                    diff.ConstraintsChanged.Add(c);
                };
            };

            Func<Constraint, Constraint> getOtherConstraint = (c) => {
                var c2 = otherTable.FindConstraint(c.Name);
                if(compareConfig.IgnoreConstraintsNameMismatch && c2 == null)
                    return  otherTable.FindSimilarConstraint(c);

                return c2;
            };

            CheckSource(compareConfig.ConstraintsCompareMethod,
                Constraints,
                getOtherConstraint,
                c => diff.ConstraintsAdded.Add(c),
                checkIfConstraintChanged);

            Func<Constraint, bool> constraintExistsOnlyInTaget = (c) => {
                var c2 = FindConstraint(c.Name);
                if (compareConfig.IgnoreConstraintsNameMismatch && c2 == null)
                    c2 = FindSimilarConstraint(c);

                return c2 == null;
            };
            CheckTarget(compareConfig.ConstraintsCompareMethod,
                otherTable.Constraints,
                constraintExistsOnlyInTaget,
                c => diff.ConstraintsDeleted.Add(c));
        }