summaryrefslogtreecommitdiff
path: root/src/mailman/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/mailman/database')
-rw-r--r--src/mailman/database/base.py7
-rw-r--r--src/mailman/database/factory.py80
-rw-r--r--src/mailman/database/model.py34
-rw-r--r--src/mailman/database/schema/mm_00000000000000_base.py13
-rw-r--r--src/mailman/database/schema/mm_20120407000000.py13
5 files changed, 85 insertions, 62 deletions
diff --git a/src/mailman/database/base.py b/src/mailman/database/base.py
index af035914b..39ce017c5 100644
--- a/src/mailman/database/base.py
+++ b/src/mailman/database/base.py
@@ -233,13 +233,6 @@ class StormBaseDatabase:
# is used by the test suite to reset the database between tests.
store.add(Version(component=version, version=module_path))
- def _reset(self):
- """See `IDatabase`."""
- from mailman.database.model import ModelMeta
- self.store.rollback()
- ModelMeta._reset(self.store)
- self.store.commit()
-
@staticmethod
def _make_temporary():
raise NotImplementedError
diff --git a/src/mailman/database/factory.py b/src/mailman/database/factory.py
new file mode 100644
index 000000000..4783410cc
--- /dev/null
+++ b/src/mailman/database/factory.py
@@ -0,0 +1,80 @@
+# Copyright (C) 2012 by the Free Software Foundation, Inc.
+#
+# This file is part of GNU Mailman.
+#
+# GNU Mailman is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option)
+# any later version.
+#
+# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
+
+"""Database factory."""
+
+from __future__ import absolute_import, print_function, unicode_literals
+
+__metaclass__ = type
+__all__ = [
+ 'DatabaseFactory',
+ 'DatabaseTestingFactory',
+ ]
+
+
+import types
+
+from zope.interface import implementer
+from zope.interface.verify import verifyObject
+
+from mailman.config import config
+from mailman.interfaces.database import IDatabase, IDatabaseFactory
+from mailman.utilities.modules import call_name
+
+
+
+@implementer(IDatabaseFactory)
+class DatabaseFactory:
+ """Create a new database."""
+
+ @staticmethod
+ def create():
+ """See `IDatabaseFactory`."""
+ database_class = config.database['class']
+ database = call_name(database_class)
+ verifyObject(IDatabase, database)
+ database.initialize()
+ database.load_migrations()
+ database.commit()
+ return database
+
+
+
+def _reset(self):
+ """See `IDatabase`."""
+ from mailman.database.model import ModelMeta
+ self.store.rollback()
+ ModelMeta._reset(self.store)
+ self.store.commit()
+
+
+@implementer(IDatabaseFactory)
+class DatabaseTestingFactory:
+ """Create a new database for testing."""
+
+ @staticmethod
+ def create():
+ """See `IDatabaseFactory`."""
+ database_class = config.database['class']
+ database = call_name(database_class)
+ verifyObject(IDatabase, database)
+ database.initialize()
+ database.load_migrations()
+ database.commit()
+ # Make _reset() a bound method of the database instance.
+ database._reset = types.MethodType(_reset, database)
+ return database
diff --git a/src/mailman/database/model.py b/src/mailman/database/model.py
index c45517c9b..227543351 100644
--- a/src/mailman/database/model.py
+++ b/src/mailman/database/model.py
@@ -25,8 +25,6 @@ __all__ = [
]
-import sys
-
from operator import attrgetter
from storm.properties import PropertyPublisherMeta
@@ -43,6 +41,9 @@ class ModelMeta(PropertyPublisherMeta):
# property to enforce our table naming convention.
self.__storm_table__ = name.lower()
super(ModelMeta, self).__init__(name, bases, dict)
+ # By default, the table should be reset by the testing framework.
+ if not hasattr(self, 'PRESERVE'):
+ self.PRESERVE = False
# Register the model class so that it can be more easily cleared.
# This is required by the test framework.
if name == 'Model':
@@ -52,38 +53,13 @@ class ModelMeta(PropertyPublisherMeta):
@staticmethod
def _reset(store):
from mailman.config import config
- from mailman.model.version import Version
config.db._pre_reset(store)
- # Give each schema migration a chance to do its pre-reset. See below
- # for calling its post reset too.
- versions = sorted(version.version for version in
- store.find(Version, component='schema'))
- migrations = {}
- for version in versions:
- # We have to give the migrations module that loaded this version a
- # chance to do both pre- and post-reset operations. The following
- # find the actual the module path for the migration. See
- # StormBaseDatabase.load_schema().
- migration = store.find(Version, component=version).one()
- if migration is None:
- continue
- migrations[version] = module_path = migration.version
- module = sys.modules[module_path]
- pre_reset = getattr(module, 'pre_reset', None)
- if pre_reset is not None:
- pre_reset(store)
# Make sure this is deterministic, by sorting on the storm table name.
classes = sorted(ModelMeta._class_registry,
key=attrgetter('__storm_table__'))
for model_class in classes:
- store.find(model_class).remove()
- # Now give each migration a chance to do post-reset operations.
- for version in versions:
- module = sys.modules[migrations[version]]
- post_reset = getattr(module, 'post_reset', None)
- if post_reset is not None:
- post_reset(store)
- config.db._post_reset(store)
+ if not model_class.PRESERVE:
+ store.find(model_class).remove()
diff --git a/src/mailman/database/schema/mm_00000000000000_base.py b/src/mailman/database/schema/mm_00000000000000_base.py
index f98b30a8d..0dcd28edd 100644
--- a/src/mailman/database/schema/mm_00000000000000_base.py
+++ b/src/mailman/database/schema/mm_00000000000000_base.py
@@ -21,8 +21,6 @@ from __future__ import absolute_import, print_function, unicode_literals
__metaclass__ = type
__all__ = [
- 'post_reset',
- 'pre_reset',
'upgrade',
]
@@ -35,14 +33,3 @@ _helper = None
def upgrade(database, store, version, module_path):
filename = '{0}.sql'.format(database.TAG)
database.load_schema(store, version, filename, module_path)
-
-
-
-## def pre_reset(store):
-## global _helper
-## from mailman.testing.database import ResetHelper
-## _helper = ResetHelper(VERSION, store)
-
-
-## def post_reset(store):
-## _helper.restore(store)
diff --git a/src/mailman/database/schema/mm_20120407000000.py b/src/mailman/database/schema/mm_20120407000000.py
index 017141683..9ed99e225 100644
--- a/src/mailman/database/schema/mm_20120407000000.py
+++ b/src/mailman/database/schema/mm_20120407000000.py
@@ -36,8 +36,6 @@ from __future__ import absolute_import, print_function, unicode_literals
__metaclass__ = type
__all__ = [
- 'post_reset',
- 'pre_reset',
'upgrade',
]
@@ -127,14 +125,3 @@ def upgrade_postgres(database, store, version, module_path):
store.execute('ALTER TABLE mailinglist DROP COLUMN archive_private;')
# Record the migration in the version table.
database.load_schema(store, version, None, module_path)
-
-
-
-## def pre_reset(store):
-## global _helper
-## from mailman.testing.database import ResetHelper
-## _helper = ResetHelper(VERSION, store)
-
-
-## def post_reset(store):
-## _helper.restore(store)