/// <summary> /// Copies the contents of this database to another. /// </summary> /// <param name="sink">The database to copy to.</param> public void CopyTo(Database sink) { Constraints.CopyTo(sink.Constraints); FulltextCatalogs.CopyTo(sink.FulltextCatalogs); FulltextIndexes.CopyTo(sink.FulltextIndexes); Functions.CopyTo(sink.Functions); Permissions.CopyTo(sink.Permissions); Procedures.CopyTo(sink.Procedures); Tables.CopyTo(sink.Tables); Triggers.CopyTo(sink.Triggers); TriggerOrder.CopyTo(sink.TriggerOrder); Views.CopyTo(sink.Views); }
/// <summary> /// Gets the differences between this database and another. /// </summary> /// <param name="other">The other database.</param> /// <returns></returns> public DifferenceSet GetDifferences(Database other) { CalculateDependencies(); other.CalculateDependencies(); DifferenceSet differences = new DifferenceSet(); //check table differences Tables.GetDifferences(other.Tables, dependencies, other.dependencies, differences); foreach (Table table in Tables) { Table otherTable = (Table)other.Tables[table.Name]; table.Indexes.GetDifferences(otherTable == null ? null : otherTable.Indexes, dependencies, other.dependencies, differences); if (!table.Data.AreEqual(otherTable == null ? null : otherTable.Data)) { differences.Add(new Difference(DifferenceType.TableData, table.Data.Name, table.Name), otherTable == null ? null : dependencies); } } foreach (Table otherTable in other.Tables) { Table table = (Table)Tables[otherTable.Name]; if (table == null) { foreach (Index index in otherTable.Indexes) { differences.Add(new Difference(DifferenceType.Removed, index.Name, otherTable.Name), other.dependencies); } } } //check constraint differences Constraints.GetDifferences(other.Constraints, dependencies, other.dependencies, differences); //deal with changing names... foreach (Constraint constraint in Constraints) { Table constrainedTable = other.Tables[constraint.ConstrainedTable]; if (constrainedTable == null) { continue; } if (constraint.Type == ConstraintType.Default) { foreach (Name dependency in other.dependencies[constrainedTable.Name]) { Constraint otherConstraint = other[dependency] as Constraint; if (otherConstraint != null && otherConstraint.Type == ConstraintType.Default && constraint.ConstrainedTable == otherConstraint.ConstrainedTable && constraint.ColumnsEqual(otherConstraint) && constraint != otherConstraint ) { differences.Add(new Difference(DifferenceType.Removed, otherConstraint.Name), other.dependencies); break; } } } else if (constraint.Type == ConstraintType.ForeignKey && other.Tables[constraint.ConstrainedTable] != null) { foreach (Name dependency in other.dependencies[constrainedTable.Name]) { Constraint otherConstraint = other[dependency] as Constraint; if (otherConstraint != null && otherConstraint.Type == ConstraintType.ForeignKey && constraint.ConstrainedTable == otherConstraint.ConstrainedTable && constraint.ReferencedTable == otherConstraint.ReferencedTable && constraint.ColumnsEqual(otherConstraint) && constraint != otherConstraint ) { differences.Add(new Difference(DifferenceType.Removed, otherConstraint.Name), other.dependencies); break; } } } else if (constraint.Type == ConstraintType.PrimaryKey && other.Tables[constraint.ConstrainedTable] != null) { foreach (Name dependency in other.dependencies[constrainedTable.Name]) { Constraint otherConstraint = other[dependency] as Constraint; if (otherConstraint != null && otherConstraint.Type == ConstraintType.PrimaryKey && constraint.ConstrainedTable == otherConstraint.ConstrainedTable && constraint != otherConstraint ) { differences.Add(new Difference(DifferenceType.Removed, otherConstraint.Name), other.dependencies); break; } } } } //check function differences Functions.GetDifferences(other.Functions, dependencies, other.dependencies, differences); //check fulltext catalog differences FulltextCatalogs.GetDifferences(other.FulltextCatalogs, dependencies, other.dependencies, differences); //check fulltext index differences FulltextIndexes.GetDifferences(other.FulltextIndexes, dependencies, other.dependencies, differences); //check procedure differences Procedures.GetDifferences(other.Procedures, dependencies, other.dependencies, differences); //check trigger differences Triggers.GetDifferences(other.Triggers, dependencies, other.dependencies, differences); //check trigger order differences TriggerOrder.GetDifferences(other.TriggerOrder, dependencies, other.dependencies, differences); //check view differences Views.GetDifferences(other.Views, dependencies, other.dependencies, differences); foreach (View view in Views) { View otherView = (View)other.Views[view.Name]; view.Indexes.GetDifferences(otherView == null ? null : otherView.Indexes, dependencies, other.dependencies, differences); } foreach (View view in Views) { View otherView = (View)other.Views[view.Name]; if (otherView != null) { view.Indexes.GetDifferences(otherView.Indexes, dependencies, other.dependencies, differences); } } //check for new permissions Permissions.GetDifferences(other.Permissions, differences); return(differences); }