diff options
| author | Aurélien Bompard | 2014-10-06 15:58:58 +0200 |
|---|---|---|
| committer | Aurélien Bompard | 2014-10-06 15:58:58 +0200 |
| commit | 506b0fff2b4b1f068223d6e9cdfb254fd53bcdac (patch) | |
| tree | d5c4e598fccf67c133179c4d1156252a61c4b8dc /src/mailman/database | |
| parent | cbbac03083357ca928d104d386d9e3008c937581 (diff) | |
| parent | 061799ef5031977bd343bbe54a6ad809138bdb45 (diff) | |
| download | mailman-506b0fff2b4b1f068223d6e9cdfb254fd53bcdac.tar.gz mailman-506b0fff2b4b1f068223d6e9cdfb254fd53bcdac.tar.zst mailman-506b0fff2b4b1f068223d6e9cdfb254fd53bcdac.zip | |
Merge from Abhilash's branch
Diffstat (limited to 'src/mailman/database')
| -rw-r--r-- | src/mailman/database/alembic/__init__.py | 32 | ||||
| -rw-r--r-- | src/mailman/database/alembic/env.py | 68 | ||||
| -rw-r--r-- | src/mailman/database/base.py | 1 | ||||
| -rw-r--r-- | src/mailman/database/factory.py | 13 | ||||
| -rw-r--r-- | src/mailman/database/postgresql.py | 3 | ||||
| -rw-r--r-- | src/mailman/database/tests/test_factory.py | 5 | ||||
| -rw-r--r-- | src/mailman/database/types.py | 2 |
7 files changed, 73 insertions, 51 deletions
diff --git a/src/mailman/database/alembic/__init__.py b/src/mailman/database/alembic/__init__.py index e69de29bb..73f30832e 100644 --- a/src/mailman/database/alembic/__init__.py +++ b/src/mailman/database/alembic/__init__.py @@ -0,0 +1,32 @@ +# Copyright (C) 2014 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/>. + +"Alembic config init." + +from __future__ import absolute_import, print_function, unicode_literals + +__metaclass__ = type +__all__ = [ + 'alembic_cfg' +] + + +from alembic.config import Config +from mailman.utilities.modules import expand_path + + +alembic_cfg=Config(expand_path("python:mailman.config.alembic")) diff --git a/src/mailman/database/alembic/env.py b/src/mailman/database/alembic/env.py index 402c4f7da..d1caec58d 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,40 +15,45 @@ # 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 logging.config import fileConfig +from sqlalchemy import create_engine from mailman.core import initialize from mailman.config import config -from mailman.utilities.string import expand +from mailman.database.alembic import alembic_cfg from mailman.database.model import Model +from mailman.utilities.modules import expand_path +from mailman.utilities.string import expand -target_metadata = Model.metadata +fileConfig(alembic_cfg.config_file_name) + 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) - alembic_cfg= Config() - 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 +61,19 @@ 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.set_main_option( - "script_location", config.alembic['script_location']) - 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 784efcd68..6e86faf04 100644 --- a/src/mailman/database/base.py +++ b/src/mailman/database/base.py @@ -26,7 +26,6 @@ __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 diff --git a/src/mailman/database/factory.py b/src/mailman/database/factory.py index fedfb816f..7d5c2cc73 100644 --- a/src/mailman/database/factory.py +++ b/src/mailman/database/factory.py @@ -30,7 +30,6 @@ import os import types from alembic import command -from alembic.config import Config as AlembicConfig from alembic.migration import MigrationContext from alembic.script import ScriptDirectory from flufl.lock import Lock @@ -40,8 +39,9 @@ from zope.interface.verify import verifyObject from mailman.config import config from mailman.database.model import Model +from mailman.database.alembic import alembic_cfg from mailman.interfaces.database import IDatabase, IDatabaseFactory -from mailman.utilities.modules import call_name +from mailman.utilities.modules import call_name, expand_path @@ -70,10 +70,7 @@ class SchemaManager: def __init__(self, database): self.database = database - self.alembic_cfg = AlembicConfig() - self.alembic_cfg.set_main_option( - "script_location", config.alembic['script_location']) - self.script = ScriptDirectory.from_config(self.alembic_cfg) + self.script = ScriptDirectory.from_config(alembic_cfg) def get_storm_schema_version(self): md = MetaData() @@ -89,10 +86,10 @@ class SchemaManager: def _create(self): # initial DB creation Model.metadata.create_all(self.database.engine) - command.stamp(self.alembic_cfg, "head") + command.stamp(alembic_cfg, "head") def _upgrade(self): - command.upgrade(self.alembic_cfg, "head") + command.upgrade(alembic_cfg, "head") def setup_db(self): context = MigrationContext.configure(self.database.store.connection()) 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/tests/test_factory.py b/src/mailman/database/tests/test_factory.py index 42a05fe1b..81febbde5 100644 --- a/src/mailman/database/tests/test_factory.py +++ b/src/mailman/database/tests/test_factory.py @@ -35,6 +35,7 @@ from mailman.config import config from mailman.testing.layers import DatabaseLayer from mailman.database.factory import SchemaManager, _reset from mailman.database.sqlite import SQLiteDatabase +from mailman.database.alembic import alembic_cfg from mailman.database.model import Model @@ -75,7 +76,7 @@ class TestSchemaManager(unittest.TestCase): def test_current_db(self): """The database is already at the latest version""" - alembic.command.stamp(self.schema_mgr.alembic_cfg, "head") + alembic.command.stamp(alembic_cfg, "head") self.schema_mgr._create = Mock() self.schema_mgr._upgrade = Mock() self.schema_mgr.setup_db() @@ -114,7 +115,7 @@ class TestSchemaManager(unittest.TestCase): def test_old_db(self): """The database is in an old revision, must upgrade""" - alembic.command.stamp(self.schema_mgr.alembic_cfg, "head") + alembic.command.stamp(alembic_cfg, "head") md = MetaData() md.reflect(bind=config.db.engine) config.db.store.execute(md.tables["alembic_version"].delete()) 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 |
