summaryrefslogtreecommitdiff
path: root/src/mailman/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/mailman/database')
-rw-r--r--src/mailman/database/alembic/env.py1
-rw-r--r--src/mailman/database/base.py15
-rw-r--r--src/mailman/database/factory.py8
-rw-r--r--src/mailman/database/model.py2
-rw-r--r--src/mailman/database/postgresql.py10
-rw-r--r--src/mailman/database/types.py3
6 files changed, 24 insertions, 15 deletions
diff --git a/src/mailman/database/alembic/env.py b/src/mailman/database/alembic/env.py
index ee6d8293f..55f0418f1 100644
--- a/src/mailman/database/alembic/env.py
+++ b/src/mailman/database/alembic/env.py
@@ -63,6 +63,7 @@ def run_migrations_online():
alembic_cfg = Config()
alembic_cfg.set_main_option(
'script_location', expand_path(config.database['alembic_scripts']))
+ alembic_cfg.set_section_option('logger_alembic' ,'level' , 'ERROR')
url = expand(config.database.url, config.paths)
engine = create_engine(url)
diff --git a/src/mailman/database/base.py b/src/mailman/database/base.py
index e360dcedf..e49d512c0 100644
--- a/src/mailman/database/base.py
+++ b/src/mailman/database/base.py
@@ -25,6 +25,8 @@ __all__ = [
import logging
+from alembic import command
+from alembic.config import Config
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from zope.interface import implementer
@@ -32,6 +34,7 @@ from zope.interface import implementer
from mailman.config import config
from mailman.interfaces.database import IDatabase
from mailman.utilities.string import expand
+from mailman.utilities.modules import expand_path
log = logging.getLogger('mailman.config')
@@ -89,6 +92,18 @@ class SABaseDatabase:
"""
pass
+ def stamp(self, debug=False):
+ """Stamp the database with the latest Alembic version."""
+ # Newly created databases don't need migrations from Alembic, since
+ # create_all() ceates the latest schema. This patches the database
+ # with the latest Alembic version to add an entry in the
+ # alembic_version table.
+ alembic_cfg = Config()
+ alembic_cfg.set_main_option(
+ 'script_location', expand_path(config.database['alembic_scripts']))
+ command.stamp(alembic_cfg, 'head')
+
+
def initialize(self, debug=None):
"""See `IDatabase`."""
# Calculate the engine url.
diff --git a/src/mailman/database/factory.py b/src/mailman/database/factory.py
index 189fd6ac4..0a295331a 100644
--- a/src/mailman/database/factory.py
+++ b/src/mailman/database/factory.py
@@ -29,11 +29,7 @@ __all__ = [
import os
import types
-from alembic import command
-from alembic.config import Config
-
from flufl.lock import Lock
-from pkg_resources import resource_filename
from zope.interface import implementer
from zope.interface.verify import verifyObject
@@ -57,9 +53,7 @@ class DatabaseFactory:
verifyObject(IDatabase, database)
database.initialize()
Model.metadata.create_all(database.engine)
- alembic_cfg = Config(
- resource_filename('mailman.config', 'alembic.ini'))
- command.stamp(alembic_cfg, 'head')
+ database.stamp()
database.commit()
return database
diff --git a/src/mailman/database/model.py b/src/mailman/database/model.py
index 06705dfe9..b65d7d5eb 100644
--- a/src/mailman/database/model.py
+++ b/src/mailman/database/model.py
@@ -49,7 +49,7 @@ class ModelMeta:
for table in reversed(Model.metadata.sorted_tables):
connection.execute(table.delete())
except:
- transaction.abort()
+ transaction.rollback()
raise
else:
transaction.commit()
diff --git a/src/mailman/database/postgresql.py b/src/mailman/database/postgresql.py
index 59fff0865..717b69dd1 100644
--- a/src/mailman/database/postgresql.py
+++ b/src/mailman/database/postgresql.py
@@ -26,7 +26,7 @@ __all__ = [
from mailman.database.base import SABaseDatabase
-from operator import attrgetter
+from mailman.database.model import Model
@@ -40,15 +40,13 @@ class PostgreSQLDatabase(SABaseDatabase):
restart from zero for new tests.
"""
super(PostgreSQLDatabase, self)._post_reset(store)
- from mailman.database.model import ModelMeta
- classes = sorted(ModelMeta._class_registry,
- key=attrgetter('__storm_table__'))
+ tables = reversed(Model.metadata.sorted_tables)
# Recipe adapted from
# http://stackoverflow.com/questions/544791/
# django-postgresql-how-to-reset-primary-key
- for model_class in classes:
+ for table in tables:
store.execute("""\
SELECT setval('"{0}_id_seq"', coalesce(max("id"), 1),
max("id") IS NOT null)
FROM "{0}";
- """.format(model_class.__storm_table__))
+ """.format(table))
diff --git a/src/mailman/database/types.py b/src/mailman/database/types.py
index 641e065ba..1984b08b5 100644
--- a/src/mailman/database/types.py
+++ b/src/mailman/database/types.py
@@ -29,6 +29,7 @@ __all__ = [
import uuid
from sqlalchemy import Integer
+from sqlalchemy.dialects import postgresql
from sqlalchemy.types import TypeDecorator, CHAR
@@ -68,7 +69,7 @@ class UUID(TypeDecorator):
def load_dialect_impl(self, dialect):
if dialect.name == 'postgresql':
- return dialect.type_descriptor(UUID())
+ return dialect.type_descriptor(postgresql.UUID())
else:
return dialect.type_descriptor(CHAR(32))