diff --git a/inspira/cli/cli.py b/inspira/cli/cli.py index 92bc2dc..34e355d 100644 --- a/inspira/cli/cli.py +++ b/inspira/cli/cli.py @@ -58,29 +58,70 @@ def database(name, type): @cli.command() -@click.argument("module_name") +@click.argument("module_name", required=False) @click.option( - "--empty", nargs=1, type=str, required=False, help="Generate empty migration file." + "--empty", nargs=2, type=str, required=False, help="Generate empty migration file." ) +@click.pass_context def createmigrations(module_name, empty): - migration_name = None + """ + Create migrations for the specified module(s). + + :param module_name: Name of the module for which migrations should be created.\n + :param empty: If provided, generate an empty migration file with the specified name. + """ + try: + handle_creations(module_name, empty) + except click.UsageError as e: + click.echo(f"Error: {e}") + click.echo("Use 'createmigrations --help' for usage information.") + + +def handle_creations(module_name, empty): + """ + Handle migration creations based on the provided arguments. + + :param module_name: Name of the module for which migrations should be created. + :param empty: If provided, generate an empty migration file with the specified name. + """ if empty: - migration_name = empty + module_name = empty[0] + migration_name = empty[1] + else: + migration_name = None - create_migrations(module_name, migration_name) + module_names = [module_name] if module_name else get_all_module_names() + + for module_name in module_names: + create_migrations(module_name, migration_name) + + +def handle_migrations(module_name): + """ + Handle migration process based on the provided arguments. + + :param module_name: Name of the module for which migrations should be run. + """ + module_names = [module_name] if module_name else get_all_module_names() + + for current_module_name in module_names: + run_migrations(current_module_name) @cli.command() @click.argument("module_name", required=False) def migrate(module_name): - if module_name: - module_names = [module_name] - else: - module_names = get_all_module_names() - - for module_name in module_names: - run_migrations(module_name) + """ + Run migrations for the specified module(s). + + :param module_name: Name of the module for which migrations should be run. + """ + try: + handle_migrations(module_name) + except Exception as e: + click.echo(f"Error: {e}") + click.echo("Migration failed. Check logs for more details.") @cli.command()