summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--alembic.ini57
-rw-r--r--setup.py1
-rw-r--r--src/mailman/database/alembic/README1
-rw-r--r--src/mailman/database/alembic/env.py72
-rw-r--r--src/mailman/database/alembic/script.py.mako22
-rw-r--r--src/mailman/database/factory.py7
-rw-r--r--src/mailman/testing/layers.py4
7 files changed, 164 insertions, 0 deletions
diff --git a/alembic.ini b/alembic.ini
new file mode 100644
index 000000000..a7247743c
--- /dev/null
+++ b/alembic.ini
@@ -0,0 +1,57 @@
+# A generic, single database configuration.
+
+[alembic]
+# path to migration scripts
+script_location = src/mailman/database/alembic
+
+# template used to generate migration files
+# file_template = %%(rev)s_%%(slug)s
+
+# max length of characters to apply to the
+# "slug" field
+#truncate_slug_length = 40
+
+# set to 'true' to run the environment during
+# the 'revision' command, regardless of autogenerate
+# revision_environment = false
+
+# set to 'true' to allow .pyc and .pyo files without
+# a source .py file to be detected as revisions in the
+# versions/ directory
+# sourceless = false
+
+
+# Logging configuration
+[loggers]
+keys = root,sqlalchemy,alembic
+
+[handlers]
+keys = console
+
+[formatters]
+keys = generic
+
+[logger_root]
+level = WARN
+handlers = console
+qualname =
+
+[logger_sqlalchemy]
+level = WARN
+handlers =
+qualname = sqlalchemy.engine
+
+[logger_alembic]
+level = INFO
+handlers =
+qualname = alembic
+
+[handler_console]
+class = StreamHandler
+args = (sys.stderr,)
+level = NOTSET
+formatter = generic
+
+[formatter_generic]
+format = %(levelname)-5.5s [%(name)s] %(message)s
+datefmt = %H:%M:%S
diff --git a/setup.py b/setup.py
index b4a741f5b..94e9d5488 100644
--- a/setup.py
+++ b/setup.py
@@ -93,6 +93,7 @@ case second `m'. Any other spelling is incorrect.""",
'console_scripts' : list(scripts),
},
install_requires = [
+ 'alembic',
'enum34',
'flufl.bounce',
'flufl.i18n',
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