def test_one_pending_migration_failed(self):
        self.migration_script_repository_mock.find_all.return_value = [
            Migration(version="1.0.0",
                      description="test migration",
                      checksum="1234",
                      script="",
                      filename="V1.0.0__description.sh")
        ]

        commandline_parse_phase = CommandlineParsePhase()
        self.migration_script_runner_mock.run_migration_script.return_value = MigrationExecutionResult(
            success=False)

        with self.assertLogs() as cm:
            with self.assertRaises(SystemExit):
                commandline_parse_phase.start(
                    args=["--migration-path", "/path", "migrate"])

        expected_path = os.path.join("/path", "V1.0.0__description.sh")
        self.migration_script_runner_mock.run_migration_script.assert_called_once_with(
            expected_path)

        self.assertIn(
            "INFO:symigrate.command.migrate_command:Found 1 pending migrations",
            cm.output)
        self.assertIn(
            "ERROR:symigrate.main.symigrate:StopOnMigrationError: Stopping migration due to failed "
            "migration script execution", cm.output)

        migration_table_row = self.database_connection.execute(
            "SELECT version, status FROM migration").fetchone()
        self.assertIsNotNone(migration_table_row)
        self.assertEqual("1.0.0", migration_table_row[0])
        self.assertEqual("FAILED", migration_table_row[1])
    def test_unable_to_find_migration_script(self):
        commandline_parse_phase = CommandlineParsePhase()

        with self.assertLogs() as log:
            commandline_parse_phase.start(args=["diff", "--version", "2.0.0"])

        self.assertIn(
            "WARNING:symigrate.command.diff_command:Unable to find executed migration for version '2.0.0'",
            log.output)
    def test_diff_output(self):
        self.migration_script_repository_mock.find_by_version.return_value = \
            Migration(version="1.0.0", description="test migration", checksum="2345", script="", filename="")

        commandline_parse_phase = CommandlineParsePhase()

        with capture_output() as output:
            commandline_parse_phase.start(args=["diff", "--version", "1.0.0"])

        self.assertEqual("- The script", output[0].getvalue())
    def test_no_migrations_at_all(self):
        self.migration_script_repository_mock.find_all.return_value = []
        commandline_parse_phase = CommandlineParsePhase()

        with self.assertLogs() as cm:
            commandline_parse_phase.start(args=["migrate"])

        self.assertIn(
            "INFO:symigrate.command.migrate_command:No pending migrations found",
            cm.output)
    def test_single_migration(self):
        def call_single_migration(expected_version: str,
                                  expected_entries_in_table: int,
                                  pending_migrations_found: int):
            with self.assertLogs() as _cm:
                commandline_parse_phase.start(
                    args=["--migration-path", "/path", "migrate", "--single"])

            self.assertIn(
                "INFO:symigrate.command.migrate_command:Found %d pending migrations"
                % pending_migrations_found, _cm.output)
            self.assertIn(
                "INFO:symigrate.command.migrate_command:Only executing the next pending migration",
                _cm.output)

            migration_table_rows = self.database_connection.execute(
                "SELECT version, status FROM migration").fetchall()
            self.assertEqual(expected_entries_in_table,
                             len(migration_table_rows))
            self.assertEqual(expected_version, migration_table_rows[-1][0])
            self.assertEqual("SUCCESS", migration_table_rows[-1][1])

        self.migration_script_repository_mock.find_all.return_value = [
            Migration(version="1.0.0",
                      description="first migration",
                      checksum="1234",
                      script="",
                      filename="V1.0.0__description.sh"),
            Migration(version="1.1.0",
                      description="second migration",
                      checksum="2345",
                      script="",
                      filename="V1.1.0_descr.sh"),
        ]
        commandline_parse_phase = CommandlineParsePhase()
        self.migration_script_runner_mock.run_migration_script.return_value = MigrationExecutionResult(
            success=True)

        # first single migration call
        call_single_migration(expected_version="1.0.0",
                              expected_entries_in_table=1,
                              pending_migrations_found=2)
        # second single migration call
        call_single_migration(expected_version="1.1.0",
                              expected_entries_in_table=2,
                              pending_migrations_found=1)

        with self.assertLogs() as cm:
            commandline_parse_phase.start(
                args=["--migration-path", "/path", "migrate", "--single"])

        self.assertIn(
            "INFO:symigrate.command.migrate_command:No pending migrations found",
            cm.output)
    def test_diff_no_difference(self):
        self.migration_script_repository_mock.find_by_version.return_value = \
            Migration(version="1.0.0", description="test migration", checksum="1234", script="", filename="")

        commandline_parse_phase = CommandlineParsePhase()

        with self.assertLogs() as log:
            commandline_parse_phase.start(args=["diff", "--version", "1.0.0"])

        self.assertIn(
            "INFO:symigrate.command.diff_command:No difference found",
            log.output)
    def test_info_custom_scope(self):
        commandline_parse_phase = CommandlineParsePhase()

        commandline_parse_phase.start(["--scope", "my_scope", "info"])

        expected_output = dedent_and_remove_first_empty_line("""
            Scope: my_scope
            +-----------+-------------------+------------------+----------+
            | Version   | Description       | Migration Date   | Status   |
            +===========+===================+==================+==========+
            | 1.0.0     | test migration    |                  | PENDING  |
            +-----------+-------------------+------------------+----------+
            | 1.1.0     | another migration |                  | PENDING  |
            +-----------+-------------------+------------------+----------+
        """)

        self.assertEqual(expected_output, self.out_stream.getvalue())
    def test_no_pending_migrations(self):
        self.database_connection.execute(
            "INSERT INTO migration (scope, version, description, status, timestamp, checksum)"
            "VALUES"
            "('DEFAULT', '1.0.0', 'test migration', 'SUCCESS', '2018-04-29T11:40:00', '1234')"
        )
        self.migration_script_repository_mock.find_all.return_value = [
            Migration(version="1.0.0",
                      description="test migration",
                      checksum="1234",
                      script="",
                      filename="")
        ]

        commandline_parse_phase = CommandlineParsePhase()

        with self.assertLogs() as cm:
            commandline_parse_phase.start(args=["migrate"])

        self.assertIn(
            "INFO:symigrate.command.migrate_command:No pending migrations found",
            cm.output)
    def test_info_with_already_executed_migrations_checksum_mismatch(self):
        self.database_connection.execute(
            "INSERT INTO migration (scope, version, description, status, timestamp, checksum)"
            "VALUES"
            "('DEFAULT', '1.0.0', 'test migration', 'SUCCESS', '2018-04-29T11:40:00', '4321')"
        )
        self.database_connection.commit()

        commandline_parse_phase = CommandlineParsePhase()

        commandline_parse_phase.start(["info"])

        expected_output = dedent_and_remove_first_empty_line("""
            Scope: DEFAULT
            +-----------+-------------------+---------------------+-------------------+
            | Version   | Description       | Migration Date      | Status            |
            +===========+===================+=====================+===================+
            | 1.0.0     | test migration    | 2018-04-29 11:40:00 | SUCCESS, MODIFIED |
            +-----------+-------------------+---------------------+-------------------+
            | 1.1.0     | another migration |                     | PENDING           |
            +-----------+-------------------+---------------------+-------------------+
            """)

        self.assertEqual(expected_output, self.out_stream.getvalue())
Exemple #10
0
    def test_version_printing(self):
        command_line_parser_phase = CommandlineParsePhase()
        with capture_output() as output:
            command_line_parser_phase.start(["--version"])

        self.assertEqual("{}\n".format(__version__), output[0].getvalue())