Ejemplo n.º 1
0
        private void TestTableDiff(string tableName, Action <TableInfo> mangle)
        {
            var dbinfo           = FullAnalyse();
            var table            = dbinfo.FindTableLike(tableName);
            var alteredTableInfo = table.CloneTable();

            mangle(alteredTableInfo);

            var       caps = DatabaseFactory.DumperCaps;
            var       opts = new DbDiffOptions();
            AlterPlan plan = DbDiffTool.PlanAlterTable(table, alteredTableInfo, opts, dbinfo);

            plan.AddLogicalDependencies(caps, opts);
            plan.Transform(caps, opts);

            string sql = GenerateScriptForPlan(plan);

            _output.WriteLine(sql);

            RunPlan(plan);

            var newStructure = FullAnalyse();
            var newTableInfo = newStructure.FindTableLike(tableName);

            AssertEqual(alteredTableInfo, newTableInfo);
        }
Ejemplo n.º 2
0
 public override void ChangeColumn(IColumnStructure oldcol, IColumnStructure newcol, IEnumerable <IConstraint> constraints)
 {
     if (oldcol.ColumnName != newcol.ColumnName)
     {
         PutCmd("^alter ^table %f ^rename ^column %i ^to %i", oldcol.Table, oldcol.ColumnName, newcol.ColumnName);
     }
     if (!DbDiffTool.EqualTypes(oldcol.DataType, newcol.DataType, new DbDiffOptions()))
     {
         PutCmd("^alter ^table %f ^alter ^column %i ^type %s", newcol.Table, newcol.ColumnName, m_dialect.GenericTypeToSpecific(newcol.DataType));
     }
     if (oldcol.IsNullable != newcol.IsNullable)
     {
         if (newcol.IsNullable)
         {
             PutCmd("^alter ^table %f ^alter ^column %i ^drop ^not ^null", newcol.Table, newcol.ColumnName);
         }
         else
         {
             PutCmd("^alter ^table %f ^alter ^column %i ^set ^not ^null", newcol.Table, newcol.ColumnName);
         }
     }
     if (oldcol.DefaultValue.SafeGetSql(m_dialect) != newcol.DefaultValue.SafeGetSql(m_dialect))
     {
         if (newcol.DefaultValue == null)
         {
             PutCmd("^alter ^table %f ^alter ^column %i ^drop ^default", newcol.Table, newcol.ColumnName);
         }
         else
         {
             PutCmd("^alter ^table %f ^alter ^column %i ^set ^default %s", newcol.Table, newcol.ColumnName, newcol.DefaultValue.SafeGetSql(m_dialect));
         }
     }
     this.CreateConstraints(constraints);
 }
Ejemplo n.º 3
0
 public override void ChangeColumn(IColumnStructure oldcol, IColumnStructure newcol, IEnumerable <IConstraint> constrains)
 {
     if (oldcol.ColumnName != newcol.ColumnName)
     {
         RenameColumn(oldcol, newcol.ColumnName);
     }
     if (oldcol.IsNullable != newcol.IsNullable)
     {
         PutCmd("^alter ^table %f ^modify %i %k", newcol.Table, newcol.ColumnName, newcol.IsNullable ? "null" : "not null");
     }
     if (!DbDiffTool.EqualDefaultValues(oldcol, newcol))
     {
         if (newcol.DefaultValue == null)
         {
             PutCmd("^alter ^table %f ^modify %i ^default ^null", newcol.Table, newcol.ColumnName);
         }
         else
         {
             Put("^alter ^table %f ^modify %i ", newcol.Table, newcol.ColumnName);
             ColumnDefinition_Default(newcol);
             this.EndCommand();
         }
     }
     if (!DbDiffTool.EqualTypes(oldcol.DataType, newcol.DataType, new DbDiffOptions()))
     {
         Put("^alter ^table %f ^modify %i ", newcol.Table, newcol.ColumnName);
         ColumnDefinition(newcol, false, false, false);
     }
     EndCommand();
 }
Ejemplo n.º 4
0
 public override void ChangeColumn(ColumnInfo oldcol, ColumnInfo newcol, IEnumerable <ConstraintInfo> constraints)
 {
     if (oldcol.Name != newcol.Name)
     {
         PutCmd("^alter ^table %f ^rename ^column %i ^to %i", oldcol.OwnerTable, oldcol.Name, newcol.Name);
     }
     if (!DbDiffTool.EqualTypes(oldcol, newcol, new DbDiffOptions()))
     {
         PutCmd("^alter ^table %f ^alter ^column %i ^type %s", newcol.OwnerTable, newcol.Name, newcol.DataType);
     }
     if (oldcol.NotNull != newcol.NotNull)
     {
         if (newcol.NotNull)
         {
             PutCmd("^alter ^table %f ^alter ^column %i ^set ^not ^null", newcol.OwnerTable, newcol.Name);
         }
         else
         {
             PutCmd("^alter ^table %f ^alter ^column %i ^drop ^not ^null", newcol.OwnerTable, newcol.Name);
         }
     }
     if (oldcol.DefaultValue != newcol.DefaultValue)
     {
         if (newcol.DefaultValue == null)
         {
             PutCmd("^alter ^table %f ^alter ^column %i ^drop ^default", newcol.OwnerTable, newcol.Name);
         }
         else
         {
             PutCmd("^alter ^table %f ^alter ^column %i ^set ^default %s", newcol.OwnerTable, newcol.Name, newcol.DefaultValue);
         }
     }
     this.CreateConstraints(constraints);
 }
Ejemplo n.º 5
0
 public override void ChangeColumn(ColumnInfo oldcol, ColumnInfo newcol, IEnumerable <ConstraintInfo> constraints)
 {
     if (DbDiffTool.EqualsColumns(oldcol, newcol, false, false))
     {
         DropDefault(oldcol);
         if (oldcol.Name != newcol.Name)
         {
             RenameColumn(oldcol, newcol.Name);
         }
         CreateDefault(newcol);
     }
     else
     {
         DropDefault(oldcol);
         if (oldcol.Name != newcol.Name)
         {
             RenameColumn(oldcol, newcol.Name);
         }
         Put("^alter ^table %f ^alter ^column %i ", newcol.OwnerTable.FullName, newcol.Name);
         // remove autoincrement flag
         var newcol2 = newcol.CloneColumn();
         newcol2.SetDummyTable(newcol.OwnerTable.FullName);
         newcol2.AutoIncrement = false;
         ColumnDefinition(newcol2, false, true, true);
         EndCommand();
         CreateDefault(newcol);
         this.CreateConstraints(constraints);
     }
 }
Ejemplo n.º 6
0
        private void PairTables()
        {
            foreach (TableStructure tsrc in m_src.Tables)
            {
                if (IsPaired(tsrc))
                {
                    continue;
                }
                foreach (TableStructure tdst in m_dst.Tables)
                {
                    if (DbDiffTool.EqualFullNames(tsrc.FullName, tdst.FullName, m_options) && !IsPaired(tdst))
                    {
                        PairObjects(tsrc, tdst);
                        break;
                    }
                }
                //TableStructure tdst = m_dst.FindTable(tsrc.FullName);
                //if (tdst != null) PairObjects(tsrc, tdst);
            }

            if (m_options.AllowPairRenamedTables)
            {
                // snazime se tabulky sparovat na zaklade stejnych jmen VSECH sloupcu
                foreach (TableStructure tsrc in m_src.Tables)
                {
                    if (IsPaired(tsrc))
                    {
                        continue;
                    }
                    foreach (TableStructure tdst in m_dst.Tables)
                    {
                        if (DbDiffTool.EqualColumnNames(tsrc, tdst) && !IsPaired(tdst))
                        {
                            PairObjects(tsrc, tdst);
                            break;
                        }
                    }
                }
            }

            foreach (TableStructure tsrc in m_src.Tables)
            {
                TableStructure tdst = FindPair(tsrc);
                if (tdst != null)
                {
                    PairTableContent(tsrc, tdst);
                }
            }
        }
Ejemplo n.º 7
0
 private void PairSpecificObjects()
 {
     foreach (SpecificObjectStructure osrc in m_src.GetAllSpecificObjects())
     {
         if (IsPaired(osrc))
         {
             continue;
         }
         foreach (SpecificObjectStructure odst in m_dst.GetAllSpecificObjects())
         {
             if (odst.ObjectType == osrc.ObjectType && DbDiffTool.EqualFullNames(osrc.ObjectName, odst.ObjectName, m_options))
             {
                 if (!IsPaired(odst))
                 {
                     PairObjects(osrc, odst);
                 }
             }
         }
     }
 }
Ejemplo n.º 8
0
 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);
                 }
             }
         }
     }
 }
Ejemplo n.º 9
0
        public override bool Save()
        {
            var dialect = m_conn.Dialect ?? GenericDialect.Instance;
            var plan    = new AlterPlan();
            var opts    = new DbDiffOptions();
            var log     = new CachingLogger(LogLevel.Info);

            opts.AlterLogger = log;
            DbDiffTool.AlterDatabase(plan, new DbObjectPairing(m_origDb, m_db), opts);
            string alterSql = dialect.GenerateScript(dmp => plan.CreateRunner().Run(dmp, opts));

            if (!SqlConfirmForm.Run(alterSql, dialect, log))
            {
                return(false);
            }
            m_conn.AlterDatabase(plan, opts);
            objectGridView1.Modified = false;
            UpdateState();
            LoadStructure();
            return(true);
        }
Ejemplo n.º 10
0
 public override void ChangeColumn(IColumnStructure oldcol, IColumnStructure newcol, IEnumerable <IConstraint> constraints)
 {
     if (oldcol.DefaultValue != null)
     {
         PutCmd("^alter ^table %f ^alter ^column %i ^drop ^default", newcol.Table, newcol.ColumnName);
     }
     if (oldcol.ColumnName != newcol.ColumnName)
     {
         RenameColumn(oldcol, newcol.ColumnName);
     }
     // change data type
     if (!DbDiffTool.EqualTypes(oldcol.DataType, newcol.DataType, new DbDiffOptions()))
     {
         Put("^alter ^table %f ^alter ^column %i ", newcol.Table, newcol.ColumnName);
         // remove autoincrement flag
         ColumnStructure newcol2 = new ColumnStructure(newcol);
         newcol2.SetDummyTable(newcol.Table.FullName);
         newcol2.DataType.SetAutoincrement(false);
         ColumnDefinition(newcol2, false, false, false);
         EndCommand();
     }
     if (oldcol.IsNullable != newcol.IsNullable)
     {
         PutCmd("^alter ^table %f ^alter ^column %i ^set %k ^null", oldcol.Table, newcol.ColumnName, newcol.IsNullable ? "" : "not");
     }
     CreateDefault(newcol);
     if (oldcol.DataType.IsAutoIncrement() != newcol.DataType.IsAutoIncrement())
     {
         if (oldcol.DataType.IsAutoIncrement())
         {
             PutCmd("^alter ^table %f ^alter ^column %i ^drop ^generated", newcol.Table.FullName, newcol.ColumnName);
         }
         if (newcol.DataType.IsAutoIncrement())
         {
             PutCmd("^alter ^table %f ^alter ^column %i %s ^generated ^always ^as ^identity (^start ^with %s)",
                    newcol.Table.FullName, newcol.ColumnName, m_dialect.GenericTypeToSpecific(newcol.DataType), 1);
         }
     }
     this.CreateConstraints(constraints);
 }
Ejemplo n.º 11
0
        private void CreateActions()
        {
            m_plan           = new AlterPlan();
            m_plan.Structure = m_dst; // optimalization ... plan fill not download extra data
            DbDiffTool.AlterDatabase(Plan, new DbObjectPairing(m_dst, m_src), m_options);
            DbDiffAction lastAlterTable = null;

            foreach (var op in m_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();
                        m_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();
                    m_actions.Elements.Add(act);
                }
            }
            //this.AlterDatabase(m_dst, m_src, m_options);
        }