diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/mailman/database/alembic/README | 1 | ||||
| -rw-r--r-- | src/mailman/database/alembic/env.py | 72 | ||||
| -rw-r--r-- | src/mailman/database/alembic/script.py.mako | 22 | ||||
| -rw-r--r-- | src/mailman/database/factory.py | 7 | ||||
| -rw-r--r-- | src/mailman/testing/layers.py | 4 |
5 files changed, 106 insertions, 0 deletions
diff --git a/src/mailman/database/alembic/README b/src/mailman/database/alembic/README new file mode 100644 index 000000000..98e4f9c44 --- /dev/null +++ b/src/mailman/database/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration.
\ No newline at end of file diff --git a/src/mailman/database/alembic/env.py b/src/mailman/database/alembic/env.py new file mode 100644 index 000000000..8c5e0b342 --- /dev/null +++ b/src/mailman/database/alembic/env.py @@ -0,0 +1,72 @@ +from __future__ import with_statement +from alembic import context +from sqlalchemy import create_engine, pool +from logging.config import fileConfig + +from mailman.config import config +from mailman.utilities.string import expand + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +alembic_config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(alembic_config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +target_metadata = None + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +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. + + """ + url = expand(config.database.url, config.paths) + context.configure(url=url, target_metadata=target_metadata) + + with context.begin_transaction(): + context.run_migrations() + + +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. + + """ + url = expand(config.database.url, config.paths) + engine = create_engine(url) + connection = engine.connect() + context.configure( + connection=connection, + target_metadata=target_metadata + ) + + try: + with context.begin_transaction(): + context.run_migrations() + finally: + connection.close() + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/src/mailman/database/alembic/script.py.mako b/src/mailman/database/alembic/script.py.mako new file mode 100644 index 000000000..95702017e --- /dev/null +++ b/src/mailman/database/alembic/script.py.mako @@ -0,0 +1,22 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision} +Create Date: ${create_date} + +""" + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/src/mailman/database/factory.py b/src/mailman/database/factory.py index c06f75031..01a1357dd 100644 --- a/src/mailman/database/factory.py +++ b/src/mailman/database/factory.py @@ -29,6 +29,9 @@ __all__ = [ import os import types +from alembic.config import Config +from alembic import command + from flufl.lock import Lock from zope.interface import implementer from zope.interface.verify import verifyObject @@ -39,6 +42,8 @@ from mailman.interfaces.database import IDatabase, IDatabaseFactory from mailman.utilities.modules import call_name +alembic_cfg = Config("./alembic.ini") + @implementer(IDatabaseFactory) class DatabaseFactory: @@ -53,6 +58,7 @@ class DatabaseFactory: verifyObject(IDatabase, database) database.initialize() Model.metadata.create_all(database.engine) + command.stamp(alembic_cfg, "head") database.commit() return database @@ -81,6 +87,7 @@ class DatabaseTestingFactory: verifyObject(IDatabase, database) database.initialize() Model.metadata.create_all(database.engine) + command.stamp(alembic_cfg, "head") database.commit() # Make _reset() a bound method of the database instance. database._reset = types.MethodType(_reset, database) diff --git a/src/mailman/testing/layers.py b/src/mailman/testing/layers.py index eb51e309f..3d2a50782 100644 --- a/src/mailman/testing/layers.py +++ b/src/mailman/testing/layers.py @@ -190,6 +190,10 @@ class ConfigLayer(MockAndMonkeyLayer): @classmethod def tearDown(cls): assert cls.var_dir is not None, 'Layer not set up' + # Reset the test database after the tests are done so that there + # is no data incase the tests are rerun with a database layer like + # mysql or postgresql which are not deleted in teardown. + reset_the_world() config.pop('test config') shutil.rmtree(cls.var_dir) cls.var_dir = None |
