private StringBuilder getColumnCreates() { _createdColumns = new List <string>(); var sb = new StringBuilder(); bool firstIn = true; foreach (var col in databaseObject.Columns.OrderBy(c => c.OrdinalPosition)) { _createdColumns.Add(col.ColumnName); if (!string.IsNullOrEmpty(col.Formula)) { sb.AppendLine("\t" + (firstIn ? string.Empty : ",") + string.Format("[{0}] AS {1}", col.ColumnName, col.Formula)); firstIn = false; continue; } string part2 = new ColumnSynchronization(col).GetPostScript(); string def = new ColumnSynchronization(col).GetDefaultScript(); sb.AppendLine("\t" + (firstIn ? string.Empty : ",") + string.Format("[{0}] [{1}]{2} {3} {4}", col.ColumnName, col.DataType, part2, col.IsNullable ? "NULL" : "NOT NULL", def)); firstIn = false; } return(sb); }
public override List <SynchronizationItem> GetAlterItems(DatabaseObjectBase target) { var targetTable = target as Table; var items = new List <SynchronizationItem>(); bool recreate = false; foreach (var fc in databaseObject.Columns) { var tc = targetTable.Columns.FirstOrDefault(c => c.ColumnName.ToLower() == fc.ColumnName.ToLower()); if (tc != null && tc.IsIdentity != fc.IsIdentity) { var item = getRecreateTableItem(targetTable); item.Differences.Add(new Difference() { PropertyName = "IsIdentity", SourceValue = fc.IsIdentity.ToString(), TargetValue = tc.IsIdentity.ToString() }); items.Add(item); recreate = true; targetTable = null; break; } if (tc != null && tc.DataType == "timestamp") { var diff = new ColumnSynchronization(fc).GetPropertyDifferences(tc); if (diff.Any()) { var item = getRecreateTableItem(targetTable); item.Differences.AddRange(diff); items.Add(item); recreate = true; targetTable = null; break; } } } if (!recreate) { foreach (var tk in targetTable.ForeignKeys) { if (!databaseObject.ForeignKeys.Any(k => k.ForeignKeyName.ToLower() == tk.ForeignKeyName.ToLower())) { items.AddRange(new ForeignKeySynchronization(tk).GetDropItems()); } } foreach (var fk in databaseObject.ForeignKeys) { var tk = targetTable.ForeignKeys.FirstOrDefault(x => x.ObjectName == fk.ObjectName); if (tk != null) { items.AddRange(new ForeignKeySynchronization(fk).GetSynchronizationItems(tk)); } else { items.AddRange(new ForeignKeySynchronization(fk).GetCreateItems()); } } foreach (var tc in targetTable.Columns) { if (!databaseObject.Columns.Any(c => c.ColumnName.ToLower() == tc.ColumnName.ToLower())) { items.AddRange(new ColumnSynchronization(tc).GetDropItems()); } } _createdColumns = new List <string>(); _alteredColumns = new List <string>(); foreach (var fc in databaseObject.Columns) { var tc = targetTable.Columns.FirstOrDefault(c => c.ColumnName.ToLower() == fc.ColumnName.ToLower()); if (tc == null) { items.AddRange(new ColumnSynchronization(fc).GetAddAlterItems(null)); _createdColumns.Add(fc.ColumnName); } else { var alteredItems = new ColumnSynchronization(fc).GetSynchronizationItems(tc); if (alteredItems.Any()) { items.AddRange(alteredItems); _alteredColumns.Add(fc.ColumnName); } } } } if (!recreate) { items.AddRange(getKeyConstraintUpdateItems(targetTable)); } items.AddRange(getIndexCreateUpdateItems(targetTable)); items.AddRange(getDefaultConstraintCreateUpdateItems(targetTable)); items.AddRange(getTriggerUpdateItems(targetTable)); foreach (var column in databaseObject.Columns) { items.AddRange(ExtendedPropertySynchronization.GetExtendedProperties(column, targetTable == null ? null : targetTable.Columns.FirstOrDefault(c => c.ColumnName == column.ColumnName))); } return(items); }