Exemple #1
0
 private static void HandleDataTables(Database db, string tableNames)
 {
     foreach (var value in tableNames.Split(',')) {
         var schema = "dbo";
         var name = value;
         if (value.Contains(".")) {
             schema = value.Split('.')[0];
             name = value.Split('.')[1];
         }
         var t = db.FindTable(name, schema);
         if (t == null) {
             Console.WriteLine(
                 "warning: could not find data table {0}.{1}",
                 schema, name);
         }
         if (db.DataTables.Contains(t)) continue;
         db.DataTables.Add(t);
     }
 }
Exemple #2
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);
        }
Exemple #3
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;
        }
Exemple #4
0
        private void CompareTables(Database otherDb, CompareConfig compareConfig, DatabaseDiff diff)
        {
            Action<Table, Table> checkIfTableChanged = (t, t2) => {
                //compare mutual tables
                TableDiff tDiff = t.Compare(t2, compareConfig);
                if (tDiff.IsDiff) {
                    diff.TablesDiff.Add(tDiff);
                }
            };

            CheckSource(compareConfig.TablesCompareMethod,
                Tables,
                t => otherDb.FindTable(t.Name, t.Owner),
                t => diff.TablesAdded.Add(t),
                checkIfTableChanged);

            //get deleted tables
            CheckTarget(compareConfig.TablesCompareMethod,
                otherDb.Tables,
                t => FindTable(t.Name, t.Owner) == null,
                t => diff.TablesDeleted.Add(t));
        }
Exemple #5
0
        public DatabaseDiff Compare(Database db)
        {
            var diff = new DatabaseDiff();
            diff.Db = db;

            //compare database properties
            foreach (DbProp p in from p in Props
                let p2 = db.FindProp(p.Name)
                where p.Script() != p2.Script()
                select p) {
                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.Where(t => 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.Where(r => 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.Where(fk => FindForeignKey(fk.Name) == null)) {
                diff.ForeignKeysDeleted.Add(fk);
            }

            //get added and compare mutual assemblies
            foreach (SqlAssembly a in Assemblies) {
                SqlAssembly a2 = db.FindAssembly(a.Name);
                if (a2 == null) {
                    diff.AssembliesAdded.Add(a);
                } else {
                    if (a.ScriptCreate(this) != a2.ScriptCreate(db)) {
                        diff.AssembliesDiff.Add(a);
                    }
                }
            }
            //get deleted assemblies
            foreach (SqlAssembly a in db.Assemblies.Where(a => FindAssembly(a.Name) == null)) {
                diff.AssembliesDeleted.Add(a);
            }

            //get added and compare mutual users
            foreach (SqlUser u in Users) {
                SqlUser u2 = db.FindUser(u.Name);
                if (u2 == null) {
                    diff.UsersAdded.Add(u);
                } else {
                    if (u.ScriptCreate(this) != u2.ScriptCreate(db)) {
                        diff.UsersDiff.Add(u);
                    }
                }
            }
            //get deleted users
            foreach (SqlUser u in db.Users.Where(u => FindUser(u.Name) == null)) {
                diff.UsersDeleted.Add(u);
            }

            //get added and compare view indexes
            foreach (Constraint c in ViewIndexes) {
                Constraint c2 = db.FindViewIndex(c.Name);
                if (c2 == null) {
                    diff.ViewIndexesAdded.Add(c);
                } else {
                    if (c.Script() != c2.Script()) {
                        diff.ViewIndexesDiff.Add(c);
                    }
                }
            }
            //get deleted view indexes
            foreach (Constraint c in db.ViewIndexes.Where(c => FindViewIndex(c.Name) == null)) {
                diff.ViewIndexesDeleted.Add(c);
            }

            return diff;
        }
Exemple #6
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);
            }
        }