2021-10-08 11:19:21 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
# standard imports
|
2021-02-18 06:04:30 +01:00
|
|
|
import os
|
|
|
|
import argparse
|
|
|
|
import logging
|
2021-06-30 16:44:17 +02:00
|
|
|
import re
|
2021-02-18 06:04:30 +01:00
|
|
|
|
2021-10-08 11:19:21 +02:00
|
|
|
# external imports
|
2021-02-18 06:04:30 +01:00
|
|
|
import alembic
|
|
|
|
from alembic.config import Config as AlembicConfig
|
|
|
|
import confini
|
|
|
|
|
2021-10-08 11:19:21 +02:00
|
|
|
# local imports
|
2021-02-18 06:04:30 +01:00
|
|
|
from cic_cache.db import dsn_from_config
|
2021-10-08 11:19:21 +02:00
|
|
|
import cic_cache.cli
|
2021-02-18 06:04:30 +01:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.WARNING)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
# BUG: the dbdir doesn't work after script install
|
2021-11-15 14:07:48 +01:00
|
|
|
rootdir = os.path.dirname(os.path.dirname(os.path.realpath(cic_cache.__file__)))
|
2021-02-18 06:04:30 +01:00
|
|
|
dbdir = os.path.join(rootdir, 'cic_cache', 'db')
|
2021-11-10 09:07:23 +01:00
|
|
|
default_migrations_dir = os.path.join(dbdir, 'migrations')
|
2021-10-29 11:36:14 +02:00
|
|
|
configdir = os.path.join(rootdir, 'cic_cache', 'data', 'config')
|
2021-02-18 06:04:30 +01:00
|
|
|
|
2021-10-08 11:19:21 +02:00
|
|
|
#config_dir = os.path.join('/usr/local/etc/cic-cache')
|
2021-02-18 06:04:30 +01:00
|
|
|
|
2021-10-08 11:19:21 +02:00
|
|
|
arg_flags = cic_cache.cli.argflag_std_base
|
|
|
|
local_arg_flags = cic_cache.cli.argflag_local_sync
|
|
|
|
argparser = cic_cache.cli.ArgumentParser(arg_flags)
|
|
|
|
argparser.process_local_flags(local_arg_flags)
|
2021-06-30 16:44:17 +02:00
|
|
|
argparser.add_argument('--reset', action='store_true', help='downgrade before upgrading')
|
2021-10-08 11:19:21 +02:00
|
|
|
argparser.add_argument('-f', '--force', action='store_true', help='force action')
|
2021-11-10 09:07:23 +01:00
|
|
|
argparser.add_argument('--migrations-dir', dest='migrations_dir', default=default_migrations_dir, type=str, help='migrations directory')
|
2021-02-18 06:04:30 +01:00
|
|
|
args = argparser.parse_args()
|
|
|
|
|
2021-10-08 11:19:21 +02:00
|
|
|
extra_args = {
|
|
|
|
'reset': None,
|
|
|
|
'force': None,
|
|
|
|
'migrations_dir': None,
|
|
|
|
}
|
|
|
|
# process config
|
|
|
|
config = cic_cache.cli.Config.from_args(args, arg_flags, local_arg_flags, extra_args=extra_args)
|
2021-02-18 06:04:30 +01:00
|
|
|
|
2021-11-10 09:07:23 +01:00
|
|
|
migrations_dir = os.path.join(config.get('_MIGRATIONS_DIR'), config.get('DATABASE_ENGINE', 'default'))
|
2021-02-18 06:04:30 +01:00
|
|
|
if not os.path.isdir(migrations_dir):
|
|
|
|
logg.debug('migrations dir for engine {} not found, reverting to default'.format(config.get('DATABASE_ENGINE')))
|
|
|
|
migrations_dir = os.path.join(args.migrations_dir, 'default')
|
|
|
|
|
|
|
|
# connect to database
|
2021-11-10 09:07:23 +01:00
|
|
|
dsn = dsn_from_config(config, 'cic_cache')
|
2021-02-18 06:04:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
logg.info('using migrations dir {}'.format(migrations_dir))
|
|
|
|
logg.info('using db {}'.format(dsn))
|
|
|
|
ac = AlembicConfig(os.path.join(migrations_dir, 'alembic.ini'))
|
|
|
|
ac.set_main_option('sqlalchemy.url', dsn)
|
|
|
|
ac.set_main_option('script_location', migrations_dir)
|
|
|
|
|
2021-06-30 16:44:17 +02:00
|
|
|
if args.reset:
|
|
|
|
if not args.f:
|
|
|
|
if not re.match(r'[yY][eE]?[sS]?', input('EEK! this will DELETE the existing db. are you sure??')):
|
|
|
|
logg.error('user chickened out on requested reset, bailing')
|
|
|
|
sys.exit(1)
|
|
|
|
alembic.command.downgrade(ac, 'base')
|
2021-02-18 06:04:30 +01:00
|
|
|
alembic.command.upgrade(ac, 'head')
|