Пример #1
0
        public void Migrate(IEnumerable <IStep> migrationSteps, DatabaseVersion currentVersion, long?targetVersion, IScriptRunner scriptRunner, string schema, string tablespace)
        {
            var steps = migrationSteps as IList <IStep> ?? migrationSteps.ToList();

            var forwardScripts  = steps.Select(m => m.ForwardScript).ToList();
            var backwardScripts = steps.Select(m => m.BackwardScript).ToList();

            var applicableScripts = forwardScripts.Where(s => currentVersion.IsNull() || s.Version > currentVersion.Version)
                                    .Where(s => !targetVersion.HasValue || s.Version <= targetVersion).ToArray();

            if (applicableScripts.IsNullOrEmpty())
            {
                Output.Warn("No migration scripts need to be run. Nothing will be done.");
            }
            else
            {
                Output.Info("The following scripts will be applied to the database:");
                foreach (var script in applicableScripts)
                {
                    Output.Info(script.Name, 1);
                }

                Output.EmptyLine();
                Prompt.ForAnyKey("Press any key to start the 'up' migration. Ctrl-C to abort.");

                foreach (var script in applicableScripts)
                {
                    Output.Info("Executing script: {0}".FormatWith(script.Path));

                    try
                    {
                        scriptRunner.Execute(script);
                    }
                    catch (SqlPlusException)
                    {
                        if (!_force)
                        {
                            throw;
                        }
                    }

                    Output.Info("Script '{0}' completed.".FormatWith(script.Name));

                    Output.Text("Adding version {0} for script '{1}' to version table".FormatWith(script.Version, script.Name));
                    _versionRespository.InsertVersion(script.AsDatabaseVersion(), schema);

                    Output.Text("Adding script contents for script '{0}' to applied scripts table".FormatWith(script.Name));
                    _appliedScriptsRepository.InsertAppliedScript(script.AsDatabaseVersion(), schema, script, backwardScripts.FirstOrDefault(m => m.IsNotNull() && m.Version == script.Version));
                }
            }
        }
Пример #2
0
        private void DowngradeDatabase(IEnumerable <IScript> migrationScripts, DatabaseVersion currentVersion, long?targetVersionNumber, IScriptRunner scriptRunner, string schema)
        {
            var applicableScripts = migrationScripts.Where(s => currentVersion.IsNull() || s.Version <= currentVersion.Version)
                                    .Where(s => !targetVersionNumber.HasValue || s.Version > targetVersionNumber).ToArray();

            if (applicableScripts.IsNullOrEmpty())
            {
                Output.Warn("No migration scripts need to be run. Nothing will be done.");
            }
            else
            {
                Output.Info("The following scripts will be applied to the database:");
                foreach (var script in applicableScripts)
                {
                    Output.Info(script.Name, 1);
                }

                Output.EmptyLine();
                Prompt.ForAnyKey("Press any key to start the 'down' migration. Ctrl-C to abort.");

                foreach (var script in applicableScripts)
                {
                    Output.Info("Executing script: {0}".FormatWith(script.Path));

                    try
                    {
                        scriptRunner.Execute(script);
                    }
                    catch (SqlPlusException)
                    {
                        if (!_force)
                        {
                            throw;
                        }
                    }

                    Output.Info("Script '{0}' completed.".FormatWith(script.Name));

                    Output.Text("Removing script contents for script '{0}' from applied scripts table".FormatWith(script.Name));
                    _appliedScriptsRepository.RemoveAppliedScript(script.AsDatabaseVersion(), schema);

                    Output.Text("Removing version {0} for script '{1}' from version table".FormatWith(script.Version, script.Name));
                    _versionRespository.RemoveVersion(script.AsDatabaseVersion(), schema);
                }
            }
        }
Пример #3
0
        private void DowngradeDatabase(IEnumerable<IScript> migrationScripts, DatabaseVersion currentVersion, long? targetVersionNumber, IScriptRunner scriptRunner, string schema)
        {
            var applicableScripts = migrationScripts.Where(s => currentVersion.IsNull() || s.Version <= currentVersion.Version)
                .Where(s => !targetVersionNumber.HasValue || s.Version > targetVersionNumber).ToArray();

            if (applicableScripts.IsNullOrEmpty())
            {
                Output.Warn("No migration scripts need to be run. Nothing will be done.");
            }
            else
            {
                Output.Info("The following scripts will be applied to the database:");
                foreach (var script in applicableScripts)
                {
                    Output.Info(script.Name, 1);
                }

                Output.EmptyLine();
                Prompt.ForAnyKey("Press any key to start the 'down' migration. Ctrl-C to abort.");

                foreach (var script in applicableScripts)
                {
                    Output.Info("Executing script: {0}".FormatWith(script.Path));

                    try
                    {
                        scriptRunner.Execute(script);
                    }
                    catch (SqlPlusException)
                    {
                        if (!_force)
                        {
                            throw;
                        }
                    }

                    Output.Info("Script '{0}' completed.".FormatWith(script.Name));

                    Output.Text("Removing script contents for script '{0}' from applied scripts table".FormatWith(script.Name));
                    _appliedScriptsRepository.RemoveAppliedScript(script.AsDatabaseVersion(), schema);

                    Output.Text("Removing version {0} for script '{1}' from version table".FormatWith(script.Version, script.Name));
                    _versionRespository.RemoveVersion(script.AsDatabaseVersion(), schema);
                }
            }
        }