Exemplo n.º 1
0
        public void when_there_are_no_migration_scripts_then_none_are_ever_executed()
        {
            var migration = new UpMigration(_mockVersionRepository.Object, _mockAppliedScriptsRepository.Object, false);

            migration.Migrate(Enumerable.Empty <IStep>(), null, null, _mockScriptRunner.Object, Some.String(), Some.String());

            _mockScriptRunner.Verify(m => m.Execute(It.IsAny <IScript>()), Times.Never);
        }
Exemplo n.º 2
0
        public void for_each_migration_script_upgraded_their_version_is_added()
        {
            var migration = new UpMigration(_mockVersionRepository.Object, _mockAppliedScriptsRepository.Object, false);
            migration.Migrate(SomeScripts, null, null, _mockScriptRunner.Object, Some.String(), Some.String());

            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[0].ForwardScript.AsDatabaseVersion(), It.IsAny<string>()), Times.Once);
            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[1].ForwardScript.AsDatabaseVersion(), It.IsAny<string>()), Times.Once);
            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[2].ForwardScript.AsDatabaseVersion(), It.IsAny<string>()), Times.Once);
        }
Exemplo n.º 3
0
        public void for_each_migration_script_upgraded_their_scripts_are_added()
        {
            var migration = new UpMigration(_mockVersionRepository.Object, _mockAppliedScriptsRepository.Object, false);

            migration.Migrate(SomeScripts, null, null, _mockScriptRunner.Object, Some.String(), Some.String());

            _mockAppliedScriptsRepository.Verify(m => m.InsertAppliedScript(SomeScripts[0].ForwardScript.AsDatabaseVersion(), It.IsAny <string>(), SomeScripts[0].ForwardScript, SomeScripts[0].BackwardScript), Times.Once);
            _mockAppliedScriptsRepository.Verify(m => m.InsertAppliedScript(SomeScripts[1].ForwardScript.AsDatabaseVersion(), It.IsAny <string>(), SomeScripts[1].ForwardScript, SomeScripts[1].BackwardScript), Times.Once);
            _mockAppliedScriptsRepository.Verify(m => m.InsertAppliedScript(SomeScripts[2].ForwardScript.AsDatabaseVersion(), It.IsAny <string>(), SomeScripts[2].ForwardScript, SomeScripts[2].BackwardScript), Times.Once);
        }
Exemplo n.º 4
0
        public void if_force_is_specified_then_the_migration_scripts_will_still_run()
        {
            var migration = new UpMigration(_mockVersionRepository.Object, _mockAppliedScriptsRepository.Object, true);

            _mockScriptRunner.Setup(m => m.Execute(SomeScripts[1].ForwardScript)).Throws(new SqlPlusException());

            Ignore.Exception(() => migration.Migrate(SomeScripts, null, null, _mockScriptRunner.Object, Some.String(), Some.String()));

            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[0].ForwardScript.AsDatabaseVersion(), It.IsAny <string>()), Times.Once);
            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[1].ForwardScript.AsDatabaseVersion(), It.IsAny <string>()), Times.Once);
            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[2].ForwardScript.AsDatabaseVersion(), It.IsAny <string>()), Times.Once);
        }
Exemplo n.º 5
0
        public void if_a_migration_script_fails_then_the_following_migration_scripts_do_not_run()
        {
            var migration = new UpMigration(_mockVersionRepository.Object, _mockAppliedScriptsRepository.Object, false);

            _mockScriptRunner.Setup(m => m.Execute(SomeScripts[1].ForwardScript)).Throws(new SqlPlusException());

            Ignore.Exception(() => migration.Migrate(SomeScripts, null, null, _mockScriptRunner.Object, Some.String(), Some.String()));

            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[0].ForwardScript.AsDatabaseVersion(), It.IsAny <string>()), Times.Once);
            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[1].ForwardScript.AsDatabaseVersion(), It.IsAny <string>()), Times.Never);
            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[2].ForwardScript.AsDatabaseVersion(), It.IsAny <string>()), Times.Never);
        }
Exemplo n.º 6
0
        public void if_force_is_specified_then_the_migration_scripts_will_still_run()
        {
            var migration = new UpMigration(_mockVersionRepository.Object, _mockAppliedScriptsRepository.Object, true);

            _mockScriptRunner.Setup(m => m.Execute(SomeScripts[1].ForwardScript)).Throws(new SqlPlusException());

            Ignore.Exception(() => migration.Migrate(SomeScripts, null, null, _mockScriptRunner.Object, Some.String(), Some.String()));

            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[0].ForwardScript.AsDatabaseVersion(), It.IsAny<string>()), Times.Once);
            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[1].ForwardScript.AsDatabaseVersion(), It.IsAny<string>()), Times.Once);
            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[2].ForwardScript.AsDatabaseVersion(), It.IsAny<string>()), Times.Once);
        }
Exemplo n.º 7
0
        public void if_a_migration_script_fails_then_the_following_migration_scripts_do_not_run()
        {
            var migration = new UpMigration(_mockVersionRepository.Object, _mockAppliedScriptsRepository.Object, false);

            _mockScriptRunner.Setup(m => m.Execute(SomeScripts[1].ForwardScript)).Throws(new SqlPlusException());

            Ignore.Exception(() => migration.Migrate(SomeScripts, null, null, _mockScriptRunner.Object, Some.String(), Some.String()));

            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[0].ForwardScript.AsDatabaseVersion(), It.IsAny<string>()), Times.Once);
            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[1].ForwardScript.AsDatabaseVersion(), It.IsAny<string>()), Times.Never);
            _mockVersionRepository.Verify(m => m.InsertVersion(SomeScripts[2].ForwardScript.AsDatabaseVersion(), It.IsAny<string>()), Times.Never);
        }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
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);
            }
        }
Exemplo n.º 10
0
        public void when_there_are_no_migration_scripts_then_none_are_ever_executed()
        {
            var migration = new UpMigration(_mockVersionRepository.Object, _mockAppliedScriptsRepository.Object, false);
            migration.Migrate(Enumerable.Empty<IStep>(), null, null, _mockScriptRunner.Object, Some.String(), Some.String());

            _mockScriptRunner.Verify(m => m.Execute(It.IsAny<IScript>()), Times.Never);
        }