def test_it_should_stop_process_when_an_error_occur_during_database_change(self):
        self.execute_returns["insert into spam"] = Exception("invalid sql")

        try:
            oracle = Oracle(self.config_mock, self.db_driver_mock, self.getpass_mock, self.stdin_mock)
            oracle.change("create table spam(); insert into spam", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", label_version="label")
        except Exception as e:
            self.assertEqual("error executing migration: invalid sql\n\n[ERROR DETAILS] SQL command was:\ninsert into spam", str(e))
            self.assertTrue(isinstance(e, simple_db_migrate.core.exceptions.MigrationException))

        self.assertEqual(1, self.db_mock.rollback.call_count)
        self.assertEqual(5, self.db_driver_mock.connect.call_count)
        self.assertEqual(2, self.db_mock.commit.call_count)
        self.assertEqual(5, self.db_mock.close.call_count)

        expected_execute_calls = [
            call('select version from db_version'),
            call('select count(*) from db_version'),
            call("insert into db_version (id, version) values (db_version_seq.nextval, '0')"),
            call('create table spam()'),
            call('insert into spam')
        ]

        self.assertEqual(expected_execute_calls, self.cursor_mock.execute.mock_calls)
        self.assertEqual(4, self.cursor_mock.close.call_count)
    def test_it_should_execute_migration_up_and_update_schema_version(self):
        self.db_driver_mock.CLOB = 'X'

        oracle = Oracle(self.config_mock, self.db_driver_mock, self.getpass_mock, self.stdin_mock)
        oracle.change("create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;")

        self.assertEqual(6, self.db_driver_mock.connect.call_count)
        self.assertEqual(4, self.db_mock.commit.call_count)
        self.assertEqual(6, self.db_mock.close.call_count)

        expected_execute_calls = [
            call('select version from db_version'),
            call('select count(*) from db_version'),
            call("insert into db_version (id, version) values (db_version_seq.nextval, '0')"),
            call('create table spam()'),
            call('insert into db_version (id, version, label, name, sql_up, sql_down) values (db_version_seq.nextval, :version, :label, :migration_file_name, :sql_up, :sql_down)', {'label': None, 'sql_up': 'create table spam();', 'version': '20090212112104', 'sql_down': 'drop table spam;', 'migration_file_name': '20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration'})
        ]

        self.assertEqual(expected_execute_calls, self.cursor_mock.execute.mock_calls)
        self.assertEqual(5, self.cursor_mock.close.call_count)

        expected_var_calls = [
            call('X', 20),
            call().setvalue(0, 'create table spam();'),
            call('X', 16),
            call().setvalue(0, 'drop table spam;')
        ]
        self.assertEqual(expected_var_calls, self.cursor_mock.var.mock_calls)
示例#3
0
def run_example():
    db = Oracle(config=config)
    db.change(sql=sql,
              new_db_version=new_db_version,
              migration_file_name=migration_file_name,
              sql_up=sql_up,
              sql_down=sql_down)
    def test_it_should_log_execution_when_a_function_is_given_when_updating_schema_version(self):
        execution_log_mock = Mock()
        oracle = Oracle(self.config_mock, self.db_driver_mock, self.getpass_mock, self.stdin_mock)
        oracle.change("create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", execution_log=execution_log_mock)

        expected_execution_log_calls = [
            call('create table spam()\n-- 0 row(s) affected\n'),
            call('migration 20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration registered\n')
        ]
        self.assertEqual(expected_execution_log_calls, execution_log_mock.mock_calls)
    def test_it_should_execute_migration_down_and_update_schema_version(self):
        oracle = Oracle(self.config_mock, self.db_driver_mock, self.getpass_mock, self.stdin_mock)
        oracle.change("drop table spam;", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", False)

        self.assertEqual(6, self.db_driver_mock.connect.call_count)
        self.assertEqual(4, self.db_mock.commit.call_count)
        self.assertEqual(6, self.db_mock.close.call_count)

        expected_execute_calls = [
            call('select version from db_version'),
            call('select count(*) from db_version'),
            call("insert into db_version (id, version) values (db_version_seq.nextval, '0')"),
            call('drop table spam'),
            call('delete from db_version where version = :version', {'version': '20090212112104'})
        ]

        self.assertEqual(expected_execute_calls, self.cursor_mock.execute.mock_calls)
        self.assertEqual(5, self.cursor_mock.close.call_count)