Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor migration functions for better readability #64

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 88 additions & 12 deletions inspira/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ def new():
@click.option("--only-controller", "only_controller", is_flag=True, required=False)
@click.option("--websocket", "is_websocket", is_flag=True, required=False)
def module(name, only_controller, is_websocket):
"""
Generate files for a new module.

This command takes a required argument 'name' for the module name, and two optional flags:

:param str name: Name of the module.

Optional Flags:
:param bool only_controller: If provided, generates only the controller file.

:param bool is_websocket: If provided, includes WebSocket support in the module.

Example usage:

```\n
inspira new module orders --only-controller --websocket\n
```

This command will generate files for a module named 'orders' with the specified options.
"""
if not name:
click.echo("Please provide a name for the module")
return
Expand Down Expand Up @@ -54,33 +74,89 @@ def create_module_files(name, only_controller, is_websocket):
@click.option("--name", required=True, help="Name of the database.")
@click.option("--type", required=True, help="Database type")
def database(name, type):
"""
Create a new database file with the given name and type.

This command takes two required parameters:

:param str name: Name of the database.

:param str type: Type of the database.

Example usage:
```
inspira new database --name my_database --type sqlite
```

This command will create a new database file named 'my_database' of type 'sqlite'.
"""
create_database_file(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."
)
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

module_names = [module_name] if module_name else get_all_module_names()

for module_name in module_names:
create_migrations(module_name, migration_name)

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