Skip to content

Commit

Permalink
Refactor migration functions for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
cicekhayri committed Jan 14, 2024
1 parent 821d0bf commit 94e2105
Showing 1 changed file with 53 additions and 12 deletions.
65 changes: 53 additions & 12 deletions inspira/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 94e2105

Please sign in to comment.