diff options
author | olly <olly@ollycope.com> | 2015-09-04 09:29:58 +0000 |
---|---|---|
committer | olly <olly@ollycope.com> | 2015-09-04 09:29:58 +0000 |
commit | f0cbbe17200614b8e2166098ff411e636605c4cc (patch) | |
tree | 7a9a6311a233a66ef526f436a08b3757e78a2956 | |
parent | ec1e0fb3744e16ac309ee7e55c7865fe1e151405 (diff) | |
download | yoyo-f0cbbe17200614b8e2166098ff411e636605c4cc.tar.gz |
Add backend.DatabaseError
This replaces the clunky "except tuple(exceptions.DatabaseErrors)" idiom
-rw-r--r-- | yoyo/backends.py | 5 | ||||
-rwxr-xr-x | yoyo/migrations.py | 6 | ||||
-rw-r--r-- | yoyo/tests/test_backends.py | 5 | ||||
-rw-r--r-- | yoyo/tests/test_migrations.py | 7 |
4 files changed, 8 insertions, 15 deletions
diff --git a/yoyo/backends.py b/yoyo/backends.py index 2cb9e8f..f37f4df 100644 --- a/yoyo/backends.py +++ b/yoyo/backends.py @@ -119,6 +119,7 @@ class DatabaseBackend(object): self.migration_table = migration_table self.create_migrations_table() self.has_transactional_ddl = self._check_transactional_ddl() + self.DatabaseError = self.driver.DatabaseError def _load_driver_module(self): """ @@ -152,7 +153,7 @@ class DatabaseBackend(object): try: with self.transaction(): self.execute("DROP TABLE {}".format(table_name)) - except tuple(exceptions.DatabaseErrors): + except self.DatabaseError: return True return False @@ -225,7 +226,7 @@ class DatabaseBackend(object): try: with self.transaction(): self.execute(sql) - except tuple(exceptions.DatabaseErrors): + except self.DatabaseError: pass def _with_placeholders(self, sql): diff --git a/yoyo/migrations.py b/yoyo/migrations.py index 72fd1bf..fd12c4d 100755 --- a/yoyo/migrations.py +++ b/yoyo/migrations.py @@ -99,13 +99,13 @@ class Migration(object): try: getattr(step, direction)(backend, force) executed_steps.append(step) - except tuple(exceptions.DatabaseErrors): + except backend.DatabaseError: backend.connection.rollback() exc_info = sys.exc_info() try: for step in reversed(executed_steps): getattr(step, reverse)(backend) - except tuple(exceptions.DatabaseErrors): + except backend.DatabaseError: logger.exception( 'Database error when reversing %s of step', direction) reraise(exc_info[0], exc_info[1], exc_info[2]) @@ -152,7 +152,7 @@ class TransactionWrapper(StepBase): with backend.transaction() as transaction: try: getattr(self.step, direction)(backend, force) - except tuple(exceptions.DatabaseErrors): + except backend.DatabaseError: if force or self.ignore_errors in (direction, 'all'): logger.exception("Ignored error in %r", self.step) transaction.rollback() diff --git a/yoyo/tests/test_backends.py b/yoyo/tests/test_backends.py index 0fc8050..2509ecc 100644 --- a/yoyo/tests/test_backends.py +++ b/yoyo/tests/test_backends.py @@ -1,7 +1,6 @@ import pytest from yoyo import backends -from yoyo import exceptions from yoyo.tests import get_test_backends @@ -31,13 +30,11 @@ class TestTransactionHandling(object): assert rows == [('A',)] def test_it_rolls_back(self, backend): - try: + with pytest.raises(backend.DatabaseError): with backend.transaction(): backend.execute("INSERT INTO _yoyo_t values ('A')") # Invalid SQL to produce an error backend.execute("INSERT INTO nonexistant values ('A')") - except tuple(exceptions.DatabaseErrors): - pass with backend.transaction(): rows = list(backend.execute("SELECT * FROM _yoyo_t").fetchall()) diff --git a/yoyo/tests/test_migrations.py b/yoyo/tests/test_migrations.py index 6843b56..4a2c303 100644 --- a/yoyo/tests/test_migrations.py +++ b/yoyo/tests/test_migrations.py @@ -35,13 +35,8 @@ step("INSERT INTO _yoyo_test VALUES ('x', 'y')") def test_transaction_is_not_committed_on_error(tmpdir): backend = get_backend(dburi) migrations = read_migrations(tmpdir) - try: + with pytest.raises(backend.DatabaseError): backend.apply_migrations(migrations) - except tuple(exceptions.DatabaseErrors): - # Expected - pass - else: - raise AssertionError("Expected a DatabaseError") cursor = backend.cursor() cursor.execute("SELECT count(1) FROM _yoyo_test") assert cursor.fetchone() == (0,) |