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); }
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; }
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; }
private void CompareRoutines(Database otherDb, CompareConfig compareConfig, DatabaseDiff diff) { Action<Routine, Routine> checkIfRoutineChanged = (r, r2) => { if (compareConfig.IgnoreRoutinesTextMismatch) { return; } //compare mutual procs if (r.Text.Replace("\r\n", "\n") != r2.Text.Replace("\r\n", "\n")) { diff.RoutinesDiff.Add(r); } }; CheckSource(compareConfig.RoutinesCompareMethod, Routines, r => otherDb.FindRoutine(r.Name, r.Schema), r => diff.RoutinesAdded.Add(r), checkIfRoutineChanged); //get procs deleted in source db or added in target db CheckTarget(compareConfig.RoutinesCompareMethod, otherDb.Routines, r => FindRoutine(r.Name, r.Schema) == null, r => diff.RoutinesDeleted.Add(r)); }
public void TestScriptDeletedProc() { var source = new Database(); source.Routines.Add(new Routine("dbo", "test")); source.FindRoutine("test", "dbo").RoutineType = Routine.RoutineKind.Procedure; source.FindRoutine("test", "dbo").Text = @" create procedure [dbo].[test] as select * from Table1 "; var target = new Database(); string scriptUp = target.Compare(source).Script(); string scriptDown = source.Compare(target).Script(); Assert.IsTrue(scriptUp.ToLower().Contains("drop procedure [dbo].[test]")); Assert.IsTrue(scriptDown.ToLower().Contains("create procedure [dbo].[test]")); }