11import datetime as dt
2+ import logging
23from pathlib import Path
4+ from typing import Annotated
35
46import typer
57
68from mpt_tool .constants import MIGRATION_FOLDER
9+ from mpt_tool .enums import MigrationTypeEnum
10+ from mpt_tool .errors import RunMigrationError
711from mpt_tool .templates import MIGRATION_SCAFFOLDING_TEMPLATE
12+ from mpt_tool .use_cases import RunMigrationsUseCase
813
914app = typer .Typer (help = "MPT CLI - Migration tool for extensions." , no_args_is_help = True )
1015
@@ -15,26 +20,46 @@ def callback() -> None:
1520
1621
1722@app .command ("migrate" )
18- def migrate (
19- new_data : str | None = typer .Option ( # noqa: WPS404
20- None ,
21- "--new-data" ,
22- metavar = "FILENAME" ,
23- help = "Scaffold a new data migration script with the provided filename." ,
24- ),
25- new_schema : str | None = typer .Option ( # noqa: WPS404
26- None ,
27- "--new-schema" ,
28- metavar = "FILENAME" ,
29- help = "Scaffold a new schema migration script with the provided filename." ,
30- ),
23+ def migrate ( # noqa: C901, WPS238, WPS210, WPS213, WPS231
24+ data : Annotated [bool , typer .Option ("--data" , help = "Run data migrations." )] = False , # noqa: FBT002
25+ schema : Annotated [bool , typer .Option ("--schema" , help = "Run schema migrations." )] = False , # noqa: FBT002
26+ new_data : Annotated [
27+ str | None ,
28+ typer .Option (
29+ "--new-data" ,
30+ metavar = "FILENAME" ,
31+ help = "Scaffold a new data migration script with the provided filename." ,
32+ ),
33+ ] = None ,
34+ new_schema : Annotated [
35+ str | None ,
36+ typer .Option (
37+ "--new-schema" ,
38+ metavar = "FILENAME" ,
39+ help = "Scaffold a new schema migration script with the provided filename." ,
40+ ),
41+ ] = None ,
3142) -> None :
3243 """Migrate command."""
33- if new_data and new_schema :
34- raise typer .BadParameter (
35- "Options --new-data and --new-schema cannot be combined." ,
36- param_hint = "migrate" ,
37- )
44+ options = sum ([bool (data ), bool (schema ), bool (new_data ), bool (new_schema )]) # noqa: WPS221
45+ if options > 1 :
46+ raise typer .BadParameter ("Only one option can be used." , param_hint = "migrate" )
47+ if not options :
48+ raise typer .BadParameter ("At least one option must be used." , param_hint = "migrate" )
49+
50+ if data or schema :
51+ migration_type = MigrationTypeEnum .DATA if data else MigrationTypeEnum .SCHEMA
52+ typer .echo (f"Running { migration_type } migrations..." )
53+
54+ run_migration = RunMigrationsUseCase ()
55+ try :
56+ run_migration .execute (migration_type )
57+ except RunMigrationError as error :
58+ typer .secho (f"Error running migrations: { error !s} " , fg = typer .colors .RED )
59+ raise typer .Abort
60+
61+ typer .secho ("Migrations completed successfully." , fg = typer .colors .GREEN )
62+ return
3863
3964 if new_schema or new_data :
4065 filename_suffix = new_data or new_schema
@@ -58,11 +83,9 @@ def migrate(
5883 ),
5984 )
6085 typer .secho (f"Migration file: { filename } has been created." , fg = typer .colors .GREEN )
61- return
62-
63- typer .secho ("Running migrations is not implemented yet." , fg = typer .colors .YELLOW )
6486
6587
6688def main () -> None :
6789 """Entry point for the CLI."""
90+ logging .basicConfig (level = logging .INFO , format = "%(asctime)s - %(levelname)s - %(message)s" )
6891 app ()
0 commit comments