Inheritance: IMigrator, IRestorePoint
        public void DetermineOperation()
        {
            if (migratorName == "")
            {
                return;
            }
            executed = false;
            Version currentVersion = genericData.GetUniverseVersion(migratorName);

            //if there is no Universe version, this is likely an entirely new installation
            if (currentVersion == null)
            {
                MainConsole.Instance.InfoFormat("[Migrator]: Clean installation for {0} found", migratorName);
                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();
                    MainConsole.Instance.InfoFormat("[Migrator]: New migration script for {0} found", migratorName);
                    MainConsole.Instance.CleanInfoFormat("            Depending upon the amount of {0} data, this process may take quite a while", migratorName);
                    MainConsole.Instance.CleanInfo("             You may also see warnings and possible timeout errors until this proccess is completed");
                    MainConsole.Instance.CleanInfo("");

                    operationDescription = new MigrationOperationDescription(MigrationOperationTypes.UpgradeToTarget,
                                                                             currentVersion, startMigrator.Version,
                                                                             targetMigrator.Version);
                }
                else
                {
                    operationDescription = new MigrationOperationDescription(MigrationOperationTypes.DoNothing,
                                                                             currentVersion);
                }
            }
        }
Esempio n. 2
0
        public void DetermineOperation()
        {
            if (migratorName == "")
            {
                return;
            }
            executed = false;
            Version currentVersion = genericData.GetUniverseVersion(migratorName);

            //if there is no Universe 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);
                }
            }
        }
        public void ExecuteOperation()
        {
            if (migratorName == "")
            {
                return;
            }

            if (operationDescription != null && !executed &&
                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("[Migrator]: 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(
                                                       "[Migrator]: 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(
                                                                      "[Migrator]: 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("[Migrator]: 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("[Migrator]: 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);
                }
            }
        }