Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public void CanAddCategoryToCategoryForDeletedColumn()
        {
            var diff = new DatabaseDiff();
            var tableDiff = new TableDiff();
            tableDiff.ColumnsDroped.Add(new Column { Type = "text" });
            diff.TablesDiff.Add(tableDiff);

            var report = diff.GetDiffReport();

            report.Categories[0].Entries.Count.Should().Be(1);
            report.Categories[0].Entries[0].Categories.Count.Should().Be(1);
        }
Ejemplo n.º 3
0
        public void CanAddCategoryToCategoryForChangedConstraint()
        {
            var diff = new DatabaseDiff();
            var tableDiff = new TableDiff();
            tableDiff.ConstraintsChanged.Add(new Constraint("", "", "") { Table = new TableInfo("dbo", "myTable") });
            diff.TablesDiff.Add(tableDiff);

            var report = diff.GetDiffReport();

            report.Categories[0].Entries.Count.Should().Be(1);
            report.Categories[0].Entries[0].Categories.Count.Should().Be(1);
        }
Ejemplo n.º 4
0
        public void CanAddCategoryToCategoryForChangedColumn()
        {
            var diff = new DatabaseDiff();
            var tableDiff = new TableDiff();
            tableDiff.ColumnsDiff.Add(new ColumnDiff(new Column { Type = "text" }, new Column { Type = "text" }, new CompareConfig()));
            diff.TablesDiff.Add(tableDiff);

            var report = diff.GetDiffReport();

            report.Categories[0].Entries.Count.Should().Be(1);
            report.Categories[0].Entries[0].Categories.Count.Should().Be(1);
        }
Ejemplo n.º 5
0
        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;
        }
Ejemplo n.º 6
0
        public DatabaseDiff Compare(Database db)
        {
            var diff = new DatabaseDiff();

            diff.Db = db;

            //compare database properties
            foreach (DbProp p in Props)
            {
                DbProp p2 = db.FindProp(p.Name);
                if (p.Script() != p2.Script())
                {
                    diff.PropsChanged.Add(p);
                }
            }

            //get tables added and changed
            foreach (Table t in Tables)
            {
                Table t2 = db.FindTable(t.Name, t.Owner);
                if (t2 == null)
                {
                    diff.TablesAdded.Add(t);
                }
                else
                {
                    //compare mutual tables
                    TableDiff tDiff = t.Compare(t2);
                    if (tDiff.IsDiff)
                    {
                        diff.TablesDiff.Add(tDiff);
                    }
                }
            }
            //get deleted tables
            foreach (Table t in db.Tables)
            {
                if (FindTable(t.Name, t.Owner) == null)
                {
                    diff.TablesDeleted.Add(t);
                }
            }

            //get procs added and changed
            foreach (Routine r in Routines)
            {
                Routine r2 = db.FindRoutine(r.Name, r.Schema);
                if (r2 == null)
                {
                    diff.RoutinesAdded.Add(r);
                }
                else
                {
                    //compare mutual procs
                    if (r.Text != r2.Text)
                    {
                        diff.RoutinesDiff.Add(r);
                    }
                }
            }
            //get procs deleted
            foreach (Routine r in db.Routines)
            {
                if (FindRoutine(r.Name, r.Schema) == null)
                {
                    diff.RoutinesDeleted.Add(r);
                }
            }

            //get added and compare mutual foreign keys
            foreach (ForeignKey fk in ForeignKeys)
            {
                ForeignKey fk2 = db.FindForeignKey(fk.Name);
                if (fk2 == null)
                {
                    diff.ForeignKeysAdded.Add(fk);
                }
                else
                {
                    if (fk.ScriptCreate() != fk2.ScriptCreate())
                    {
                        diff.ForeignKeysDiff.Add(fk);
                    }
                }
            }
            //get deleted foreign keys
            foreach (ForeignKey fk in db.ForeignKeys)
            {
                if (FindForeignKey(fk.Name) == null)
                {
                    diff.ForeignKeysDeleted.Add(fk);
                }
            }

            return(diff);
        }
Ejemplo n.º 7
0
        public void CanAddCorrectEntryForDeletedConstraint()
        {
            var diff = new DatabaseDiff();
            var tableDiff = new TableDiff();
            tableDiff.ConstraintsDeleted.Add(new Constraint("MyConstraint", "", "") { Table = new TableInfo("dbo", "myTable") });
            diff.TablesDiff.Add(tableDiff);

            var report = diff.GetDiffReport();

            DiffEntry entry;
            (entry = report.Categories[0].Entries[0].Categories[0].Entries.FirstOrDefault()).Should().NotBeNull();
            entry.Name.Should().Be("MyConstraint");
            entry.Type.Should().Be(DiffEntryType.Deleted);
        }
Ejemplo n.º 8
0
        public void CanAddCorrectEntryForDeletedColumn()
        {
            var diff = new DatabaseDiff();
            var tableDiff = new TableDiff();
            tableDiff.ColumnsDroped.Add(new Column { Name = "MyColumn", Type = "text" });
            diff.TablesDiff.Add(tableDiff);

            var report = diff.GetDiffReport();

            DiffEntry entry;
            (entry = report.Categories[0].Entries[0].Categories[0].Entries.FirstOrDefault()).Should().NotBeNull();
            entry.Name.Should().Be("MyColumn");
            entry.Type.Should().Be(DiffEntryType.Deleted);
        }
Ejemplo n.º 9
0
        public void CanAddCorrectEntryForChangedColumn()
        {
            var diff = new DatabaseDiff();
            var tableDiff = new TableDiff();
            tableDiff.ColumnsDiff.Add(new ColumnDiff(new Column { Name = "TargetColumn", Type = "text" }, new Column { Name = "SourceColumn", Type = "text" }, new CompareConfig()));
            diff.TablesDiff.Add(tableDiff);

            var report = diff.GetDiffReport();

            DiffEntry entry;
            (entry = report.Categories[0].Entries[0].Categories[0].Entries.FirstOrDefault()).Should().NotBeNull();
            entry.Name.Should().Be("SourceColumn");
            entry.Type.Should().Be(DiffEntryType.Changed);
        }
Ejemplo n.º 10
0
        public TableDiff Compare(Table otherTable, CompareConfig compareConfig)
        {
            var diff = new TableDiff();
            diff.Owner = otherTable.Owner;
            diff.Name = otherTable.Name;

            CompareColumns(otherTable, compareConfig, diff);
            CompareConstraints(otherTable, compareConfig, diff);

            return diff;
        }
Ejemplo n.º 11
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));
        }
Ejemplo n.º 12
0
        private void CompareColumns(Table otherTable, CompareConfig compareConfig, TableDiff diff)
        {
            Action<Column, Column> checkIfColumnChanged = (c, c2) => {
                //compare mutual columns
                ColumnDiff cDiff = c.Compare(c2, compareConfig);
                if (cDiff.IsDiff) {
                    diff.ColumnsDiff.Add(cDiff);
                }
            };

            CheckSource(compareConfig.ColumnsCompareMethod,
                Columns,
                c => otherTable.Columns.Find(c.Name),
                c => diff.ColumnsAdded.Add(c),
                checkIfColumnChanged);

            CheckTarget(compareConfig.RoutinesCompareMethod,
                otherTable.Columns,
                c => Columns.Find(c.Name) == null,
                c => diff.ColumnsDroped.Add(c));
        }