Skip to content

Commit

Permalink
Simplified db-migration command for better npm script usage
Browse files Browse the repository at this point in the history
  • Loading branch information
puehringer committed Jan 30, 2020
1 parent 8ad7a70 commit 2cca2d1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 47 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@
"predist:python": "npm run build:python && npm run docs:python",
"prebuild:python": "node -e \"process.exit(process.env.PHOVEA_SKIP_TESTS === undefined?1:0)\" || npm run test:python",
"prebuild:web": "node -e \"process.exit(process.env.PHOVEA_SKIP_TESTS === undefined?1:0)\" || npm run test:web",
"db-migration": "docker-compose run api python phovea_server/__main__.py --env dev db-migration",
"db-migration:list": "npm run db-migration -- list"
"db-migration:base": "docker-compose run api python phovea_server/__main__.py --env dev db-migration",
"db-migration": "npm run db-migration:base -- exec",
"db-migration:list": "npm run db-migration:base -- list"
},
"dependencies": {
"@types/d3": "~3.5.36",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ urllib3==1.25.3
flask-swagger-ui==3.20.9
yamlreader==3.0.4
openpyxl~=2.6.3
alembic==1.3.3
56 changes: 11 additions & 45 deletions tdp_core/dbmigration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import alembic.command
import alembic.config
from os import path
from argparse import ArgumentTypeError, Action, REMAINDER
from argparse import REMAINDER


__author__ = 'Datavisyn'
Expand Down Expand Up @@ -72,7 +72,6 @@ def execute(self, arguments: List[str]) -> bool:

# Parse the options (incl. validation)
options = cmd_parser.parser.parse_args(arguments)
options.raiseerr = True

# Retrieve engine
engine = engines.engine(self.db_key)
Expand Down Expand Up @@ -113,7 +112,7 @@ def __init__(self):
_log.info('DBMigration found: %s', p.id)

# Check if configKey is set, otherwise use the plugin configuration
config = configview(p.configKey) if p.configKey else {}
config = configview(p.configKey) if hasattr(p, 'configKey') else {}

# Priority of assignments: Configuration File -> Plugin Definition
id = config.get('id') or (p.id if hasattr(p, 'id') else None)
Expand Down Expand Up @@ -152,27 +151,6 @@ def migrations(self) -> List[DBMigration]:
db_migration_manager = DBMigrationManager()


def parse_migration(item: str) -> DBMigration:
"""
Parses an argparse value by retrieving the DBMigration instance from the db_migration_manager.
"""
if len(db_migration_manager) == 0:
raise ArgumentTypeError('No migrations available')
if item not in db_migration_manager:
raise ArgumentTypeError('Must be one of the following options: {}'.format(', '.join(db_migration_manager.ids)))
return db_migration_manager[item]


class StoreAllMigrationsAction(Action):
"""
argparse Action which stores all migrations from the db_migration_manager in the destination variable.
"""
def __call__(self, parser, args, values, option_string=None):
if len(db_migration_manager) == 0:
parser.error('No migrations available')
setattr(args, self.dest, db_migration_manager.migrations)


def create_migration_command(parser):
"""
Creates a migration command used by the 'command' extension point.
Expand All @@ -185,20 +163,9 @@ def create_migration_command(parser):

command_parser = subparsers.add_parser('exec', help='Execute command on migration(s)')

selection_parser = command_parser.add_mutually_exclusive_group(required=True)

selection_parser.add_argument('-I', '--id',
dest='migrations',
metavar='<migration-id>',
action='append',
type=parse_migration,
help='ID of the migration defined in the registry')

selection_parser.add_argument('-A', '--all',
dest='migrations',
nargs=0,
action=StoreAllMigrationsAction,
help='Use all migrations defined in the registry')
command_parser.add_argument('id',
choices=db_migration_manager.ids + ['all'],
help='ID of the migration, or all of them')

command_parser.add_argument('command',
nargs=REMAINDER,
Expand All @@ -211,15 +178,14 @@ def execute(args):
else:
print('Available migrations: {}'.format(', '.join(str(migration) for migration in db_migration_manager.migrations)))
elif args.action == 'exec':
# TODO: For some reason, the migrations can only be executed for a single id.
# When using multiple ids, alembic doesn't do anything in the 2nd, 3rd, ... migration.
if len(args.migrations) > 1:
if args.id == 'all':
# TODO: For some reason, the migrations can only be executed for a single id.
# When using multiple ids, alembic doesn't do anything in the 2nd, 3rd, ... migration.
print('Currently, only single migrations are supported. Please execute the command for each migration individually as we are working on a fix.')
return

for migration in args.migrations:
# Using REMAINDER as nargs causes the argument to be be optional, but '+' does not work because it also parses additional --attr with the parser which should actually be ignored.
# Therefore, args.command might be None and we simply pass an empty array as alternative
migration.execute(args.command or [])
# Using REMAINDER as nargs causes the argument to be be optional, but '+' does not work because it also parses additional --attr with the parser which should actually be ignored.
# Therefore, args.command might be empty and we simply pass None to trigger the error message
db_migration_manager[args.id].execute(args.command if len(args.command) > 0 else None)

return lambda args: lambda: execute(args)

0 comments on commit 2cca2d1

Please sign in to comment.