summaryrefslogtreecommitdiff
path: root/src/mailman/database
diff options
context:
space:
mode:
authorAbhilash Raj2014-10-02 20:16:00 +0530
committerAbhilash Raj2014-10-02 20:16:00 +0530
commit6d4af22c8ee51e1b097ec0370cadd3f5a6b95c08 (patch)
tree2f5a8b9723f670bdc4179f7dbdbb0322ab966fd9 /src/mailman/database
parent556631243a206f1680d6737227c34e2d66d85748 (diff)
parent5166ae93e5c51285661f4ac251c0d4b9390fe785 (diff)
downloadmailman-6d4af22c8ee51e1b097ec0370cadd3f5a6b95c08.tar.gz
mailman-6d4af22c8ee51e1b097ec0370cadd3f5a6b95c08.tar.zst
mailman-6d4af22c8ee51e1b097ec0370cadd3f5a6b95c08.zip
Merge barry\'s branch with test fixes and clean code
Diffstat (limited to 'src/mailman/database')
-rw-r--r--src/mailman/database/alembic/env.py59
-rw-r--r--src/mailman/database/base.py14
-rw-r--r--src/mailman/database/postgresql.py3
-rw-r--r--src/mailman/database/types.py2
4 files changed, 40 insertions, 38 deletions
diff --git a/src/mailman/database/alembic/env.py b/src/mailman/database/alembic/env.py
index 402c4f7da..5e4f6bfdb 100644
--- a/src/mailman/database/alembic/env.py
+++ b/src/mailman/database/alembic/env.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2006-2014 by the Free Software Foundation, Inc.
+# Copyright (C) 2014 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
@@ -15,31 +15,39 @@
# You should have received a copy of the GNU General Public License along with
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
-from __future__ import with_statement
+"""Alembic migration environment."""
+
+from __future__ import absolute_import, print_function, unicode_literals
+
+__metaclass__ = type
+__all__ = [
+ 'run_migrations_offline',
+ 'run_migrations_online',
+ ]
+
from alembic import context
from alembic.config import Config
-from sqlalchemy import create_engine, pool
+from contextlib import closing
+from sqlalchemy import create_engine
from mailman.core import initialize
from mailman.config import config
-from mailman.utilities.string import expand
from mailman.database.model import Model
-
-target_metadata = Model.metadata
+from mailman.utilities.modules import expand_path
+from mailman.utilities.string import expand
+
def run_migrations_offline():
"""Run migrations in 'offline' mode.
- This configures the context with just a URL
- and not an Engine, though an Engine is acceptable
- here as well. By skipping the Engine creation
- we don't even need a DBAPI to be available.
-
- Calls to context.execute() here emit the given string to the
- script output.
+ This configures the context with just a URL and not an Engine,
+ though an Engine is acceptable here as well. By skipping the Engine
+ creation we don't even need a DBAPI to be available.
+ Calls to context.execute() here emit the given string to the script
+ output.
"""
if not config.initialized:
initialize.initialize_1(context.config.config_file_name)
@@ -47,8 +55,7 @@ def run_migrations_offline():
alembic_cfg.set_main_option(
"script_location", config.alembic['script_location'])
url = expand(config.database.url, config.paths)
- context.configure(url=url, target_metadata=target_metadata)
-
+ context.configure(url=url, target_metadata=Model.metadata)
with context.begin_transaction():
context.run_migrations()
@@ -56,30 +63,26 @@ def run_migrations_offline():
def run_migrations_online():
"""Run migrations in 'online' mode.
- In this scenario we need to create an Engine
- and associate a connection with the context.
-
+ In this scenario we need to create an Engine and associate a
+ connection with the context.
"""
+
if not config.initialized:
initialize.initialize_1(context.config.config_file_name)
- alembic_cfg= Config()
+ alembic_cfg = Config()
alembic_cfg.set_main_option(
- "script_location", config.alembic['script_location'])
+ '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)
connection = engine.connect()
- context.configure(
- connection=connection,
- target_metadata=target_metadata
- )
-
- try:
+ with closing(connection):
+ context.configure(
+ connection=connection, target_metadata=Model.metadata)
with context.begin_transaction():
context.run_migrations()
- finally:
- connection.close()
+
if context.is_offline_mode():
run_migrations_offline()
diff --git a/src/mailman/database/base.py b/src/mailman/database/base.py
index 0afdad204..26b6ddbbb 100644
--- a/src/mailman/database/base.py
+++ b/src/mailman/database/base.py
@@ -92,15 +92,15 @@ class SABaseDatabase:
pass
def stamp(self, debug=False):
- """Stamp the database with the latest alembic version.
- """
- # Newly created database don't need to migrations from alembic, since
- # `create_all`` ceates the latest schema. SO patch the database with
- # the latest alembic version to add a entry in alembic_version table.
+ """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", config.alembic['script_location'])
- command.stamp(alembic_cfg, "head")
+ 'script_location', config.database['alembic_scripts'])
+ command.stamp(alembic_cfg, 'head')
def initialize(self, debug=None):
diff --git a/src/mailman/database/postgresql.py b/src/mailman/database/postgresql.py
index ca1068302..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,7 +40,6 @@ class PostgreSQLDatabase(SABaseDatabase):
restart from zero for new tests.
"""
super(PostgreSQLDatabase, self)._post_reset(store)
- from mailman.database.model import Model
tables = reversed(Model.metadata.sorted_tables)
# Recipe adapted from
# http://stackoverflow.com/questions/544791/
diff --git a/src/mailman/database/types.py b/src/mailman/database/types.py
index eec6df6d5..1984b08b5 100644
--- a/src/mailman/database/types.py
+++ b/src/mailman/database/types.py
@@ -29,8 +29,8 @@ __all__ = [
import uuid
from sqlalchemy import Integer
-from sqlalchemy.types import TypeDecorator, CHAR
from sqlalchemy.dialects import postgresql
+from sqlalchemy.types import TypeDecorator, CHAR