//RenameSpecificObject public static void RenameObject(this IAlterProcessor proc, DatabaseObjectInfo obj, DbDiffOptions opts, NameWithSchema newName) { bool renameOk = false; //var dom = obj as IDomainStructure; //if (dom != null) //{ // renameOk = DbDiffTool.GenerateRename(dom.FullName, newName, // (old, sch) => proc.ChangeDomainSchema(old, sch), // (old, nam) => proc.RenameDomain(old, nam), // proc.AlterCaps.ChangeTableSchema, proc.AlterCaps.RenameDomain, opts); //} var tbl = obj as TableInfo; if (tbl != null) { renameOk = DbDiffTool.GenerateRename(tbl.FullName, newName, (old, sch) => proc.ChangeTableSchema(new TableInfo(null) { FullName = old }, sch), (old, nam) => proc.RenameTable(new TableInfo(null) { FullName = old }, nam), proc.AlterCaps.ChangeTableSchema, proc.AlterCaps.RenameTable, opts); } var col = obj as ColumnInfo; if (col != null) { if (proc.AlterCaps.RenameColumn) { proc.RenameColumn(col, newName.Name); renameOk = true; } } var cnt = obj as ConstraintInfo; if (cnt != null) { if (proc.AlterCaps.RenameConstraint) { proc.RenameConstraint(cnt, newName.Name); renameOk = true; } } var spec = obj as SpecificObjectInfo; if (spec != null) { renameOk = DbDiffTool.GenerateRenameSpecificObject(spec, newName, (old, sch) => proc.ChangeSpecificObjectSchema(old, sch), (old, nam) => proc.RenameSpecificObject(old, nam), proc.AlterCaps.GetSpecificObjectCaps(spec.ObjectType).ChangeSchema, proc.AlterCaps.GetSpecificObjectCaps(spec.ObjectType).Rename, opts); } if (!renameOk) { throw new AlterNotPossibleError(); } }
public static bool GenerateRenameSpecificObject(SpecificObjectInfo oldObj, NameWithSchema newName, Action <SpecificObjectInfo, string> changeSchema, Action <SpecificObjectInfo, string> rename, bool allowChangeSchema, bool allowRename, DbDiffOptions opts) { newName = GenerateNewName(oldObj.FullName, newName, opts); if (DbDiffTool.EqualFullNames(oldObj.FullName, newName, opts)) { return(true); } if (!EqualSchemas(oldObj.FullName.Schema, newName.Schema, opts) && !allowChangeSchema) { return(false); } if (oldObj.FullName.Name != newName.Name && !allowRename) { return(false); } if (!EqualSchemas(oldObj.FullName.Schema, newName.Schema, opts)) { changeSchema(oldObj, newName.Schema); } if (oldObj.FullName.Name != newName.Name) { var tmpo = oldObj.CloneSpecificObject(); tmpo.FullName = new NameWithSchema(newName.Schema, oldObj.FullName.Name); rename(tmpo, newName.Name); } return(true); }
public static bool GenerateRename(NameWithSchema oldName, NameWithSchema newName, Action <NameWithSchema, string> changeSchema, Action <NameWithSchema, string> rename, bool allowChangeSchema, bool allowRename, DbDiffOptions opts) { newName = GenerateNewName(oldName, newName, opts); if (DbDiffTool.EqualFullNames(oldName, newName, opts)) { return(true); } if (!EqualSchemas(oldName.Schema, newName.Schema, opts) && !allowChangeSchema) { return(false); } if (oldName.Name != newName.Name && !allowRename) { return(false); } if (!EqualSchemas(oldName.Schema, newName.Schema, opts)) { changeSchema(oldName, newName.Schema); } if (oldName.Name != newName.Name) { rename(new NameWithSchema(newName.Schema, oldName.Name), newName.Name); } return(true); }
public static void AlterDatabase(this IAlterProcessor proc, DatabaseInfo src, DatabaseInfo dst, DatabaseInfo targetDb, DbDiffOptions opts, Action <AlterPlan> extendPlan) { AlterPlan plan = new AlterPlan(targetDb); DbDiffTool.AlterDatabase(plan, src, dst, opts); if (extendPlan != null) { extendPlan(plan); } plan.Transform(proc.AlterCaps, opts); var run = plan.CreateRunner(); run.Run(proc, opts); }
private void PairTables() { foreach (var tsrc in _src.Tables) { if (IsPaired(tsrc)) { continue; } foreach (var tdst in _dst.Tables) { if (DbDiffTool.EqualFullNames(tsrc.FullName, tdst.FullName, _options) && !IsPaired(tdst)) { PairObjects(tsrc, tdst); break; } } //TableStructure tdst = m_dst.FindTable(tsrc.FullName); //if (tdst != null) PairObjects(tsrc, tdst); } if (_options.AllowPairRenamedTables) { // snazime se tabulky sparovat na zaklade stejnych jmen VSECH sloupcu foreach (var tsrc in _src.Tables) { if (IsPaired(tsrc)) { continue; } foreach (var tdst in _dst.Tables) { if (DbDiffTool.EqualColumnNames(tsrc, tdst) && !IsPaired(tdst)) { PairObjects(tsrc, tdst); break; } } } } foreach (var tsrc in _src.Tables) { var tdst = FindPair(tsrc); if (tdst != null) { PairTableContent(tsrc, tdst); } } }
public override void AddLogicalDependencies(AlterProcessorCaps caps, DbDiffOptions opts, List <AlterOperation> before, List <AlterOperation> after, AlterPlan plan) { var oldcol = OldObject as ColumnInfo; var newcol = NewObject as ColumnInfo; //var recreateFks = new List<ForeignKeyInfo>(); //var changeCols = new List<Tuple<ColumnInfo, ColumnInfo>>(); if (caps.DropConstraint) { foreach (var fk in ParentTable.GetReferences()) { for (int i = 0; i < fk.RefColumns.Count; i++) { if (fk.RefColumns[i].Name == oldcol.Name) { //plan.RecreateObject(fk, null); var table = fk.OwnerTable; var othercol = table.ColumnByName(fk.Columns[i].Name); // compare types with ignoring autoincrement flag // HACK: ignore specific attributes var opts2 = opts.Clone(); opts2.IgnoreSpecificData = true; if (!DbDiffTool.EqualTypes(othercol, newcol, opts2)) { var othercolNewType = othercol.CloneColumn(); CopyDataType(othercolNewType, newcol); after.Add(new AlterOperation_ChangeColumn { ParentTable = table, OldObject = othercol, NewObject = othercolNewType, }); } opts.AlterLogger.Warning(String.Format("Changed referenced column {0}.{1}", fk.OwnerTable.FullName, othercol.Name)); } } } } }
//private void PairDomains() //{ // foreach (DomainStructure dsrc in m_src.Domains) // { // if (IsPaired(dsrc)) continue; // foreach (DomainStructure ddst in m_dst.Domains) // { // if (DbDiffTool.EqualFullNames(dsrc.FullName, ddst.FullName, m_options)) // { // if (!IsPaired(ddst)) PairObjects(dsrc, ddst); // } // } // } //} //private void PairViews() //{ // foreach (var osrc in m_src.Views) // { // if (IsPaired(osrc)) continue; // foreach (SpecificObjectStructure odst in m_dst.Views) // { // if (odst.ObjectType == osrc.ObjectType && DbDiffTool.EqualFullNames(osrc.ObjectName, odst.ObjectName, m_options)) // { // if (!IsPaired(odst)) PairObjects(osrc, odst); // } // } // } //} private void PairSpecificObjects() { foreach (var osrc in _src.GetAllSpecificObjects()) { if (IsPaired(osrc)) { continue; } foreach (var odst in _dst.GetAllSpecificObjects()) { if (odst.ObjectType == osrc.ObjectType && DbDiffTool.EqualFullNames(osrc.FullName, odst.FullName, _options)) { if (!IsPaired(odst)) { PairObjects(osrc, odst); } } } } }
private void CreateActions() { _plan = new AlterPlan(_dst); DbDiffTool.AlterDatabase(Plan, new DbObjectPairing(_dst, _src), _options); DbDiffAction lastAlterTable = null; foreach (var op in _plan.Operations) { DbDiffAction act; if (op.ParentTable != null) { // this operation should be added to ALTER TABLE operation if (lastAlterTable == null || lastAlterTable.ParentTable != op.ParentTable) { lastAlterTable = new DbDiffAction(this); lastAlterTable.ParentTable = op.ParentTable; lastAlterTable.AfterCreate(); _actions.Elements.Add(lastAlterTable); } act = new DbDiffAction(this); act.Operation = op; //act.IsChecked = true; act.AfterCreate(); lastAlterTable.Elements.Add(act); } else { act = new DbDiffAction(this); act.Operation = op; lastAlterTable = null; act.AfterCreate(); _actions.Elements.Add(act); } } //this.AlterDatabase(m_dst, m_src, m_options); }
public static bool EqualsColumns(ColumnInfo a, ColumnInfo b, bool checkName, bool checkDefault, DbDiffOptions opts, DbObjectPairing pairing) { if (checkName && !DbDiffTool.EqualNames(a.Name, b.Name, opts)) { opts.DiffLogger.Trace("Column, different name: {0}; {1}", a, b); return(false); } //if (!DbDiffTool.EqualFullNames(a.Domain, b.Domain, opts)) //{ // opts.DiffLogger.Trace("Column {0}, {1}: different domain: {2}; {3}", a, b, a.Domain, b.Domain); // return false; //} if (a.ComputedExpression != b.ComputedExpression) { opts.DiffLogger.Trace("Column {0}, {1}: different computed expression: {2}; {3}", a, b, a.ComputedExpression, b.ComputedExpression); return(false); } if (a.ComputedExpression != null) { return(true); } if (checkDefault) { if (a.DefaultValue == null) { if (a.DefaultValue != b.DefaultValue) { opts.DiffLogger.Trace("Column {0}, {1}: different default values: {2}; {3}", a, b, a.DefaultValue, b.DefaultValue); return(false); } } else { if (!a.DefaultValue.Equals(b.DefaultValue)) { opts.DiffLogger.Trace("Column {0}, {1}: different default values: {2}; {3}", a, b, a.DefaultValue, b.DefaultValue); return(false); } } if (a.DefaultConstraint != b.DefaultConstraint) { opts.DiffLogger.Trace("Column {0}, {1}: different default constraint names: {2}; {3}", a, b, a.DefaultConstraint, b.DefaultConstraint); return(false); } } if (a.NotNull != b.NotNull) { opts.DiffLogger.Trace("Column {0}, {1}: different nullable: {2}; {3}", a, b, a.NotNull, b.NotNull); return(false); } if (a.AutoIncrement != b.AutoIncrement) { opts.DiffLogger.Trace("Column {0}, {1}: different autoincrement: {2}; {3}", a, b, a.AutoIncrement, b.AutoIncrement); return(false); } if (a.IsSparse != b.IsSparse) { opts.DiffLogger.Trace("Column {0}, {1}: different is_sparse: {2}; {3}", a, b, a.IsSparse, b.IsSparse); return(false); } if (!EqualTypes(a, b, opts)) { return(false); } //var btype = b.DataType; //var atype = a.DataType; //if (pairing != null && pairing.Target != null && pairing.Source.Dialect != null) //{ // btype = pairing.Source.Dialect.MigrateDataType(b, btype, pairing.Source.Dialect.GetDefaultMigrationProfile(), null); // btype = pairing.Source.Dialect.GenericTypeToSpecific(btype).ToGenericType(); // // normalize type // atype = pairing.Source.Dialect.GenericTypeToSpecific(atype).ToGenericType(); //} //if (!EqualTypes(atype, btype, opts)) //{ // opts.DiffLogger.Trace("Column {0}, {1}: different types: {2}; {3}", a, b, a.DataType, b.DataType); // return false; //} //if (!opts.IgnoreColumnCollation && a.Collation != b.Collation) //{ // opts.DiffLogger.Trace("Column {0}, {1}: different collations: {2}; {3}", a, b, a.Collation, b.Collation); // return false; //} //if (!opts.IgnoreColumnCharacterSet && a.CharacterSet != b.CharacterSet) //{ // opts.DiffLogger.Trace("Column {0}, {1}: different character sets: {2}; {3}", a, b, a.CharacterSet, b.CharacterSet); // return false; //} return(true); }
public static void AlterTable(AlterPlan plan, TableInfo oldTable, TableInfo newTable, DbDiffOptions opts, DbObjectPairing pairing) { //plan.BeginFixedOrder(); if (oldTable == null) { throw new ArgumentNullException("oldTable", "DBSH-00141 oldTable is null"); } if (newTable == null) { throw new ArgumentNullException("newTable", "DBSH-00142 newTable is null"); } //bool processed; //proc.AlterTable(oldTable, newTable, out processed); //if (processed) return; //InMemoryTableOperation dataOps = null; //if (oldTable.FixedData != null) dataOps = new InMemoryTableOperation(oldTable.FixedData.Structure); NameWithSchema newTableName = GenerateNewName(oldTable.FullName, newTable.FullName, opts); bool permuteColumns = false; bool insertColumns = false; //bool renameColumns = false; List <int> columnMap = new List <int>(); List <int> constraintMap = new List <int>(); foreach (var col in newTable.Columns) { columnMap.Add(oldTable.Columns.IndexOfIf(c => c.GroupId == col.GroupId)); } foreach (var cnt in newTable.Constraints) { int cindex = oldTable.Constraints.IndexOfIf(c => c.GroupId == cnt.GroupId); if (cindex < 0 && cnt is PrimaryKeyInfo) { // primary keys for one table are equal cindex = oldTable.Constraints.IndexOfIf(c => c is PrimaryKeyInfo); } constraintMap.Add(cindex); } if (!opts.IgnoreColumnOrder) { // count alter requests int lastcol = -1; foreach (int col in columnMap) { if (col < 0) { continue; } if (col < lastcol) { permuteColumns = true; } lastcol = col; } bool wasins = false; foreach (int col in columnMap) { if (col < 0) { wasins = true; } if (col >= 0 && wasins) { insertColumns = true; } } } int index; // drop constraints index = 0; foreach (var cnt in oldTable.Constraints) { if (constraintMap.IndexOf(index) < 0) { plan.DropConstraint(cnt); } index++; } // drop columns index = 0; foreach (var col in oldTable.Columns) { if (columnMap.IndexOf(index) < 0) { plan.DropColumn(col); //if (dataOps != null) dataOps.DropColumn(col.ColumnName); } index++; } if (!DbDiffTool.EqualFullNames(oldTable.FullName, newTable.FullName, opts)) { plan.RenameTable(oldTable, newTable.FullName); } // create columns index = 0; foreach (var col in newTable.Columns) { if (columnMap[index] < 0) { var newcol = col.CloneColumn(); plan.CreateColumn(oldTable, newcol); //if (dataOps != null) dataOps.CreateColumn(newcol); } index++; } // change columns index = 0; foreach (var col in newTable.Columns) { if (columnMap[index] >= 0) { var src = oldTable.Columns[columnMap[index]]; if (!DbDiffTool.EqualsColumns(src, col, true, true, opts, pairing)) { using (var ctx = new DbDiffChangeLoggerContext(opts, NopMessageLogger.Instance, DbDiffOptsLogger.DiffLogger)) { if (DbDiffTool.EqualsColumns(src, col, false, true, opts, pairing)) { plan.RenameColumn(src, col.Name); } else { plan.ChangeColumn(src, col); } //if (dataOps != null && src.ColumnName != col.ColumnName) dataOps.RenameColumn(src.ColumnName, col.ColumnName); } } } index++; } //// create fixed data script //var script = AlterFixedData(oldTable.FixedData, newTable.FixedData, dataOps, opts); //if (script != null) plan.UpdateData(oldTable.FullName, script); // change constraints index = 0; foreach (var cnt in newTable.Constraints) { if (constraintMap[index] >= 0) { var src = oldTable.Constraints[constraintMap[index]]; if (DbDiffTool.EqualsConstraints(src, cnt, opts, false, pairing) && src.ConstraintName != cnt.ConstraintName) { //if (cnt is IPrimaryKey && (pairing.Source.Dialect.DialectCaps.AnonymousPrimaryKey || pairing.Target.Dialect.DialectCaps.AnonymousPrimaryKey)) //{ // // do nothing //} //else //{ plan.RenameConstraint(src, cnt.ConstraintName); //} } else { if (!DbDiffTool.EqualsConstraints(src, cnt, opts, true, pairing)) { plan.ChangeConstraint(src, cnt); } } } index++; } // create constraints index = 0; foreach (var cnt in newTable.Constraints) { if (constraintMap[index] < 0) { plan.CreateConstraint(oldTable, cnt); } index++;; } if (permuteColumns || insertColumns) { plan.ReorderColumns(oldTable, new List <string>((from c in newTable.Columns select c.Name))); } var alteredOptions = GetTableAlteredOptions(oldTable, newTable, opts); if (alteredOptions.Count > 0) { plan.ChangeTableOptions(oldTable, alteredOptions); } //plan.EndFixedOrder(); }
public void RecreateTable(TableInfo src, TableInfo dst) { DbDiffTool.DecomposeAlterTable(this, src, dst, new DbDiffOptions()); }
public static void AlterDatabase(AlterPlan plan, DbObjectPairing pairing, DbDiffOptions opts) { var src = pairing.Source; var dst = pairing.Target; //var caps = proc.AlterCaps; //// domains //foreach (IDomainStructure dsrc in src.Domains) //{ // IDomainStructure ddst = pairing.FindPair(dsrc); // if (ddst == null) // { // plan.DropDomain(dsrc); // } // else if (!DbDiffTool.EqualDomains(dsrc, ddst, opts, true)) // { // if (DbDiffTool.EqualDomains(dsrc, ddst, opts, false)) // { // plan.RenameDomain(dsrc, ddst.FullName); // } // else // { // plan.ChangeDomain(dsrc, ddst); // } // } //} //foreach (IDomainStructure ddst in dst.Domains) //{ // if (!pairing.IsPaired(ddst)) plan.CreateDomain(ddst); //} // drop tables foreach (TableInfo tsrc in new List <TableInfo>(src.Tables)) { TableInfo tdst = pairing.FindPair(tsrc); if (tdst == null) { plan.DropTable(tsrc); } } // change tables foreach (TableInfo tsrc in new List <TableInfo>(src.Tables)) { TableInfo tdst = pairing.FindPair(tsrc); if (tdst == null) { continue; } if (!DbDiffTool.EqualTables(tsrc, tdst, opts, pairing)) { DbDiffTool.AlterTable(plan, tsrc, tdst, opts, pairing); } //else //{ // DbDiffTool.AlterFixedData(plan, tsrc, tdst, opts); //} } // create tables foreach (TableInfo tdst in dst.Tables) { if (!pairing.IsPaired(tdst)) { //var script = DbDiffTool.AlterFixedData(null, tdst.FixedData, null, opts); plan.CreateTable(tdst); //if (script != null) plan.UpdateData(tdst.FullName, script); } } // specific objects foreach (var osrc in src.GetAllSpecificObjects()) { //var repr = SpecificRepresentationAddonType.Instance.FindRepresentation(osrc.ObjectType); //if (!repr.UseInSynchronization) continue; var odst = pairing.FindPair(osrc); if (odst == null) { plan.DropSpecificObject(osrc); //proc.DropSpecificObject(osrc); } else if (!DbDiffTool.EqualsSpecificObjects(osrc, odst, opts)) { DbDiffTool.AlterSpecificObject(osrc, odst, plan, opts, pairing); } } foreach (var odst in dst.GetAllSpecificObjects()) { //var repr = SpecificRepresentationAddonType.Instance.FindRepresentation(odst.ObjectType); //if (!repr.UseInSynchronization) continue; if (!pairing.IsPaired(odst)) { plan.CreateSpecificObject(odst); //proc.CreateSpecificObject(odst); } } //foreach (ISchemaStructure ssrc in src.Schemata) //{ // ISchemaStructure sdst = pairing.FindPair(ssrc); // if (sdst == null) // { // plan.DropSchema(ssrc); // } // else if (ssrc.SchemaName != sdst.SchemaName) // { // plan.RenameSchema(ssrc, sdst.SchemaName); // //if (caps.RenameSchema) proc.RenameSchema(ssrc, sdst.SchemaName); // //else // //{ // // proc.DropSchema(ssrc); // // proc.CreateSchema(sdst); // //} // } //} //foreach (ISchemaStructure sdst in dst.Schemata) //{ // if (!pairing.IsPaired(sdst)) plan.CreateSchema(sdst); //} //var alteredOptions = GetDatabaseAlteredOptions(src, dst, opts); //if (alteredOptions.Count > 0) plan.ChangeDatabaseOptions(null, alteredOptions); //if (opts.SchemaMode == DbDiffSchemaMode.Ignore) //{ // plan.RunNameTransformation(new SetSchemaNameTransformation(null)); //} }