diff options
Diffstat (limited to 'src/mailman/database/factory.py')
| -rw-r--r-- | src/mailman/database/factory.py | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/src/mailman/database/factory.py b/src/mailman/database/factory.py index 80ec09b36..0ea2f4776 100644 --- a/src/mailman/database/factory.py +++ b/src/mailman/database/factory.py @@ -31,8 +31,9 @@ import os import types import shutil import tempfile -import functools +from functools import partial +from urlparse import urlsplit, urlunsplit from zope.interface import implementer from zope.interface.verify import verifyObject @@ -64,7 +65,9 @@ def _reset(self): """See `IDatabase`.""" from mailman.database.model import ModelMeta self.store.rollback() + self._pre_reset(self.store) ModelMeta._reset(self.store) + self._post_reset(self.store) self.store.commit() @@ -91,6 +94,12 @@ def _sqlite_cleanup(self, tempdir): shutil.rmtree(tempdir) +def _postgresql_cleanup(self, database, tempdb_name): + database.store.rollback() + database.store.close() + config.db.store.execute('DROP DATABASE {0}'.format(tempdb_name)) + + @implementer(IDatabaseFactory) class DatabaseTemporaryFactory: """Create a temporary database for some of the migration tests.""" @@ -107,9 +116,32 @@ class DatabaseTemporaryFactory: with configuration('database', url=url): database.initialize() database._cleanup = types.MethodType( - functools.partial(_sqlite_cleanup, tempdir=tempdir), database) + partial(_sqlite_cleanup, tempdir=tempdir), database) # bool column values in SQLite must be integers. database.FALSE = 0 database.TRUE = 1 + elif database.TAG == 'postgres': + parts = urlsplit(config.database.url) + assert parts.scheme == 'postgres' + new_parts = list(parts) + new_parts[2] = '/mmtest' + url = urlunsplit(new_parts) + # Use the existing database connection to create a new testing + # database. + config.db.store.execute('ABORT;') + config.db.store.execute('CREATE DATABASE mmtest;') + with configuration('database', url=url): + database.initialize() + database._cleanup = types.MethodType( + partial(_postgresql_cleanup, + database=database, + tempdb_name='mmtest'), + database) + # bool column values in PostgreSQL. + database.FALSE = 'False' + database.TRUE = 'True' + else: + raise RuntimeError( + 'Unsupported test database: {0}'.format(database.TAG)) # Don't load the migrations. return database |
