public void DetermineOperation()
        {
            if (migratorName == "")
            {
                return;
            }
            executed = false;
            Version currentVersion = genericData.GetAuroraVersion(migratorName);

            //if there is no aurora version, this is likely an entirely new installation
            if (currentVersion == null)
            {
                Migrator defaultMigrator = GetHighestVersionMigratorThatCanProvideDefaultSetup();
                currentVersion = defaultMigrator.Version;
                Migrator startMigrator  = GetMigratorAfterVersion(defaultMigrator.Version);
                var      latestMigrator = GetLatestVersionMigrator();
                Migrator targetMigrator = defaultMigrator == latestMigrator ? null : latestMigrator;
                operationDescription =
                    new MigrationOperationDescription(MigrationOperationTypes.CreateDefaultAndUpgradeToTarget,
                                                      currentVersion,
                                                      startMigrator != null ? startMigrator.Version : null,
                                                      targetMigrator != null ? targetMigrator.Version : null);
            }
            else
            {
                Migrator startMigrator = GetMigratorAfterVersion(currentVersion);
                if (startMigrator != null)
                {
                    Migrator targetMigrator = GetLatestVersionMigrator();
                    operationDescription = new MigrationOperationDescription(MigrationOperationTypes.UpgradeToTarget,
                                                                             currentVersion, startMigrator.Version,
                                                                             targetMigrator.Version);
                }
                else
                {
                    operationDescription = new MigrationOperationDescription(MigrationOperationTypes.DoNothing,
                                                                             currentVersion);
                }
            }
        }
Example #2
0
        public void ExecuteOperation()
        {
            if (operationDescription != null && executed == false && operationDescription.OperationType != MigrationOperationTypes.DoNothing)
            {
                Migrator currentMigrator = GetMigratorByVersion(operationDescription.CurrentVersion);

                //if we are creating default, do it now
                if (operationDescription.OperationType == MigrationOperationTypes.CreateDefaultAndUpgradeToTarget)
                {
                    try
                    {
                        currentMigrator.CreateDefaults(genericData);
                    }
                    catch
                    {
                    }
                    executed = true;
                }

                if (validateTables)
                {
                    //lets first validate where we think we are
                    bool validated = currentMigrator.Validate(genericData);

                    if (!validated)
                    {
                        throw new MigrationOperationException(string.Format("Current version {0} did not validate. Stopping here so we don't cause any trouble. No changes were made.", currentMigrator.Version));
                    }
                }

                bool restoreTaken = false;
                //Loop through versions from start to end, migrating then validating
                Migrator executingMigrator = GetMigratorByVersion(operationDescription.StartVersion);

                //only restore if we are going to do something
                if (executingMigrator != null)
                {
                    if (validateTables)
                    {
                        //prepare restore point if something goes wrong
                        restorePoint = currentMigrator.PrepareRestorePoint(genericData);
                        restoreTaken = true;
                    }
                }


                while (executingMigrator != null)
                {
                    try
                    {
                        executingMigrator.Migrate(genericData);
                    }
                    catch (Exception)
                    {
                    }
                    executed = true;
                    if (validateTables)
                    {
                        bool validated = executingMigrator.Validate(genericData);

                        //if it doesn't validate, rollback
                        if (!validated)
                        {
                            RollBackOperation();
                            throw new MigrationOperationException(string.Format("Migrating to version {0} did not validate. Restoring to restore point.", currentMigrator.Version));
                        }
                    }

                    if (executingMigrator.Version == operationDescription.EndVersion)
                    {
                        break;
                    }

                    executingMigrator = GetMigratorAfterVersion(executingMigrator.Version);
                }

                if (restoreTaken)
                {
                    currentMigrator.ClearRestorePoint(genericData);
                }
            }
        }
        public void ExecuteOperation()
        {
            if (migratorName == "")
            {
                return;
            }

            if (operationDescription != null && executed == false &&
                operationDescription.OperationType != MigrationOperationTypes.DoNothing)
            {
                Migrator currentMigrator = GetMigratorByVersion(operationDescription.CurrentVersion);

                //if we are creating default, do it now
                if (operationDescription.OperationType == MigrationOperationTypes.CreateDefaultAndUpgradeToTarget)
                {
                    try
                    {
                        currentMigrator.CreateDefaults(genericData);
                    }
                    catch
                    {
                    }
                    executed = true;
                }

                //lets first validate where we think we are
                bool validated = currentMigrator != null && currentMigrator.Validate(genericData);

                if (!validated && validateTables && currentMigrator != null)
                {
                    //Try rerunning the migrator and then the validation
                    //prepare restore point if something goes wrong
                    MainConsole.Instance.Fatal(string.Format("Failed to validate migration {0}-{1}, retrying...",
                                                             currentMigrator.MigrationName, currentMigrator.Version));

                    currentMigrator.Migrate(genericData);
                    validated = currentMigrator.Validate(genericData);
                    if (!validated)
                    {
                        SchemaDefinition rec;
                        currentMigrator.DebugTestThatAllTablesValidate(genericData, out rec);
                        MainConsole.Instance.Fatal(string.Format(
                                                       "FAILED TO REVALIDATE MIGRATION {0}-{1}, FIXING TABLE FORCIBLY... NEW TABLE NAME {2}",
                                                       currentMigrator.MigrationName,
                                                       currentMigrator.Version,
                                                       rec.Name + "_broken"
                                                       ));
                        genericData.RenameTable(rec.Name, rec.Name + "_broken");
                        currentMigrator.Migrate(genericData);
                        validated = currentMigrator.Validate(genericData);
                        if (!validated)
                        {
                            throw new MigrationOperationException(string.Format(
                                                                      "Current version {0}-{1} did not validate. Stopping here so we don't cause any trouble. No changes were made.",
                                                                      currentMigrator.MigrationName,
                                                                      currentMigrator.Version
                                                                      ));
                        }
                    }
                }
                //else
                //    MainConsole.Instance.Fatal (string.Format ("Failed to validate migration {0}-{1}, continueing...", currentMigrator.MigrationName, currentMigrator.Version));


                bool restoreTaken = false;
                //Loop through versions from start to end, migrating then validating
                Migrator executingMigrator = GetMigratorByVersion(operationDescription.StartVersion);

                //only restore if we are going to do something
                if (executingMigrator != null)
                {
                    if (validateTables && currentMigrator != null)
                    {
                        //prepare restore point if something goes wrong
                        restorePoint = currentMigrator.PrepareRestorePoint(genericData);
                        restoreTaken = true;
                    }
                }


                while (executingMigrator != null)
                {
                    try
                    {
                        executingMigrator.Migrate(genericData);
                    }
                    catch (Exception ex)
                    {
                        if (currentMigrator != null)
                        {
                            throw new MigrationOperationException(string.Format("Migrating to version {0} failed, {1}.",
                                                                                currentMigrator.Version, ex));
                        }
                    }
                    executed  = true;
                    validated = executingMigrator.Validate(genericData);

                    //if it doesn't validate, rollback
                    if (!validated && validateTables)
                    {
                        RollBackOperation();
                        if (currentMigrator != null)
                        {
                            throw new MigrationOperationException(
                                      string.Format("Migrating to version {0} did not validate. Restoring to restore point.",
                                                    currentMigrator.Version));
                        }
                    }
                    else
                    {
                        executingMigrator.FinishedMigration(genericData);
                    }

                    if (executingMigrator.Version == operationDescription.EndVersion)
                    {
                        break;
                    }

                    executingMigrator = GetMigratorAfterVersion(executingMigrator.Version);
                }

                if (restoreTaken)
                {
                    currentMigrator.ClearRestorePoint(genericData);
                }
            }
        }