summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBarry Warsaw2011-10-23 18:53:18 -0400
committerBarry Warsaw2011-10-23 18:53:18 -0400
commit379aa1fac89428650e40ace3a672cdf43cc6aa73 (patch)
treed9db244ea274d3f18afa5c76561a2234de8a8885 /src
parent4eb33b4c70e980d4689c42d359f9321a4b41df6c (diff)
downloadmailman-379aa1fac89428650e40ace3a672cdf43cc6aa73.tar.gz
mailman-379aa1fac89428650e40ace3a672cdf43cc6aa73.tar.zst
mailman-379aa1fac89428650e40ace3a672cdf43cc6aa73.zip
Diffstat (limited to 'src')
-rw-r--r--src/mailman/database/base.py18
-rw-r--r--src/mailman/database/model.py3
-rw-r--r--src/mailman/database/postgresql.py22
3 files changed, 42 insertions, 1 deletions
diff --git a/src/mailman/database/base.py b/src/mailman/database/base.py
index 37fe35eb9..1e71341e0 100644
--- a/src/mailman/database/base.py
+++ b/src/mailman/database/base.py
@@ -97,6 +97,24 @@ class StormBaseDatabase:
"""
raise NotImplementedError
+ def _pre_reset(self, store):
+ """Clean up method for testing.
+
+ This method is called during the test suite just before all the model
+ tables are removed. Override this to perform any database-specific
+ pre-removal cleanup.
+ """
+ pass
+
+ def _post_reset(self, store):
+ """Clean up method for testing.
+
+ This method is called during the test suite just after all the model
+ tables have been removed. Override this to perform any
+ database-specific post-removal cleanup.
+ """
+ pass
+
def _create(self, debug):
# Calculate the engine url.
url = expand(config.database.url, config.paths)
diff --git a/src/mailman/database/model.py b/src/mailman/database/model.py
index 173659269..eec88936f 100644
--- a/src/mailman/database/model.py
+++ b/src/mailman/database/model.py
@@ -49,11 +49,14 @@ class ModelMeta(PropertyPublisherMeta):
@staticmethod
def _reset(store):
+ from mailman.config import config
+ config.db._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()
+ config.db._post_reset(store)
diff --git a/src/mailman/database/postgresql.py b/src/mailman/database/postgresql.py
index b59165243..4e40558a4 100644
--- a/src/mailman/database/postgresql.py
+++ b/src/mailman/database/postgresql.py
@@ -25,6 +25,7 @@ __all__ = [
]
+from operator import attrgetter
from pkg_resources import resource_string
from mailman.database.base import StormBaseDatabase
@@ -38,10 +39,29 @@ class PostgreSQLDatabase(StormBaseDatabase):
"""See `BaseDatabase`."""
table_query = ('SELECT table_name FROM information_schema.tables '
"WHERE table_schema = 'public'")
- table_names = set(item[0] for item in
+ table_names = set(item[0] for item in
store.execute(table_query))
return 'version' in table_names
def _get_schema(self):
"""See `BaseDatabase`."""
return resource_string('mailman.database.sql', 'postgres.sql')
+
+ def _post_reset(self, store):
+ """PostgreSQL-specific test suite cleanup.
+
+ Reset the <tablename>_id_seq.last_value so that primary key ids
+ restart from zero for new tests.
+ """
+ from mailman.database.model import ModelMeta
+ classes = sorted(ModelMeta._class_registry,
+ key=attrgetter('__storm_table__'))
+ # Recipe adapted from
+ # http://stackoverflow.com/questions/544791/
+ # django-postgresql-how-to-reset-primary-key
+ for model_class in classes:
+ store.execute("""\
+ SELECT setval('"{0}_id_seq"', coalesce(max("id"), 1),
+ max("id") IS NOT null)
+ FROM "{0}";
+ """.format(model_class.__storm_table__))