aboutsummaryrefslogtreecommitdiffstats
path: root/moonv4/moon_db/moon_db/db_manager.py
blob: 1c10aa223141c0ea557f5eea7cc083fb4cc3bc2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
"""
"""

import os
import glob
import importlib
from oslo_config import cfg
from oslo_log import log as logging
from sqlalchemy import create_engine
from moon_db.migrate_repo import versions

# Note (asteroide): The next line must be called before the next import
# aka before registering all the options
cfg.CONF.register_cli_opt(cfg.StrOpt('command', positional=True,
                                     help="The command to execute (upgrade, downgrade)"))
from moon_utilities import options  # noqa

LOG = logging.getLogger(__name__)
CONF = cfg.CONF

engine = create_engine(CONF.database.url)


def format_data(ext):
    return ext.name, ext.obj.upgrade()


def run():
    files = glob.glob(versions.__path__[0] + "/[0-9][0-9][0-9]*.py")
    # args = set_options()
    for filename in files:
        filename = os.path.basename(filename).replace(".py", "")
        o = importlib.import_module("moon_db.migrate_repo.versions.{}".format(filename))
        LOG.info("Command is {}".format(CONF.command))
        if CONF.command in ("upgrade", "u", "up"):
            LOG.info("upgrading moon_db.migrate_repo.versions.{}".format(filename))
            o.upgrade(engine)
        elif CONF.command in ("downgrade", "d", "down"):
            LOG.info("downgrading moon_db.migrate_repo.versions.{}".format(filename))
            o.downgrade(engine)
        LOG.info("Done!")