Minor cleanups.
[mailman.git] / src / mailman / database / tests / test_migrations.py
blob98d8deb7909411e3bff1677736d6415d734d68ec
1 # Copyright (C) 2015 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Test database schema migrations with Alembic"""
20 __all__ = [
21 'TestMigrations',
25 import unittest
26 import alembic.command
27 import sqlalchemy as sa
29 from mailman.config import config
30 from mailman.database.alembic import alembic_cfg
31 from mailman.database.model import Model
32 from mailman.testing.layers import ConfigLayer
36 class TestMigrations(unittest.TestCase):
37 layer = ConfigLayer
39 def setUp(self):
40 alembic.command.stamp(alembic_cfg, 'head')
42 def tearDown(self):
43 # Drop and restore a virgin database.
44 md = sa.MetaData(bind=config.db.engine)
45 md.reflect()
46 md.drop_all()
47 Model.metadata.create_all(config.db.engine)
49 def test_all_migrations(self):
50 script_dir = alembic.script.ScriptDirectory.from_config(alembic_cfg)
51 revisions = [sc.revision for sc in
52 script_dir.walk_revisions('base', 'heads')]
53 for revision in revisions:
54 alembic.command.downgrade(alembic_cfg, revision)
55 revisions.reverse()
56 for revision in revisions:
57 alembic.command.upgrade(alembic_cfg, revision)