diff --git a/higgins/cmd/db_manage.py b/higgins/cmd/db_manage.py new file mode 100644 index 000000000..5c67a8c5e --- /dev/null +++ b/higgins/cmd/db_manage.py @@ -0,0 +1,67 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Starter script for higgins-db-manage.""" + +from oslo_config import cfg + +from higgins.db import migration + + +CONF = cfg.CONF + + +def do_version(): + print('Current DB revision is %s' % migration.version()) + + +def do_upgrade(): + migration.upgrade(CONF.command.revision) + + +def do_stamp(): + migration.stamp(CONF.command.revision) + + +def do_revision(): + migration.revision(message=CONF.command.message, + autogenerate=CONF.command.autogenerate) + + +def add_command_parsers(subparsers): + parser = subparsers.add_parser('version') + parser.set_defaults(func=do_version) + + parser = subparsers.add_parser('upgrade') + parser.add_argument('revision', nargs='?') + parser.set_defaults(func=do_upgrade) + + parser = subparsers.add_parser('stamp') + parser.add_argument('revision') + parser.set_defaults(func=do_stamp) + + parser = subparsers.add_parser('revision') + parser.add_argument('-m', '--message') + parser.add_argument('--autogenerate', action='store_true') + parser.set_defaults(func=do_revision) + + +def main(): + command_opt = cfg.SubCommandOpt('command', + title='Command', + help='Available commands', + handler=add_command_parsers) + CONF.register_cli_opt(command_opt) + + CONF(project='higgins') + CONF.command.func() diff --git a/higgins/db/migration.py b/higgins/db/migration.py new file mode 100644 index 000000000..865224525 --- /dev/null +++ b/higgins/db/migration.py @@ -0,0 +1,45 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Database setup and migration commands.""" + +from oslo_config import cfg +from stevedore import driver + +_IMPL = None + + +def get_backend(): + global _IMPL + if not _IMPL: + cfg.CONF.import_opt('backend', 'oslo_db.options', group='database') + _IMPL = driver.DriverManager("higgins.database.migration_backend", + cfg.CONF.database.backend).driver + return _IMPL + + +def upgrade(version=None): + """Migrate the database to `version` or the most recent version.""" + return get_backend().upgrade(version) + + +def version(): + return get_backend().version() + + +def stamp(version): + return get_backend().stamp(version) + + +def revision(message, autogenerate): + return get_backend().revision(message, autogenerate) diff --git a/requirements.txt b/requirements.txt index eed617dc9..87367bace 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,3 +22,4 @@ oslo.db>=4.1.0 # Apache-2.0 six>=1.9.0 # MIT WSME>=0.8 # MIT SQLAlchemy<1.1.0,>=1.0.10 # MIT +stevedore>=1.10.0 # Apache-2.0