public static SqlPreCommand RenameOrMove(DiffTable oldTable, ITable newTable) { if (object.Equals(oldTable.Name.Schema, newTable.Name.Schema)) { return(RenameTable(oldTable.Name, newTable.Name.Name)); } if (object.Equals(oldTable.Name.Schema.Database, newTable.Name.Schema.Database)) { var oldNewSchema = oldTable.Name.OnSchema(newTable.Name.Schema); return(SqlPreCommand.Combine(Spacing.Simple, AlterSchema(oldTable.Name, newTable.Name.Schema), oldNewSchema.Equals(newTable.Name) ? null : RenameTable(oldNewSchema, newTable.Name.Name))); } return(SqlPreCommand.Combine(Spacing.Simple, CreateTableSql(newTable), MoveRows(oldTable.Name, newTable.Name, newTable.Columns.Keys), DropTable(oldTable))); }
internal static SqlPreCommand CopyData(ITable newTable, DiffTable oldTable, Replacements rep) { var selectColumns = newTable.Columns .Select(col => oldTable.Columns.TryGetC(col.Key)?.Name ?? GetDefaultValue(newTable, col.Value, rep)) .ToString(", "); var insertSelect = new SqlPreCommandSimple( $@"INSERT INTO {newTable.Name} ({newTable.Columns.Values.ToString(a => a.Name, ", ")}) SELECT {selectColumns} FROM {oldTable.Name}"); if (!newTable.PrimaryKey.Identity) { return(insertSelect); } return(SqlPreCommand.Combine(Spacing.Simple, SqlBuilder.SetIdentityInsert(newTable.Name, true), insertSelect, SqlBuilder.SetIdentityInsert(newTable.Name, false) )); }
internal bool IndexEquals(DiffTable dif, Index mix) { if (this.ViewName != (mix as UniqueIndex)?.ViewName) { return(false); } if (this.ColumnsChanged(dif, mix)) { return(false); } if (this.IsPrimary != mix is PrimaryClusteredIndex) { return(false); } if (this.Type != GetIndexType(mix)) { return(false); } return(true); }
private static Dictionary <string, DiffIndex> ApplyIndexAutoReplacements(DiffTable diff, ITable tab, Dictionary <string, Index> dictionary) { List <string> oldOnly = diff.Indices.Keys.Where(n => !dictionary.ContainsKey(n)).ToList(); List <string> newOnly = dictionary.Keys.Where(n => !diff.Indices.ContainsKey(n)).ToList(); if (oldOnly.Count == 0 || newOnly.Count == 0) { return(diff.Indices); } Dictionary <string, string> replacements = new Dictionary <string, string>(); foreach (var o in oldOnly) { var oldIx = diff.Indices[o]; var nIx = newOnly.FirstOrDefault(n => { var newIx = dictionary[n]; if (oldIx.IsPrimary && newIx is PrimaryClusteredIndex) { return(true); } if (oldIx.IsPrimary || newIx is PrimaryClusteredIndex) { return(false); } if (oldIx.IsUnique != (newIx is UniqueIndex)) { return(false); } if (oldIx.ViewName != null || (newIx is UniqueIndex) && ((UniqueIndex)newIx).ViewName != null) { return(false); } var news = newIx.Columns.Select(c => diff.Columns.TryGetC(c.Name)?.Name).NotNull().ToHashSet(); if (!news.SetEquals(oldIx.Columns)) { return(false); } var uix = newIx as UniqueIndex; if (uix != null && uix.Where != null && !oldIx.IndexName.EndsWith(StringHashEncoder.Codify(uix.Where))) { return(false); } return(true); }); if (nIx != null) { replacements.Add(o, nIx); newOnly.Remove(nIx); } } if (replacements.IsEmpty()) { return(diff.Indices); } return(diff.Indices.SelectDictionary(on => replacements.TryGetC(on) ?? on, dif => dif)); }
bool ColumnsChanged(DiffTable dif, Index mix) { if (this.Columns.Count != mix.Columns.Length) return true; var difColumns = this.Columns.Select(cn => dif.Columns.Values.SingleOrDefault(dc => dc.Name == cn)).ToList(); var perfect = difColumns.ZipOrDefault(mix.Columns, (dc, mc) => dc != null && mc != null && dc.ColumnEquals(mc, ignorePrimaryKey: true)).All(a => a); return !perfect; }
internal bool IndexEquals(DiffTable dif, Index mix) { if (this.ViewName != (mix as UniqueIndex)?.ViewName) return false; if (this.ColumnsChanged(dif, mix)) return false; if (this.IsPrimary != mix is PrimaryClusteredIndex) return false; if (this.Type != GetIndexType(mix)) return false; return true; }
private static Dictionary<string, DiffIndex> ApplyIndexAutoReplacements(DiffTable diff, ITable tab, Dictionary<string, Index> dictionary) { List<string> oldOnly = diff.Indices.Keys.Where(n => !dictionary.ContainsKey(n)).ToList(); List<string> newOnly = dictionary.Keys.Where(n => !diff.Indices.ContainsKey(n)).ToList(); if (oldOnly.Count == 0 || newOnly.Count == 0) return diff.Indices; Dictionary<string, string> replacements = new Dictionary<string, string>(); foreach (var o in oldOnly) { var oldIx = diff.Indices[o]; var nIx = newOnly.FirstOrDefault(n => { var newIx = dictionary[n]; if (oldIx.IsPrimary && newIx is PrimaryClusteredIndex) return true; if (oldIx.IsPrimary || newIx is PrimaryClusteredIndex) return false; if (oldIx.IsUnique != (newIx is UniqueIndex)) return false; if (oldIx.ViewName != null || (newIx is UniqueIndex) && ((UniqueIndex)newIx).ViewName != null) return false; var news = newIx.Columns.Select(c => diff.Columns.TryGetC(c.Name)?.Name).NotNull().ToHashSet(); if (!news.SetEquals(oldIx.Columns)) return false; var uix = newIx as UniqueIndex; if (uix != null && uix.Where != null && !oldIx.IndexName.EndsWith(StringHashEncoder.Codify(uix.Where))) return false; return true; }); if (nIx != null) { replacements.Add(o, nIx); newOnly.Remove(nIx); } } if (replacements.IsEmpty()) return diff.Indices; return diff.Indices.SelectDictionary(on => replacements.TryGetC(on) ?? on, dif => dif); }
public static SqlPreCommand RenameOrMove(DiffTable oldTable, ITable newTable) { if (object.Equals(oldTable.Name.Schema, newTable.Name.Schema)) return RenameTable(oldTable.Name, newTable.Name.Name); if (object.Equals(oldTable.Name.Schema.Database, newTable.Name.Schema.Database)) return SqlPreCommand.Combine(Spacing.Simple, AlterSchema(oldTable.Name, newTable.Name.Schema), oldTable.Name == newTable.Name ? null : RenameTable(new ObjectName(newTable.Name.Schema, oldTable.Name.Name), newTable.Name.Name)); return SqlPreCommand.Combine(Spacing.Simple, CreateTableSql(newTable), MoveRows(oldTable.Name, newTable.Name, newTable.Columns.Keys), DropTable(oldTable.Name)); }