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; }