Exemple #1
0
 public Alteration(Table masterTable, Table targetTable, Ddl.Dialect dialect)
 {
     this.masterTable = masterTable;
     this.targetTable = targetTable;
     this.dialect = dialect;
 }
        private void AlterPrimaryKey(Alteration alteration, Table masterTable, Table targetTable, Ddl.Dialect targetDialect, OdbcConnection targetConnection)
        {
            OdbcCommand alter = new OdbcCommand();
            alter.CommandType = CommandType.Text;
            alter.Connection = target.connection;

            if (alteration.ddlPrimaryKeyDrop != "")
            {
                try
                {
                    if (config.type == 4)
                    {
                        log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                        log.log(Logger.LogLevel.ddlChange, alteration.ddlPrimaryKeyDrop + ";");
                        log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                    }
                    else
                    {
                        if (config.ddlLogging >= Configuration.DdlLogging.changes)
                        {
                            log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                            log.log(Logger.LogLevel.ddlChange, alteration.ddlPrimaryKeyDrop + ";");
                            log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                        }

                        alter.CommandText = alteration.ddlPrimaryKeyDrop;
                        alter.CommandTimeout = 0;
                        alter.ExecuteNonQuery();
                        log.log(Logger.LogLevel.change, "Primary key constraint " + targetTable.primaryKey.constraintName + " dropped from table " + targetTable.name + " successfully.");
                    }
                }
                catch (Exception ex)
                {
                    log.log(Logger.LogLevel.error, "Exception occurred while trying to drop primary key constraint " + masterTable.primaryKey.constraintName + " from table " + targetTable.name + ".");
                    log.log(Logger.LogLevel.error, ex.Message);
                }
            }

            if (alteration.ddlPrimaryKeyAdd != "")
            {
                try
                {
                    if (config.type == 4)
                    {
                        log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                        log.log(Logger.LogLevel.ddlChange, alteration.ddlPrimaryKeyAdd + ";");
                        log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                    }
                    else
                    {
                        if (config.ddlLogging >= Configuration.DdlLogging.changes)
                        {
                            log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                            log.log(Logger.LogLevel.ddlChange, alteration.ddlPrimaryKeyAdd + ";");
                            log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                        }

                        alter.CommandText = alteration.ddlPrimaryKeyAdd;
                        alter.CommandTimeout = 0;
                        //Database["DB2"].ExecuteNonQuery("call SYSPROC.ADMIN_CMD ('REORG TABLE MyTable')");
                        if (targetDialect != Ddl.Dialect.sqlServer)
                        {
                            alter.CommandText = "CALL SYSPROC.ADMIN_CMD ('REORG TABLE " + targetTable.name + "'); " + alter.CommandText;
                        }
                        alter.ExecuteNonQuery();
                        log.log(Logger.LogLevel.change, "Primary key constraint " + masterTable.primaryKey.constraintName + " added to table " + targetTable.name + " successfully.");
                    }
                }
                catch (Exception ex)
                {
                    log.log(Logger.LogLevel.error, "Exception occurred while trying to add primary key constraint " + masterTable.primaryKey.constraintName + " from table " + targetTable.name + ".");
                    log.log(Logger.LogLevel.error, ex.Message);
                }
            }
        }
        private bool AlterSimple(Alteration alteration, Table masterTable, Table targetTable, Ddl.Dialect targetDialect, OdbcConnection targetConnection)
        {
            bool success = false;

            string alterDdl = alteration.ddl;

            if (alterDdl == "")
            {
                AlterPrimaryKey(alteration, masterTable, targetTable, targetDialect, targetConnection);
                success = true;
            }
            else
            {
                try
                {
                    if (config.type == 4)
                    {
                        log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                        log.log(Logger.LogLevel.ddlChange, alteration.ddl + ";");
                        log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                    }
                    else
                    {
                        if (config.ddlLogging >= Configuration.DdlLogging.changes)
                        {
                            log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                            log.log(Logger.LogLevel.ddlChange, alteration.ddl + ";");
                            log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                        }

                        OdbcCommand alter = new OdbcCommand(alteration.ddl, target.connection);
                        alter.CommandTimeout = 0;
                        alter.ExecuteNonQuery();
                        log.log(Logger.LogLevel.change, "Table " + targetTable.name + " altered successfully.");
                        AlterPrimaryKey(alteration, masterTable, targetTable, targetDialect, targetConnection);
                        success = true;
                    }
                }
                catch (Exception ex)
                {
                    log.log(Logger.LogLevel.warning, "Exception occurred while trying to perform simple alteration of table " + targetTable.name + ".");
                    log.log(Logger.LogLevel.warning, ex.Message);
                }
            }
            return success;
        }
        private void AlterForeignKey(Alteration alteration, Table masterTable, Table targetTable, Ddl.Dialect targetDialect, OdbcConnection targetConnection)
        {
            OdbcCommand alter = new OdbcCommand();
            alter.CommandType = CommandType.Text;
            alter.Connection = target.connection;

            if (alteration.ddlForeignKeyDrop != "")
            {
                try
                {
                    alter.CommandText = alteration.ddlForeignKeyDrop;
                    alter.CommandTimeout = 0;
                    alter.ExecuteNonQuery();
                    for (int count = 0; count < targetTable.foreignKey.columnNames.Count; count = count + 4)
                    {
                        log.log(Logger.LogLevel.change, "Foreign key constraint " + targetTable.foreignKey.columnNames[count] + " dropped from table " + targetTable.name + " successfully.");
                    }

                }
                catch (Exception ex)
                {
                    log.log(Logger.LogLevel.error, "Exception occurred while trying to drop foreign key constraint " + masterTable.foreignKey.columnNames[0] + " from table " + targetTable.name + ".");
                    log.log(Logger.LogLevel.error, ex.Message);
                }
            }

            if (alteration.ddlForeignKeyAdd != "")
            {
                try
                {
                    alter.CommandText = alteration.ddlForeignKeyAdd;
                    alter.CommandTimeout = 0;
                    alter.ExecuteNonQuery();
                    log.log(Logger.LogLevel.change, "Foreign key constraint " + masterTable.foreignKey.columnNames[0] + " added to table " + targetTable.name + " successfully.");
                }
                catch (Exception ex)
                {
                    log.log(Logger.LogLevel.error, "Exception occurred while trying to add foreign key constraint " + masterTable.primaryKey.columnNames[0] + " from table " + targetTable.name + ".");
                    log.log(Logger.LogLevel.error, ex.Message);
                }
            }
        }
        private bool AlterEasy(Alteration alteration, Table masterTable, Table targetTable, Ddl.Dialect targetDialect, OdbcConnection targetConnection)
        {
            bool canDo = true;
            bool success = false;

            string alterDdl = "ALTER TABLE " + targetTable.name + "\r\n";
            string nochangeDdl = alterDdl;

            foreach (Column col in alteration.dropColumns.Values)
                alterDdl += "    DROP COLUMN " + col.name + "\r\n";

            foreach (Column col in alteration.addColumns.Values)
            {
                if (targetDialect == Ddl.Dialect.db2)
                {
                    alterDdl += "    ADD COLUMN " + Ddl.columnDefinition(col, targetDialect) + "\r\n";
                }
                else
                {
                    alterDdl += "    ADD " + Ddl.columnDefinition(col, targetDialect) + "\r\n";
                }
            }

            foreach (ColumnAlterCandidate candidate in alteration.alterColumns)
            {
                if (candidate.masterColumn.typeName.ToUpper() != candidate.targetColumn.typeName.ToUpper())
                {
                    canDo = false;
                    log.log(Logger.LogLevel.warning, "Complex alteration required because " + candidate.targetColumn.name + "'s type was changed.");
                    break;
                }

                if (candidate.masterColumn.columnSize != candidate.targetColumn.columnSize)
                {
                    if (candidate.masterColumn.typeName.ToUpper() != "VARCHAR")
                    {
                        canDo = false;
                        log.log(Logger.LogLevel.warning, "Complex alteration required because " + candidate.targetColumn.name + "'s length was changed whose type was not VARCHAR.");
                        break;
                    }

                    if (candidate.masterColumn.columnSize > candidate.targetColumn.columnSize)
                    {
                        canDo = false;
                        log.log(Logger.LogLevel.warning, "Complex alteration required because " + candidate.targetColumn.name + "'s length was changed to less than its current length.");
                        break;
                    }

                    if (canDo)
                    {
                        alterDdl += "    ALTER COLUMN " + candidate.targetColumn.name + " SET DATA TYPE VARCHAR(" + candidate.masterColumn.columnSize + ")\r\n";
                    }
                }

                if (candidate.masterColumn.decimalDigits != candidate.targetColumn.decimalDigits)
                {
                    canDo = false;
                    log.log(Logger.LogLevel.warning, "Complex alteration required because " + candidate.targetColumn.name + "'s decimal digits changed.");
                    break;
                }

                if (candidate.masterColumn.nullable != candidate.targetColumn.nullable)
                {
                    if (candidate.targetColumn.nullable)
                    {
                        canDo = false;
                        log.log(Logger.LogLevel.warning, "Complex alteration required because " + candidate.targetColumn.name + " that was defined as NOT NULL must be changed to NULL.");
                        break;
                    }

                    if (canDo)
                    {
                        //alterDdl += "    ALTER COLUMN " + candidate.targetColumn.name + " SET NOT NULL\r\n";
                        alterDdl += "    ALTER COLUMN " + candidate.targetColumn.name + " DROP NOT NULL\r\n";
                    }
                }

                if (candidate.masterColumn.defaultValue != candidate.targetColumn.defaultValue)
                {
                    // Alter the default value here!
                    if (canDo)
                    {
                        if (candidate.masterColumn.defaultValue == null)
                            alterDdl += "    ALTER COLUMN " + candidate.targetColumn.name + " DROP DEFAULT " + "\r\n";
                        else
                            alterDdl += "    ALTER COLUMN " + candidate.targetColumn.name + " SET DEFAULT " + candidate.masterColumn.defaultValue + "\r\n";
                    }
                }
            }

            if (canDo)
            {
                if (alterDdl == nochangeDdl)
                {
                    AlterPrimaryKey(alteration, masterTable, targetTable, targetDialect, targetConnection);
                    success = true;
                }
                else
                {
                    try
                    {
                        if (config.type == 4)
                        {
                            log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                            log.log(Logger.LogLevel.ddlChange, alterDdl + ";");
                            log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                        }
                        else
                        {
                            if (config.ddlLogging >= Configuration.DdlLogging.changes)
                            {
                                log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                                log.log(Logger.LogLevel.ddlChange, alterDdl + ";");
                                log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                            }

                            OdbcCommand alter = new OdbcCommand(alterDdl, target.connection);
                            alter.CommandTimeout = 0;
                            alter.ExecuteNonQuery();
                            log.log(Logger.LogLevel.change, "Table " + targetTable.name + " altered successfully.");
                            AlterPrimaryKey(alteration, masterTable, targetTable, targetDialect, targetConnection);
                            success = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.log(Logger.LogLevel.warning, "Exception occurred while trying to alter table " + targetTable.name + ".");
                        log.log(Logger.LogLevel.warning, ex.Message);
                        log.log(Logger.LogLevel.warning, "Complex alteration will be attempted for table " + targetTable.name + ".");
                    }
                }
            }

            if (canDo)
            {
                if (alterDdl == nochangeDdl)
                {
                    //AlterForeignKey(alteration, masterTable, targetTable, targetDialect, targetConnection);
                    //success = true;
                }
                else
                {
                    try
                    {
                        if (config.type == 4)
                        {
                            log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                            log.log(Logger.LogLevel.ddlChange, alterDdl + ";");
                            log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                        }
                        else
                        {
                            if (config.ddlLogging >= Configuration.DdlLogging.changes)
                            {
                                log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                                log.log(Logger.LogLevel.ddlChange, alterDdl + ";");
                                log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                            }

                            OdbcCommand alter = new OdbcCommand(alterDdl, target.connection);
                            alter.CommandTimeout = 0;
                            alter.ExecuteNonQuery();
                            log.log(Logger.LogLevel.change, "Table " + targetTable.name + " altered successfully.");
                            //AlterForeignKey(alteration, masterTable, targetTable, targetDialect, targetConnection);
                            //success = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.log(Logger.LogLevel.warning, "Exception occurred while trying to alter table " + targetTable.name + ".");
                        log.log(Logger.LogLevel.warning, ex.Message);
                        log.log(Logger.LogLevel.warning, "Complex alteration will be attempted for table " + targetTable.name + ".");
                    }
                }
            }
            return success;
        }
        private void DropAndCreate(Table masterTable, Table targetTable, Ddl.Dialect targetDialect, OdbcConnection targetConnection)
        {
            log.log(Logger.LogLevel.change, "Dropping and recreating table " + targetTable.name + ".");

            try
            {
                if (config.type == 4)
                {
                    log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                    log.log(Logger.LogLevel.ddlChange, "drop table " + targetTable.name + ";");
                    log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                }
                else
                {
                    if (config.ddlLogging >= Configuration.DdlLogging.changes)
                    {
                        log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                        log.log(Logger.LogLevel.ddlChange, "drop table " + targetTable.name + ";");
                        log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                    }

                    OdbcCommand drop = new OdbcCommand();
                    drop.Connection = targetConnection;
                    drop.CommandType = CommandType.Text;
                    drop.CommandText = "drop table " + targetTable.name;
                    drop.CommandTimeout = 0;
                    drop.ExecuteNonQuery();
                    log.log(Logger.LogLevel.change, "Table " + targetTable.name + " dropped successfully.");
                }
            }
            catch (Exception ex)
            {
                log.log(Logger.LogLevel.error, "Exception occurred while trying to drop table " + targetTable.name + ".");
                log.log(Logger.LogLevel.error, ex.Message);
            }

            try
            {
                //TableSapce Change
                if (targetDialect == Ddl.Dialect.db2)
                {
                    findTableSpaceSQL(masterTable.name);
                }
                //TableSapce Change

                if (config.type == 4)
                {
                    log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                    log.log(Logger.LogLevel.ddlChange, (Ddl.ddl(masterTable, Ddl.Dialect.generic) + TableSpaceSQL + ";").Replace("DATETIME", "TIMESTAMP"));
                    log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                }
                else
                {
                    if (config.ddlLogging >= Configuration.DdlLogging.changes)
                    {
                        log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                        log.log(Logger.LogLevel.ddlChange, (Ddl.ddl(masterTable, Ddl.Dialect.generic) + TableSpaceSQL + ";").Replace("DATETIME", "TIMESTAMP"));
                        log.log(Logger.LogLevel.ddl, string.Concat(System.Collections.ArrayList.Repeat('-', 75).ToArray()));
                    }

                    //OdbcCommand create = new OdbcCommand(Ddl.ddl(masterTable, targetDialect), targetConnection);
                    OdbcCommand create = new OdbcCommand();
                    if (targetDialect == Ddl.Dialect.sqlServer)
                        create = new OdbcCommand(Ddl.ddl(masterTable, targetDialect).Replace("getdate", "getdate()"), targetConnection);
                    else
                        create = new OdbcCommand(Ddl.ddl(masterTable, targetDialect), targetConnection);

                    create.CommandTimeout = 0;
                    create.CommandText += TableSpaceSQL;
                    create.ExecuteNonQuery();
                    log.log(Logger.LogLevel.change, "Table " + targetTable.name + " created successfully.");
                }
            }
            catch (Exception ex)
            {
                log.log(Logger.LogLevel.error, "Exception occurred while trying to create table " + masterTable.name + ".");
                log.log(Logger.LogLevel.error, ex.Message);
            }
        }
        private void AlterComplex(Alteration alteration, Table masterTable, Table targetTable, Ddl.Dialect targetDialect, OdbcConnection targetConnection)
        {
            string PrimaryColumnName = "";
            if (config.type != 4)
            {
                bool failureDetected = false;
                // Make sure that the META_DB_TRANSFER_TEMP is deleted
                OdbcCommand command = new OdbcCommand("DROP TABLE META_DB_DEPLOY_TRANSFER_TEMP", target.connection);
                try
                {
                    command.CommandTimeout = 0;
                    command.ExecuteNonQuery();
                }
                catch {/*Do Nothing*/}

                // Drop the Primary key constraint in target table to create the Meta Temp table in SQL server database 20110811
                if (targetDialect == Ddl.Dialect.sqlServer)
                {
                    PrimaryColumnName = findPrimaryKeyColumnNameSqlServer(targetTable);
                    if (PrimaryColumnName != "")
                    {
                        command.CommandText = " ALTER TABLE " + targetTable.name + " DROP CONSTRAINT " + PrimaryColumnName;
                        command.CommandTimeout = 0;
                        command.ExecuteNonQuery();
                    }
                }

                // CREATE the META_DB_TRANSFER_TEMP table with the new target schema
                try
                {
                    command.CommandText = Ddl.ddl(masterTable, targetDialect).Replace("CREATE TABLE " + masterTable.name, "CREATE TABLE META_DB_DEPLOY_TRANSFER_TEMP");
                    if (targetDialect == Ddl.Dialect.sqlServer)
                    {
                        command.CommandText = command.CommandText.Replace("getdate", "getdate()");
                    }
                    //TableSapce Change
                    if (targetDialect == Ddl.Dialect.db2)
                    {
                        findTableSpaceSQL(masterTable.name);
                    }
                    //TableSapce Change

                    command.CommandTimeout = 0;
                    command.CommandText += TableSpaceSQL;
                    command.ExecuteNonQuery();
                    log.log(Logger.LogLevel.progress, "Transfer temporary table for complex alteration of " + masterTable.name + " created.");
                }
                catch (Exception ex)
                {
                    failureDetected = true;
                    log.log(Logger.LogLevel.error, "Exception occurred while trying to perform complex alteration of table " + masterTable.name + ".");
                    log.log(Logger.LogLevel.error, "CREATE TABLE META_DB_DEPLOY_TRANSFER_TEMP failed.");
                    log.log(Logger.LogLevel.error, ex.Message);
                }

                if (!failureDetected)
                {
                    try
                    {
                        command.CommandText = Ddl.ddl(alteration, "META_DB_DEPLOY_TRANSFER_TEMP");
                        if (targetDialect == Ddl.Dialect.sqlServer)
                        {
                            if (PrimaryColumnName != "")
                            {
                                command.CommandText = "SET IDENTITY_INSERT META_DB_DEPLOY_TRANSFER_TEMP ON;" + command.CommandText + "; SET IDENTITY_INSERT META_DB_DEPLOY_TRANSFER_TEMP OFF";
                            }
                            else
                            {
                                command.CommandText = command.CommandText + ";";
                            }
                            //command.CommandText = "SET IDENTITY_INSERT META_DB_DEPLOY_TRANSFER_TEMP ON;" + command.CommandText + "; SET IDENTITY_INSERT META_DB_DEPLOY_TRANSFER_TEMP OFF";
                        }

                        if (config.ddlLogging >= Configuration.DdlLogging.changes)
                            log.log(Logger.LogLevel.ddlChange, command.CommandText);

                        command.CommandTimeout = 0;
                        int insertCount = command.ExecuteNonQuery();
                        log.log(Logger.LogLevel.progress, insertCount + " rows were successfully transfered to temp table from table " + masterTable.name + ".");
                    }
                    catch (Exception ex)
                    {
                        failureDetected = true;
                        log.log(Logger.LogLevel.error, "Exception occurred while trying to perform complex alteration of table " + masterTable.name + ".");
                        log.log(Logger.LogLevel.error, "Transfer of data to tempory table failed.");
                        log.log(Logger.LogLevel.error, ex.Message);
                    }
                }

                if (!failureDetected)
                {
                    try
                    {
                        command.CommandText = "DROP TABLE " + targetTable.name;
                        command.CommandTimeout = 0;
                        command.ExecuteNonQuery();
                        log.log(Logger.LogLevel.progress, "Successfully dropped " + targetTable.name + ".");
                    }
                    catch (Exception ex)
                    {
                        failureDetected = true;
                        log.log(Logger.LogLevel.error, "Exception occurred while trying to perform complex alteration of table " + masterTable.name + ".");
                        log.log(Logger.LogLevel.error, "Drop of original table " + targetTable.name + " failed.");
                        log.log(Logger.LogLevel.error, ex.Message);
                    }
                }

                if (!failureDetected)
                {
                    try
                    {
                        if (targetDialect == Ddl.Dialect.db2)
                            command.CommandText = "RENAME TABLE META_DB_DEPLOY_TRANSFER_TEMP TO " + targetTable.name;
                        else if (targetDialect == Ddl.Dialect.sqlServer)
                            command.CommandText = "SP_RENAME 'META_DB_DEPLOY_TRANSFER_TEMP','" + targetTable.name + "'";

                        command.CommandTimeout = 0;
                        command.ExecuteNonQuery();
                        log.log(Logger.LogLevel.progress, "Successfully renamed temp table to " + targetTable.name + ".");
                    }
                    catch (Exception ex)
                    {
                        failureDetected = true;
                        log.log(Logger.LogLevel.error, "Exception occurred while trying to perform complex alteration of table " + masterTable.name + ".");
                        log.log(Logger.LogLevel.error, "RENAMING OF TEMP TABLE (META_DB_DEPLY_TRANSFER_TEMP) TO " + targetTable.name + " FAILED.");
                        log.log(Logger.LogLevel.error, "FAILURE TO FIX THE PROBLEM MANUALLY PRIOR TO THE NEXT RUN OF DB DEPLOY WILL RESULT IN ALL DATA THAT WAS IN " + targetTable.name + "!");
                        log.log(Logger.LogLevel.error, ex.Message);
                    }
                }

                if (!failureDetected)
                {
                    log.log(Logger.LogLevel.progress, "Complex alteration of " + targetTable.name + " completed successfully.");
                }
            }
        }
        private void Alter(Table masterTable, Table targetTable, Ddl.Dialect targetDialect, OdbcConnection targetConnection)
        {
            //TODO: 1. Check for appropriate return value here.
            Alteration alteration = Ddl.alter(masterTable, targetTable, targetDialect);

            // Check for simple alteration
            if (alteration.alterColumns.Count == 0 &&
                ((alteration.addColumns.Count > 0 && alteration.dropColumns.Count == 0) ||
                (alteration.addColumns.Count == 0 && alteration.dropColumns.Count > 0))
            )
            {
                if (!AlterSimple(alteration, masterTable, targetTable, targetDialect, targetConnection))
                {
                    AlterComplex(alteration, masterTable, targetTable, targetDialect, targetConnection);
                }
            }
            else
            {
                // Complex alteration
                // Are we moving data from the source?
                //if (ProcessData(masterTable.name))
                if (ProcessData(masterTable.name) && (!config.filterTables.ContainsKey(masterTable.name)))
                {
                    // Yes - drop the current table and recreate it, data will be copied over later via standard process
                    DropAndCreate(masterTable, targetTable, targetDialect, targetConnection);
                }
                else
                {
                    // No - need to copy the data in the current table to the newly created one...
                    // See if we need to get a mapping from the user...
                    int mappedCount = 0;
                    if (alteration.addColumns.Count > 0 && alteration.dropColumns.Count > 0)
                    {
                        MappingForm form = new MappingForm();

                        form.tableNameText.Text = masterTable.name;

                        foreach (Column column in alteration.dropColumns.Values)
                            form.oldColumnList.Items.Add(column);

                        foreach (Column column in alteration.addColumns.Values)
                            form.newColumnList.Items.Add(column);

                        if (form.ShowDialog() != DialogResult.OK)
                        {
                            log.log(Logger.LogLevel.error, "User cancelled out of a required mapping.  Alteration of table " + targetTable.name + " skipped.");
                            return;
                        }
                        mappedCount = form.mappedColumnList.Items.Count;

                        ////Update the alteration structure from the mapping form editor
                        //alteration.dropColumns.Clear();
                        //foreach (Column column in form.oldColumnList.Items)
                        //    alteration.dropColumns.Add(column.name, column);

                        //alteration.addColumns.Clear();
                        //foreach (Column column in form.newColumnList.Items)
                        //    alteration.addColumns.Add(column.name, column);

                        alteration.mappings.Clear();
                        foreach (Mapping mapping in form.mappedColumnList.Items)
                            alteration.mappings.Add(mapping);
                    }

                    if (mappedCount == 0 && alteration.alterColumns.Count == 0)
                    {
                        if (Ddl.ddlTemp(masterTable, Ddl.Dialect.generic) == Ddl.ddlTemp(targetTable, Ddl.Dialect.generic))
                        {
                            if (!AlterSimple(alteration, masterTable, targetTable, targetDialect, targetConnection))
                            {
                                AlterComplex(alteration, masterTable, targetTable, targetDialect, targetConnection);
                            }
                        }
                        else
                        {
                            AlterComplex(alteration, masterTable, targetTable, targetDialect, targetConnection);
                        }
                    }
                    else
                    {
                        if (targetDialect == Ddl.Dialect.sqlServer)
                        {
                            if (Ddl.ddlTemp(masterTable, Ddl.Dialect.generic) == Ddl.ddlTemp(targetTable, Ddl.Dialect.generic))
                            {
                                if (!AlterSimple(alteration, masterTable, targetTable, targetDialect, targetConnection))
                                {
                                    AlterComplex(alteration, masterTable, targetTable, targetDialect, targetConnection);
                                }
                            }
                            else
                            {
                                AlterComplex(alteration, masterTable, targetTable, targetDialect, targetConnection);
                            }
                        }

                        //else if (mappedCount != 0 && alteration.mappings.Count != 0)
                        //{
                        //    if (!AlterEasy(alteration, masterTable, targetTable, targetDialect, targetConnection))
                        //    {
                        //        AlterComplex(alteration, masterTable, targetTable, targetDialect, targetConnection);
                        //    }
                        //}

                        else if (mappedCount >= 0)
                        {
                            if (Ddl.ddlTemp(masterTable, Ddl.Dialect.generic) == Ddl.ddlTemp(targetTable, Ddl.Dialect.generic))
                            {
                                if (!AlterEasy(alteration, masterTable, targetTable, targetDialect, targetConnection))
                                {
                                    AlterComplex(alteration, masterTable, targetTable, targetDialect, targetConnection);
                                }
                            }
                            else
                            {
                                AlterComplex(alteration, masterTable, targetTable, targetDialect, targetConnection);
                            }
                        }
                    }
                }
            }
        }