Ejemplo n.º 1
0
        public void when_there_are_no_migration_scripts_then_none_are_ever_executed()
        {
            _mockMetadataProvider.Setup(m => m.SchemaExists(It.IsAny<string>())).Returns(false);

            var migration = new TermMigration(_mockMetadataProvider.Object, false);
            migration.Migrate(Enumerable.Empty<IStep>(), null, null, _mockScriptRunner.Object, "someSchema", "someTablespace");

            _mockScriptRunner.Verify(m => m.Execute(It.IsAny<IScript>()), Times.Never);
        }
Ejemplo n.º 2
0
        private static void RunMigration(MigrationInfo migrationInfo, DatabaseVersion currentVersion, IEnumerable<Script> initScripts, IEnumerable<Script> upScripts, IEnumerable<Script> downScripts, IEnumerable<Script> termScripts,
            IList<IStep> storedSteps, IScriptRunner scriptRunner, IDatabaseMetadataProvider databaseMetadataProvider, IVersionRespository versionRespository, IAppliedScriptsRepository appliedScriptsRepository)
        {
            var upDownSteps = upScripts.Select(u => new DatabaseStep(u, downScripts.FirstOrDefault(d => d.Version == u.Version))).ToList();
            var initTermSteps = initScripts.Select(i => new DatabaseStep(i, termScripts.FirstOrDefault(t => t.Version == i.Version))).ToList();

            if (migrationInfo.Direction == MigrationDirection.Down)
            {
                var downMigration = new DownMigration(databaseMetadataProvider, versionRespository, appliedScriptsRepository, migrationInfo.Forced);

                if (storedSteps.Any())
                {
                    Output.Warn("NOTE: Using stored applied scripts to perform downgrade instead of local 'down' scripts.");
                    downMigration.Migrate(storedSteps, currentVersion, migrationInfo.TargetVersion, scriptRunner, migrationInfo.TargetSchema, migrationInfo.TargetTablespace);
                }
                else
                {
                    downMigration.Migrate(upDownSteps, currentVersion, migrationInfo.TargetVersion, scriptRunner, migrationInfo.TargetSchema, migrationInfo.TargetTablespace);
                }

                if (!migrationInfo.TargetVersion.HasValue)
                {
                    var termMigration = new TermMigration(databaseMetadataProvider, migrationInfo.Forced);
                    termMigration.Migrate(initTermSteps, currentVersion, migrationInfo.TargetVersion, scriptRunner, migrationInfo.TargetSchema, migrationInfo.TargetTablespace);
                }
                else
                {
                    Output.Info("A target version was provided, termination scripts will not be executed.");
                }
            }
            else
            {
                var initMigration = new InitMigration(databaseMetadataProvider, migrationInfo.Forced);
                initMigration.Migrate(initTermSteps, currentVersion, migrationInfo.TargetVersion, scriptRunner, migrationInfo.TargetSchema, migrationInfo.TargetTablespace);

                EnsureVersioningTableIsInitialised(versionRespository, migrationInfo.TargetSchema, migrationInfo.TargetTablespace);
                EnsureAppliedScriptsTableIsInitialised(appliedScriptsRepository, migrationInfo.TargetSchema, migrationInfo.TargetTablespace);

                var upMigration = new UpMigration(versionRespository, appliedScriptsRepository, migrationInfo.Forced);
                upMigration.Migrate(upDownSteps, currentVersion, migrationInfo.TargetVersion, scriptRunner, migrationInfo.TargetSchema, migrationInfo.TargetTablespace);
            }
        }