diff options
Diffstat (limited to 'src/mailman/database')
| -rw-r--r-- | src/mailman/database/alembic/__init__.py | 8 | ||||
| -rw-r--r-- | src/mailman/database/alembic/env.py | 3 | ||||
| -rw-r--r-- | src/mailman/database/alembic/versions/429e08420177_initial.py | 37 | ||||
| -rw-r--r-- | src/mailman/database/base.py | 1 | ||||
| -rw-r--r-- | src/mailman/database/factory.py | 77 |
5 files changed, 73 insertions, 53 deletions
diff --git a/src/mailman/database/alembic/__init__.py b/src/mailman/database/alembic/__init__.py index 73f30832e..ffd3af6df 100644 --- a/src/mailman/database/alembic/__init__.py +++ b/src/mailman/database/alembic/__init__.py @@ -15,18 +15,18 @@ # 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." +"""Alembic configuration initization.""" from __future__ import absolute_import, print_function, unicode_literals __metaclass__ = type __all__ = [ - 'alembic_cfg' -] + 'alembic_cfg', + ] from alembic.config import Config from mailman.utilities.modules import expand_path -alembic_cfg=Config(expand_path("python:mailman.config.alembic")) +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 d1caec58d..1f4722038 100644 --- a/src/mailman/database/alembic/env.py +++ b/src/mailman/database/alembic/env.py @@ -31,16 +31,15 @@ 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.database.alembic import alembic_cfg from mailman.database.model import Model -from mailman.utilities.modules import expand_path from mailman.utilities.string import expand fileConfig(alembic_cfg.config_file_name) + def run_migrations_offline(): """Run migrations in 'offline' mode. diff --git a/src/mailman/database/alembic/versions/429e08420177_initial.py b/src/mailman/database/alembic/versions/429e08420177_initial.py index e8d612676..14f22079b 100644 --- a/src/mailman/database/alembic/versions/429e08420177_initial.py +++ b/src/mailman/database/alembic/versions/429e08420177_initial.py @@ -1,24 +1,45 @@ -"""Initial migration +# 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/>. -This empty migration file makes sure there is always an alembic_version in the -database. As a consequence, if the DB version is reported as None, it means the -database needs to be created from scratch with SQLAlchemy itself. +"""Initial migration. -It also removes the "version" table left over from Storm (if it exists). +This empty migration file makes sure there is always an alembic_version +in the database. As a consequence, if the database version is reported +as None, it means the database needs to be created from scratch with +SQLAlchemy itself. +It also removes the `version` table left over from Storm (if it exists). Revision ID: 429e08420177 Revises: None Create Date: 2014-10-02 10:18:17.333354 - """ -# revision identifiers, used by Alembic. +from __future__ import absolute_import, print_function, unicode_literals + +__metaclass__ = type +__all__ = [ + ] + +# Revision identifiers, used by Alembic. revision = '429e08420177' down_revision = None from alembic import op -import sqlalchemy as sa def upgrade(): diff --git a/src/mailman/database/base.py b/src/mailman/database/base.py index dcaedade0..2f69ed6ad 100644 --- a/src/mailman/database/base.py +++ b/src/mailman/database/base.py @@ -99,7 +99,6 @@ class SABaseDatabase: # alembic_version table. 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 469ed5d18..9cbe1088f 100644 --- a/src/mailman/database/factory.py +++ b/src/mailman/database/factory.py @@ -38,10 +38,14 @@ from zope.interface import implementer 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, expand_path +from mailman.database.model import Model +from mailman.interfaces.database import ( + DatabaseError, IDatabase, IDatabaseFactory) +from mailman.utilities.modules import call_name + + +LAST_STORM_SCHEMA_VERSION = '20130406000000' @@ -57,58 +61,55 @@ class DatabaseFactory: database = call_name(database_class) verifyObject(IDatabase, database) database.initialize() - schema_mgr = SchemaManager(database) - schema_mgr.setup_db() + SchemaManager(database).setup_database() database.commit() return database class SchemaManager: - - LAST_STORM_SCHEMA_VERSION = '20130406000000' + "Manage schema migrations.""" def __init__(self, database): - self.database = database - self.alembic_cfg = alembic_cfg - self.script = ScriptDirectory.from_config(self.alembic_cfg) + self._database = database + self._script = ScriptDirectory.from_config(alembic_cfg) - def get_storm_schema_version(self): - md = MetaData() - md.reflect(bind=self.database.engine) - if "version" not in md.tables: + def _get_storm_schema_version(self): + metadata = MetaData() + metadata.reflect(bind=self._database.engine) + if 'version' not in metadata.tables: + # There are no Storm artifacts left. return None - Version = md.tables["version"] - last_version = self.database.store.query(Version.c.version).filter( - Version.c.component == "schema" - ).order_by(Version.c.version.desc()).first() + Version = metadata.tables['version'] + last_version = self._database.store.query(Version.c.version).filter( + Version.c.component == 'schema' + ).order_by(Version.c.version.desc()).first() return last_version - def setup_db(self): - context = MigrationContext.configure(self.database.store.connection()) + def setup_database(self): + context = MigrationContext.configure(self._database.store.connection()) current_rev = context.get_current_revision() - head_rev = self.script.get_current_head() + head_rev = self._script.get_current_head() if current_rev == head_rev: - return head_rev # already at the latest revision, nothing to do - if current_rev == None: - # no alembic information - storm_version = self.get_storm_schema_version() + # We're already at the latest revision so there's nothing to do. + return head_rev + if current_rev is None: + # No Alembic information is available. + storm_version = self._get_storm_schema_version() if storm_version is None: - # initial DB creation - Model.metadata.create_all(self.database.engine) - command.stamp(self.alembic_cfg, "head") + # Initial database creation. + Model.metadata.create_all(self._database.engine) + command.stamp(alembic_cfg, 'head') else: - # DB from a previous version managed by Storm - if storm_version.version < self.LAST_STORM_SCHEMA_VERSION: - raise RuntimeError( - "Upgrading while skipping beta version is " - "unsupported, please install the previous " - "Mailman beta release") - # Run migrations to remove the Storm-specific table and - # upgrade to SQLAlchemy & Alembic - command.upgrade(self.alembic_cfg, "head") + # The database was previously managed by Storm. + if storm_version.version < LAST_STORM_SCHEMA_VERSION: + raise DatabaseError( + 'Upgrades skipping beta versions is not supported.') + # Run migrations to remove the Storm-specific table and upgrade + # to SQLAlchemy and Alembic. + command.upgrade(alembic_cfg, 'head') elif current_rev != head_rev: - command.upgrade(self.alembic_cfg, "head") + command.upgrade(alembic_cfg, 'head') return head_rev |
