diff options
author | olly <olly@ollycope.com> | 2015-09-04 09:26:08 +0000 |
---|---|---|
committer | olly <olly@ollycope.com> | 2015-09-04 09:26:08 +0000 |
commit | ec1e0fb3744e16ac309ee7e55c7865fe1e151405 (patch) | |
tree | b2d91f2b8288c1f29353d768d57b4643493bb191 | |
parent | 06f284a1de778ed02a45280dc5231453fad4b3ac (diff) | |
download | yoyo-ec1e0fb3744e16ac309ee7e55c7865fe1e151405.tar.gz |
tests: delete all _yoyo prefixed tables in backend_fixture
-rw-r--r-- | yoyo/backends.py | 11 | ||||
-rw-r--r-- | yoyo/tests/conftest.py | 10 |
2 files changed, 20 insertions, 1 deletions
diff --git a/yoyo/backends.py b/yoyo/backends.py index 7ec5938..2cb9e8f 100644 --- a/yoyo/backends.py +++ b/yoyo/backends.py @@ -102,6 +102,7 @@ class DatabaseBackend(object): id VARCHAR(255) NOT NULL PRIMARY KEY, ctime TIMESTAMP )""" + list_tables_sql = "SELECT table_name FROM information_schema.tables" is_applied_sql = "SELECT COUNT(1) FROM {0.migration_table} WHERE id=?" insert_migration_sql = ("INSERT INTO {0.migration_table} (id, ctime) " "VALUES (?, ?)") @@ -155,6 +156,15 @@ class DatabaseBackend(object): return True return False + def list_tables(self): + """ + Return a list of tables present in the backend. + This is used by the test suite to clean up tables + generated during testing + """ + cursor = self.execute(self.list_tables_sql) + return [row[0] for row in cursor.fetchall()] + def transaction(self): if not self._in_transaction: return TransactionManager(self) @@ -356,6 +366,7 @@ class MySQLBackend(DatabaseBackend): class SQLiteBackend(DatabaseBackend): driver_module = 'sqlite3' + list_tables_sql = "SELECT name FROM sqlite_master WHERE type = 'table'" def connect(self, dburi): conn = self.driver.connect(dburi.database) diff --git a/yoyo/tests/conftest.py b/yoyo/tests/conftest.py index 6bfc777..202ded8 100644 --- a/yoyo/tests/conftest.py +++ b/yoyo/tests/conftest.py @@ -8,4 +8,12 @@ def backend_fixture(request): """ Return all backends configured in ``test_databases.ini`` """ - yield request.param + backend = request.param + try: + yield backend + finally: + backend.rollback() + for table in (request.param.list_tables()): + if table.startswith('_yoyo'): + with backend.transaction(): + backend.execute("DROP TABLE {}".format(table)) |