Exemplo n.º 1
0
 private void Compare(DatabaseTable databaseTable,
                      ICollection <DatabaseConstraint> firstConstraints,
                      ICollection <DatabaseConstraint> secondConstraints)
 {
     foreach (var constraint in firstConstraints)
     {
         var constraintName  = constraint.Name;
         var matchConstraint = secondConstraints.FirstOrDefault(c => c.Name == constraintName);
         if (matchConstraint == null)
         {
             CreateResult(ResultType.Delete, databaseTable, constraintName,
                          _writer.DropConstraint(databaseTable, constraint));
             continue;
         }
         if (!ConstraintColumnsEqual(constraint, matchConstraint))
         {
             CreateResult(ResultType.Change, databaseTable, constraintName,
                          _writer.DropConstraint(databaseTable, constraint) + Environment.NewLine +
                          _writer.AddConstraint(databaseTable, matchConstraint));
             continue;
         }
         if (constraint.ConstraintType == ConstraintType.Check &&
             constraint.Expression != matchConstraint.Expression)
         {
             CreateResult(ResultType.Change, databaseTable, constraintName,
                          _writer.DropConstraint(databaseTable, constraint) + Environment.NewLine +
                          _writer.AddConstraint(databaseTable, matchConstraint));
         }
         if (constraint.ConstraintType == ConstraintType.ForeignKey &&
             constraint.RefersToTable != matchConstraint.RefersToTable)
         {
             //unlikely a foreign key will change the fk table without changing name
             CreateResult(ResultType.Change, databaseTable, constraintName,
                          _writer.DropConstraint(databaseTable, constraint) + Environment.NewLine +
                          _writer.AddConstraint(databaseTable, matchConstraint));
         }
     }
     foreach (var constraint in secondConstraints)
     {
         var constraintName  = constraint.Name;
         var firstConstraint = firstConstraints.FirstOrDefault(c => c.Name == constraintName);
         if (firstConstraint == null)
         {
             CreateResult(ResultType.Add, databaseTable, constraintName,
                          _writer.AddConstraint(databaseTable, constraint));
         }
     }
 }