From 67315dbbeb781921c7eb530b996e1020ad84e91b Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Tue, 23 Sep 2014 08:58:38 -0400 Subject: Since we don't have migrations, we don't need the ITemporaryDatabase stuff, nor do we need the TAG mechanism. We also don't need load_sql() or load_migrations(). --- src/mailman/database/postgresql.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/mailman/database/postgresql.py') diff --git a/src/mailman/database/postgresql.py b/src/mailman/database/postgresql.py index 1ee454074..bdc3c930c 100644 --- a/src/mailman/database/postgresql.py +++ b/src/mailman/database/postgresql.py @@ -40,8 +40,6 @@ from mailman.testing.helpers import configuration class PostgreSQLDatabase(SABaseDatabase): """Database class for PostgreSQL.""" - TAG = 'postgres' - def _database_exists(self, store): """See `BaseDatabase`.""" table_query = ('SELECT table_name FROM information_schema.tables ' -- cgit v1.2.3-70-g09d2 From ea124d4fba3e94d0d8885d898500c9a06d7daad0 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Tue, 23 Sep 2014 09:09:45 -0400 Subject: We do not need _database_exists() or _make_temporary() any more either. --- src/mailman/database/base.py | 14 ----------- src/mailman/database/model.py | 2 ++ src/mailman/database/postgresql.py | 51 +------------------------------------- src/mailman/database/sqlite.py | 40 +++--------------------------- 4 files changed, 6 insertions(+), 101 deletions(-) (limited to 'src/mailman/database/postgresql.py') diff --git a/src/mailman/database/base.py b/src/mailman/database/base.py index f67a60035..e360dcedf 100644 --- a/src/mailman/database/base.py +++ b/src/mailman/database/base.py @@ -62,16 +62,6 @@ class SABaseDatabase: """See `IDatabase`.""" self.store.rollback() - def _database_exists(self): - """Return True if the database exists and is initialized. - - Return False when Mailman needs to create and initialize the - underlying database schema. - - Base classes *must* override this. - """ - raise NotImplementedError - def _pre_reset(self, store): """Clean up method for testing. @@ -124,7 +114,3 @@ class SABaseDatabase: session = sessionmaker(bind=self.engine) self.store = session() self.store.commit() - - @staticmethod - def _make_temporary(): - raise NotImplementedError diff --git a/src/mailman/database/model.py b/src/mailman/database/model.py index f8a15162c..06705dfe9 100644 --- a/src/mailman/database/model.py +++ b/src/mailman/database/model.py @@ -44,6 +44,8 @@ class ModelMeta: with contextlib.closing(config.db.engine.connect()) as connection: transaction = connection.begin() try: + # Delete all the tables in reverse foreign key dependency + # order. http://tinyurl.com/on8dy6f for table in reversed(Model.metadata.sorted_tables): connection.execute(table.delete()) except: diff --git a/src/mailman/database/postgresql.py b/src/mailman/database/postgresql.py index bdc3c930c..59fff0865 100644 --- a/src/mailman/database/postgresql.py +++ b/src/mailman/database/postgresql.py @@ -22,32 +22,17 @@ from __future__ import absolute_import, print_function, unicode_literals __metaclass__ = type __all__ = [ 'PostgreSQLDatabase', - 'make_temporary', ] -import types - -from functools import partial -from operator import attrgetter -from urlparse import urlsplit, urlunsplit - from mailman.database.base import SABaseDatabase -from mailman.testing.helpers import configuration +from operator import attrgetter class PostgreSQLDatabase(SABaseDatabase): """Database class for PostgreSQL.""" - def _database_exists(self, store): - """See `BaseDatabase`.""" - table_query = ('SELECT table_name FROM information_schema.tables ' - "WHERE table_schema = 'public'") - results = store.execute(table_query) - table_names = set(item[0] for item in results) - return 'version' in table_names - def _post_reset(self, store): """PostgreSQL-specific test suite cleanup. @@ -67,37 +52,3 @@ class PostgreSQLDatabase(SABaseDatabase): max("id") IS NOT null) FROM "{0}"; """.format(model_class.__storm_table__)) - - - -# Test suite adapter for ITemporaryDatabase. - -def _cleanup(self, store, tempdb_name): - from mailman.config import config - store.rollback() - store.close() - # From the original database connection, drop the now unused database. - config.db.store.execute('DROP DATABASE {0}'.format(tempdb_name)) - - -def make_temporary(database): - """Adapts by monkey patching an existing PostgreSQL IDatabase.""" - from mailman.config import config - 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(_cleanup, store=database.store, tempdb_name='mmtest'), - database) - # bool column values in PostgreSQL. - database.FALSE = 'False' - database.TRUE = 'True' - return database diff --git a/src/mailman/database/sqlite.py b/src/mailman/database/sqlite.py index 495f69990..db7860390 100644 --- a/src/mailman/database/sqlite.py +++ b/src/mailman/database/sqlite.py @@ -22,61 +22,27 @@ from __future__ import absolute_import, print_function, unicode_literals __metaclass__ = type __all__ = [ 'SQLiteDatabase', - 'make_temporary', ] import os -import types -import shutil -import tempfile - -from functools import partial -from urlparse import urlparse from mailman.database.base import SABaseDatabase -from mailman.testing.helpers import configuration +from urlparse import urlparse class SQLiteDatabase(SABaseDatabase): """Database class for SQLite.""" - def _database_exists(self, store): - """See `BaseDatabase`.""" - table_query = 'select tbl_name from sqlite_master;' - table_names = set(item[0] for item in - store.execute(table_query)) - return 'version' in table_names - def _prepare(self, url): parts = urlparse(url) assert parts.scheme == 'sqlite', ( 'Database url mismatch (expected sqlite prefix): {0}'.format(url)) + # Ensure that the SQLite database file has the proper permissions, + # since SQLite doesn't play nice with umask. path = os.path.normpath(parts.path) fd = os.open(path, os.O_WRONLY | os.O_NONBLOCK | os.O_CREAT, 0o666) # Ignore errors if fd > 0: os.close(fd) - - - -# Test suite adapter for ITemporaryDatabase. - -def _cleanup(self, tempdir): - shutil.rmtree(tempdir) - - -def make_temporary(database): - """Adapts by monkey patching an existing SQLite IDatabase.""" - tempdir = tempfile.mkdtemp() - url = 'sqlite:///' + os.path.join(tempdir, 'mailman.db') - with configuration('database', url=url): - database.initialize() - database._cleanup = types.MethodType( - partial(_cleanup, tempdir=tempdir), - database) - # bool column values in SQLite must be integers. - database.FALSE = 0 - database.TRUE = 1 - return database -- cgit v1.2.3-70-g09d2