From 32d118329488df775cd74dad2907ed496022f757 Mon Sep 17 00:00:00 2001 From: Abhilash Raj Date: Thu, 25 Sep 2014 00:47:08 +0530 Subject: add new command `mailman migrate` to migrate the new schema on the old database --- src/mailman/commands/cli_migrate.py | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/mailman/commands/cli_migrate.py (limited to 'src/mailman/commands/cli_migrate.py') diff --git a/src/mailman/commands/cli_migrate.py b/src/mailman/commands/cli_migrate.py new file mode 100644 index 000000000..6593ea832 --- /dev/null +++ b/src/mailman/commands/cli_migrate.py @@ -0,0 +1,51 @@ +# Copyright (C) 2010-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 . + +"""bin/mailman migrate.""" + +from __future__ import absolute_import, print_function, unicode_literals + +__metaclass__ = type +__all__ = [ + 'Migrate', +] + +from alembic import command +from alembic.config import Config +from zope.interface import implementer + +from mailman.config import config +from mailman.core.i18n import _ +from mailman.interfaces.command import ICLISubCommand + + +@implementer(ICLISubCommand) +class Migrate: + """Migrate the mailman database to the schema.""" + + name = 'migrate' + + def add(self, parser, comman_parser): + """See `ICLISubCommand`.""" + pass + + def process(self, args): + alembic_cfg= Config() + alembic_cfg.set_main_option( + "script_location", config.alembic['script_location']) + command.upgrade(alembic_cfg, "head") + print("Updated the database schema.") -- cgit v1.2.3-70-g09d2 From 03647b16eb75cc841bb15c3c48ac5f18f77118b8 Mon Sep 17 00:00:00 2001 From: Abhilash Raj Date: Thu, 25 Sep 2014 02:53:09 +0530 Subject: add autogenerate switch that generates to create migration scripts automatically --- src/mailman/commands/cli_migrate.py | 13 ++++++++++--- src/mailman/database/alembic/env.py | 3 +-- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'src/mailman/commands/cli_migrate.py') diff --git a/src/mailman/commands/cli_migrate.py b/src/mailman/commands/cli_migrate.py index 6593ea832..85fd07bd4 100644 --- a/src/mailman/commands/cli_migrate.py +++ b/src/mailman/commands/cli_migrate.py @@ -39,13 +39,20 @@ class Migrate: name = 'migrate' - def add(self, parser, comman_parser): + def add(self, parser, command_parser): """See `ICLISubCommand`.""" + command_parser.add_argument( + '-a', '--autogenerate', + action="store_true", help=_("""\ + Autogenerate the migration script using alembic""")) pass def process(self, args): alembic_cfg= Config() alembic_cfg.set_main_option( "script_location", config.alembic['script_location']) - command.upgrade(alembic_cfg, "head") - print("Updated the database schema.") + if args.autogenerate: + command.revision(alembic_cfg, autogenerate=True) + else: + command.upgrade(alembic_cfg, "head") + print("Updated the database schema.") diff --git a/src/mailman/database/alembic/env.py b/src/mailman/database/alembic/env.py index 002b11aa1..11ea8f6da 100644 --- a/src/mailman/database/alembic/env.py +++ b/src/mailman/database/alembic/env.py @@ -20,7 +20,6 @@ from __future__ import with_statement from alembic import context from alembic.config import Config from sqlalchemy import create_engine, pool -from logging.config import fileConfig from mailman.config import config from mailman.utilities.string import expand @@ -58,9 +57,9 @@ def run_migrations_online(): alembic_cfg= Config() alembic_cfg.set_main_option( "script_location", config.alembic['script_location']) - url = expand(config.database.url, config.paths) engine = create_engine(url) + connection = engine.connect() context.configure( connection=connection, -- cgit v1.2.3-70-g09d2