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);
        }
Example #2
0
 public AlterPlan GetPlanForThis(DatabaseInfo targetDb, Func<DbDiffAction, bool> useActionFunc)
 {
     var plan = new AlterPlan(m_diff.Target);
     GetOperations(plan, useActionFunc);
     plan.Transform(m_diff._factory.DumperCaps, m_diff._options);
     return plan;
 }
Example #3
0
        private void TestDiff(Action<DatabaseInfo> mangle, string expectedResult)
        {
            var db1 = new DatabaseInfo();
            var t1 = new TableInfo(db1)
                {
                    Name = "t1",
                    Schema = "dbo",
                };
            t1.Columns.Add(new ColumnInfo(t1)
                {
                    Name = "c1",
                    DataType = "int",
                    NotNull = true,
                    CommonType = new DbTypeInt(),
                });
            t1.Columns.Add(new ColumnInfo(t1)
                {
                    Name = "c2",
                    DataType = "int",
                    NotNull = true,
                    CommonType = new DbTypeInt(),
                });

            var ix = new IndexInfo(t1);
            ix.Columns.Add(new ColumnReference { RefColumn = t1.Columns[0] });
            ix.ConstraintName = "ix1";
            t1.Indexes.Add(ix);

            var pk = new PrimaryKeyInfo(t1);
            pk.Columns.Add(new ColumnReference {RefColumn = t1.Columns[0]});
            pk.ConstraintName = "pk_t1";
            t1.PrimaryKey = pk;

            db1.Tables.Add(t1);

            var v1 = new ViewInfo(db1)
                {
                    Name = "v1",
                    Schema = "dbo",
                    CreateSql = "create view v1 as select * from t1",
                };
            db1.Views.Add(v1);


            var db2 = db1.CloneDatabase();
            mangle(db2);
            var plan = new AlterPlan(db1);
            DbDiffTool.AlterDatabase(plan, db1, db2, new DbDiffOptions());
            var caps = SqlServerDatabaseFactory.Instance.DumperCaps;
            plan.AddLogicalDependencies(caps, new DbDiffOptions());
            plan.Transform(caps, new DbDiffOptions());

            var runner = plan.CreateRunner();
            var sw = new StringWriter();
            var sqlo = new SqlOutputStream(SqlServerDatabaseFactory.Instance.CreateDialect(), sw, new SqlFormatProperties());
            var dmp = SqlServerDatabaseFactory.Instance.CreateDumper(sqlo, new SqlFormatProperties());
            runner.Run(dmp, new DbDiffOptions());
            string sql = sw.ToString();
            Assert.IsNotNull(sql);
            //string expectedTran = TransformSql(expectedResult);
            //string sqlTran = TransformSql(sql);
            //for(int i = 0; i < expectedResult.Length; i++)
            //{
            //    Assert.AreEqual(expectedTran[i], sqlTran[i]);
            //}
            Assert.AreEqual(TransformSql(expectedResult), TransformSql(sql));
        }
Example #4
0
 public static AlterPlan PlanAlterTable(TableInfo oldTable, TableInfo newTable, DbDiffOptions opts, DatabaseInfo currentStructure)
 {
     AlterPlan plan = new AlterPlan(currentStructure);
     if (oldTable == null)
     {
         plan.CreateTable(newTable);
     }
     else
     {
         DbObjectPairing pairing = CreateTablePairing(oldTable, newTable);
         AlterTable(plan, oldTable, newTable, opts, pairing);
     }
     return plan;
 }
Example #5
0
 public static void AlterDatabase(AlterPlan plan, DatabaseInfo src, DatabaseInfo dst, DbDiffOptions opts)
 {
     AlterDatabase(plan, new DbObjectPairing(src, dst), opts);
 }
Example #6
0
        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));
            //}
        }
Example #7
0
 public virtual void TransformToImplementedOps(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> replacement, AlterPlan plan)
 {
 }
Example #8
0
 public override void TransformToImplementedOps(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> replacement, AlterPlan plan)
 {
     replacement.Clear();
     replacement.Add(this);
     var table = (TableInfo)NewObject;
     foreach (var fk in new List<ForeignKeyInfo>(table.ForeignKeys))
     {
         table.ForeignKeys.Remove(fk);
         fk.SetDummyTable(table.FullName);
         replacement.Add(new AlterOperation_CreateConstraint
         {
             NewObject = fk
         });
     }
 }
Example #9
0
        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>>();

            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.Columns[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));
                    }
                }
            }
        }
Example #10
0
 public override void TransformToImplementedOps(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> replacement, AlterPlan plan)
 {
     if (!caps.ChangeColumn)
     {
         TransformToRecreateTable(replacement, plan);
         return;
     }
     if (!caps.ChangeAutoIncrement && ((ColumnInfo)OldObject).AutoIncrement != ((ColumnInfo)NewObject).AutoIncrement)
     {
         TransformToRecreateTable(replacement, plan);
         return;
     }
     if (!caps.ChangeComputedColumnExpression && ((ColumnInfo)NewObject).ComputedExpression != null)
     {
         plan.RecreateObject(OldObject, NewObject);
         replacement.Clear();
     }
 }
Example #11
0
 public static void AlterDatabase(AlterPlan plan, DatabaseInfo src, DatabaseInfo dst, DbDiffOptions opts)
 {
     AlterDatabase(plan, new DbObjectPairing(src, dst), opts);
 }
Example #12
0
 public static void AlterSpecificObject(SpecificObjectInfo osrc, SpecificObjectInfo odst, AlterPlan plan, DbDiffOptions opts, DbObjectPairing pairing)
 {
     //bool altered = false;
     if (osrc.CreateSql == odst.CreateSql)
     {
         plan.RenameSpecificObject(osrc, odst.FullName);
         //altered = GenerateRename(osrc.ObjectName, odst.ObjectName,
         //    (old, sch) =>
         //    {
         //        var o2 = new SpecificObjectStructure(osrc);
         //        o2.ObjectName = old;
         //        proc.ChangeSpecificObjectSchema(o2, sch);
         //    },
         //    (old, nam) =>
         //    {
         //        var o2 = new SpecificObjectStructure(osrc);
         //        o2.ObjectName = old;
         //        proc.RenameSpecificObject(o2, nam);
         //    }, caps[osrc.ObjectType].ChangeSchema, caps[osrc.ObjectType].Rename, opts);
     }
     else
     {
         plan.ChangeSpecificObject(osrc, odst);
     }
     //if (!altered)
     //{
     //    proc.DropSpecificObject(osrc);
     //    SpecificObjectStructure odst2 = new SpecificObjectStructure(odst);
     //    odst2.ObjectName = GenerateNewName(osrc.ObjectName, odst.ObjectName, opts);
     //    proc.CreateSpecificObject(odst);
     //}
 }
Example #13
0
        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));
            //}
        }
Example #14
0
 public void PlanCreate(AlterPlan plan, DbDiffOptions opts)
 {
     plan.RecreateObject_Create(RecreatedObject, NewVersion ?? RecreatedObject);
 }
Example #15
0
 public void PlanCreate(AlterPlan plan, DbDiffOptions opts)
 {
     plan.RecreateObject_Create(RecreatedObject, NewVersion ?? RecreatedObject);
 }
Example #16
0
        //public void RunDrop(IAlterProcessor proc, DbDiffOptions opts)
        //{
        //    proc.DropObject(RecreatedObject);
        //}
        //public void RunCreate(IAlterProcessor proc, DbDiffOptions opts)
        //{
        //    if (NewVersion != null && NewVersion is TableObjectStructure)
        //    {
        //        TableObjectStructure obj2 = NewVersion.CloneObject() as TableObjectStructure;
        //        obj2.SetDummyTable(((ITableObjectStructure)RecreatedObject).Table.FullName);
        //        proc.CreateObject(obj2);
        //    }
        //    else
        //    {
        //        proc.CreateObject(RecreatedObject);
        //    }
        //}

        public void PlanDrop(AlterPlan plan, DbDiffOptions opts)
        {
            plan.RecreateObject_Drop(RecreatedObject);
        }
Example #17
0
 public override void AddPhysicalDependencies(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> before, List<AlterOperation> after, AlterPlan plan)
 {
     var oldcol = OldObject as ColumnInfo;
     if (caps.DepCaps.ChangeColumn_Constraint || caps.DepCaps.ChangeColumn_Index)
     {
         foreach (var cnt in ParentTable.Constraints)
         {
             var cc = cnt as ColumnsConstraintInfo;
             if (cc == null) continue;
             if (cc.Columns.Any(c => c.Name == oldcol.Name))
             {
                 if (
                     (cc is IndexInfo && caps.DepCaps.ChangeColumn_Index) ||
                     (!(cc is IndexInfo) && caps.DepCaps.ChangeColumn_Constraint))
                 {
                     plan.RecreateObject(cc, null);
                 }
             }
         }
     }
     if (caps.DepCaps.ChangeColumn_Reference)
     {
         foreach (ForeignKeyInfo fk in ParentTable.GetReferences())
         {
             for (int i = 0; i < fk.RefColumns.Count; i++)
             {
                 if (fk.RefColumns[i].Name == oldcol.Name)
                 {
                     plan.RecreateObject(fk, null);
                 }
             }
         }
     }
 }
Example #18
0
 public virtual void AddLogicalDependencies(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> before, List<AlterOperation> after, AlterPlan plan)
 {
 }
Example #19
0
        public override void AddLogicalDependencies(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> before, List<AlterOperation> after, AlterPlan plan)
        {
            base.AddLogicalDependencies(caps, opts, before, after, plan);

            var pk = OldObject as PrimaryKeyInfo;
            if (pk != null)
            {
                foreach (var col in pk.Columns)
                {
                    foreach (var fk in ParentTable.GetReferences())
                    {
                        bool fkdeleted = false;
                        for (int i = 0; i < fk.RefColumns.Count; i++)
                        {
                            if (fk.RefColumns[i].Name == col.Name)
                            {
                                fkdeleted = true;
                                break;
                            }
                        }
                        if (fkdeleted)
                        {
                            opts.AlterLogger.Warning(String.Format("Dropped reference {0} on table {1}", fk.ConstraintName, fk.OwnerTable.FullName));
                            before.Add(new AlterOperation_DropConstraint { ParentTable = ParentTable, OldObject = fk });
                        }
                    }
                }
            }
        }
Example #20
0
 protected void TransformToRecreateTable(List<AlterOperation> replacement, AlterPlan plan)
 {
     replacement.Clear();
     var op = new AlterOperation_RecreateTable { ParentTable = ParentTable };
     //ParentTable.LoadStructure(TableStructureMembers.All, targetDb);
     foreach (var fk in ParentTable.GetReferences()) plan.RecreateObject(fk, null);
     op.AppendOp(this);
     replacement.Add(op);
 }
Example #21
0
 public override void TransformToImplementedOps(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> replacement, AlterPlan plan)
 {
     var c = GetConstraintCaps(caps, OldObject);
     if (!c.Drop) TransformToRecreateTable(replacement, plan);
 }
Example #22
0
 public static void AlterSpecificObject(SpecificObjectInfo osrc, SpecificObjectInfo odst, AlterPlan plan, DbDiffOptions opts, DbObjectPairing pairing)
 {
     //bool altered = false;
     if (osrc.CreateSql == odst.CreateSql)
     {
         plan.RenameSpecificObject(osrc, odst.FullName);
         //altered = GenerateRename(osrc.ObjectName, odst.ObjectName,
         //    (old, sch) =>
         //    {
         //        var o2 = new SpecificObjectStructure(osrc);
         //        o2.ObjectName = old;
         //        proc.ChangeSpecificObjectSchema(o2, sch);
         //    },
         //    (old, nam) =>
         //    {
         //        var o2 = new SpecificObjectStructure(osrc);
         //        o2.ObjectName = old;
         //        proc.RenameSpecificObject(o2, nam);
         //    }, caps[osrc.ObjectType].ChangeSchema, caps[osrc.ObjectType].Rename, opts);
     }
     else
     {
         plan.ChangeSpecificObject(osrc, odst);
     }
     //if (!altered)
     //{
     //    proc.DropSpecificObject(osrc);
     //    SpecificObjectStructure odst2 = new SpecificObjectStructure(odst);
     //    odst2.ObjectName = GenerateNewName(osrc.ObjectName, odst.ObjectName, opts);
     //    proc.CreateSpecificObject(odst);
     //}
 }
Example #23
0
 public override void TransformToImplementedOps(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> replacement, AlterPlan plan)
 {
     var c = GetConstraintCaps(caps, NewObject);
     if (!c.Change)
     {
         if (c.Create && c.Drop)
         {
             plan.RecreateObject(OldObject, NewObject);
             replacement.Clear();
         }
         else
         {
             TransformToRecreateTable(replacement, plan);
         }
     }
 }
Example #24
0
 /// alters table, decomposes to alter actions; doesn't transform alter plan
 /// proc must be able to run all logical operations
 public static void DecomposeAlterTable(this IAlterProcessor proc, TableInfo oldTable, TableInfo newTable, DbDiffOptions options)
 {
     DbObjectPairing pairing = CreateTablePairing(oldTable, newTable);
     // decompose to alter actions
     AlterPlan plan = new AlterPlan(oldTable.OwnerDatabase);
     AlterTable(plan, oldTable, newTable, options, pairing);
     var run = plan.CreateRunner();
     run.Run(proc, options);
 }
Example #25
0
 public override void TransformToImplementedOps(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> replacement, AlterPlan plan)
 {
     var c = GetConstraintCaps(caps, OldObject);
     if (!c.Rename)
     {
         if (c.Create && c.Drop)
         {
             var newobj = OldObject.CloneObject(null);
             ((ConstraintInfo)newobj).ConstraintName = NewName.Name;
             plan.RecreateObject(OldObject, newobj);
             replacement.Clear();
         }
         else
         {
             TransformToRecreateTable(replacement, plan);
         }
     }
 }
 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);
 }
Example #27
0
 public override void TransformToImplementedOps(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> replacement, AlterPlan plan)
 {
     //if (!caps[OldObject).ObjectType].Change)
     //{
     plan.RecreateObject(OldObject, NewObject);
     replacement.Clear();
     //}
 }
Example #28
0
        //private bool m_isChecked;

        //public bool IsChecked
        //{
        //    get { return m_isChecked; }
        //    set
        //    {
        //        if (Operation == null && Elements.Count == 0) return;
        //        m_isChecked = value;
        //        m_diff.CallChangedAction(this);
        //    }
        //}

        public void GetOperations(AlterPlan plan, Func<DbDiffAction, bool> useActionFunc)
        {
            if (!useActionFunc(this)) return;
            if (Operation != null) plan.Operations.Add(Operation);
            foreach (var elem in Elements) elem.GetOperations(plan, useActionFunc);
        }
Example #29
0
 public override void TransformToImplementedOps(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> replacement, AlterPlan plan)
 {
     if (!caps.PermuteColumns)
     {
         TransformToRecreateTable(replacement, plan);
     }
 }
Example #30
0
        //public void RunDrop(IAlterProcessor proc, DbDiffOptions opts)
        //{
        //    proc.DropObject(RecreatedObject);
        //}
        //public void RunCreate(IAlterProcessor proc, DbDiffOptions opts)
        //{
        //    if (NewVersion != null && NewVersion is TableObjectStructure)
        //    {
        //        TableObjectStructure obj2 = NewVersion.CloneObject() as TableObjectStructure;
        //        obj2.SetDummyTable(((ITableObjectStructure)RecreatedObject).Table.FullName);
        //        proc.CreateObject(obj2);
        //    }
        //    else
        //    {
        //        proc.CreateObject(RecreatedObject);
        //    }
        //}

        public void PlanDrop(AlterPlan plan, DbDiffOptions opts)
        {
            plan.RecreateObject_Drop(RecreatedObject);
        }
Example #31
0
 public override void AddPhysicalDependencies(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> before, List<AlterOperation> after, AlterPlan alterPlan)
 {
     base.AddPhysicalDependencies(caps, opts, before, after, alterPlan);
     //ParentTable.LoadStructure(TableStructureMembers.Columns | TableStructureMembers.PrimaryKey, targetDb);
 }
Example #32
0
 public override void TransformToImplementedOps(AlterProcessorCaps caps, DbDiffOptions opts, List<AlterOperation> replacement, AlterPlan plan)
 {
     base.TransformToImplementedOps(caps, opts, replacement, plan);
     //if (DbName == null) DbName = targetDb.DatabaseName;
 }
Example #33
0
        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();
        }