summaryrefslogtreecommitdiff
path: root/src/mailman/database/factory.py
blob: 1976962d3219253feb36155fd7baec6b066cec79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright (C) 2012-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/>.

"""Database factory."""

from __future__ import absolute_import, print_function, unicode_literals

__metaclass__ = type
__all__ = [
    'DatabaseFactory',
    'DatabaseTestingFactory',
    ]


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
from sqlalchemy import MetaData
from zope.interface import implementer
from zope.interface.verify import verifyObject

from mailman.config import config
from mailman.database.model import Model
from mailman.interfaces.database import IDatabase, IDatabaseFactory
from mailman.utilities.modules import call_name



@implementer(IDatabaseFactory)
class DatabaseFactory:
    """Create a new database."""

    @staticmethod
    def create():
        """See `IDatabaseFactory`."""
        with Lock(os.path.join(config.LOCK_DIR, 'dbcreate.lck')):
            database_class = config.database['class']
            database = call_name(database_class)
            verifyObject(IDatabase, database)
            database.initialize()
            schema_mgr = SchemaManager(database)
            schema_mgr.setup_db()
            database.commit()
            return database



class SchemaManager:

    LAST_STORM_SCHEMA_VERSION = '20130406000000'

    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)

    def get_storm_schema_version(self):
        md = MetaData()
        md.reflect(bind=self.database.engine)
        if "version" not in md.tables:
            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()
        return last_version

    def setup_db(self):
        context = MigrationContext.configure(self.database.store.connection())
        current_rev = context.get_current_revision()
        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()
            if storm_version is None:
                # initial DB creation
                Model.metadata.create_all(self.database.engine)
                command.stamp(self.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")
        elif db_rev != head_rev:
            command.upgrade(self.alembic_cfg, "head")
        return head_rev



def _reset(self):
    """See `IDatabase`."""
    # Avoid a circular import at module level.
    from mailman.database.model import Model
    self.store.rollback()
    self._pre_reset(self.store)
    Model._reset(self)
    self._post_reset(self.store)
    self.store.commit()


@implementer(IDatabaseFactory)
class DatabaseTestingFactory:
    """Create a new database for testing."""

    @staticmethod
    def create():
        """See `IDatabaseFactory`."""
        database_class = config.database['class']
        database = call_name(database_class)
        verifyObject(IDatabase, database)
        database.initialize()
        Model.metadata.create_all(database.engine)
        database.commit()
        # Make _reset() a bound method of the database instance.
        database._reset = types.MethodType(_reset, database)
        return database