22from typing import Optional
33
44from rich import print
5- from typer import Argument , Typer
5+ from typer import Argument , Option , Typer
66from typing_extensions import Annotated
77
88from .clients import Client , get_client
1111
1212
1313@app .command ()
14- def up (target_migration : Annotated [Optional [str ], Argument ()] = None ) -> None :
14+ def up (
15+ target_migration : Annotated [
16+ Optional [str ],
17+ Argument (help = "Name of the target migration to apply up to" ),
18+ ] = None ,
19+ migrations_path : Annotated [
20+ str ,
21+ Option ("--migrations-path" , "-P" , help = "Path to migrations folder" ),
22+ ] = "migrations" ,
23+ ) -> None :
1524 """Apply migrations up to the target migration or all if no target is provided."""
1625 client = get_client ()
1726 _create_migrations_table_if_not_exists (client )
18- for migration_name in _get_migration_names (target_migration ):
19- _apply_migration (client , migration_name )
27+ for migration_name in _get_migration_names (
28+ target_migration , migrations_path = migrations_path
29+ ):
30+ _apply_migration (client , migration_name , migrations_path = migrations_path )
2031 if target_migration :
2132 print (
2233 f"[bold green]All migrations up to { target_migration } applied successfully[/bold green] :thumbs_up:"
@@ -28,12 +39,21 @@ def up(target_migration: Annotated[Optional[str], Argument()] = None) -> None:
2839
2940
3041@app .command ()
31- def down (target_migration : Annotated [Optional [str ], Argument ()] = None ) -> None :
42+ def down (
43+ target_migration : Annotated [
44+ Optional [str ], Argument (help = "Name of the target migration to revert down to" )
45+ ] = None ,
46+ migrations_path : Annotated [
47+ str , Option ("--migrations-path" , "-P" , help = "Path to migrations folder" )
48+ ] = "migrations" ,
49+ ) -> None :
3250 """Revert migrations down to the target migration or all if no target is provided."""
3351 client = get_client ()
3452 _create_migrations_table_if_not_exists (client )
35- for migration_name in _get_migration_names (target_migration , reverse = True ):
36- _revert_migration (client , migration_name )
53+ for migration_name in _get_migration_names (
54+ target_migration , reverse = True , migrations_path = migrations_path
55+ ):
56+ _revert_migration (client , migration_name , migrations_path = migrations_path )
3757 if target_migration :
3858 print (
3959 f"[bold green]All migrations down to { target_migration } reverted successfully[/bold green] :thumbs_up:"
@@ -44,44 +64,50 @@ def down(target_migration: Annotated[Optional[str], Argument()] = None) -> None:
4464 )
4565
4666
47- def _apply_migration (client : Client , migration_name : str ) -> None :
67+ def _apply_migration (client : Client , migration_name : str , migrations_path : str ) -> None :
4868 if _is_migration_recorded (client , migration_name ):
4969 return
50- client .execute (_get_sql_up_command (migration_name ))
70+ client .execute (_get_sql_up_command (migration_name , migrations_path ))
5171 _record_migration (client , migration_name )
5272 print (f"[green]- { migration_name } [/green] applied successfully" )
5373
5474
55- def _revert_migration (client : Client , migration_name : str ) -> None :
75+ def _revert_migration (
76+ client : Client , migration_name : str , migrations_path : str
77+ ) -> None :
5678 if not _is_migration_recorded (client , migration_name ):
5779 return
58- client .execute (_get_sql_down_command (migration_name ))
80+ client .execute (_get_sql_down_command (migration_name , migrations_path ))
5981 _delete_migration_record (client , migration_name )
6082 print (f"[red]- { migration_name } [/red] reverted successfully" )
6183
6284
6385def _get_migration_names (
64- target_migration : str | None , reverse : bool = False
86+ target_migration : str | None ,
87+ reverse : bool = False ,
88+ migrations_path : str = "migrations" ,
6589) -> list [str ]:
6690 migration_names = sorted (
67- [migration_path .stem for migration_path in Path ("migrations" ).glob ("*.sql" )],
91+ [migration_path .stem for migration_path in Path (migrations_path ).glob ("*.sql" )],
6892 reverse = reverse ,
6993 )
7094 if target_migration :
7195 return migration_names [: migration_names .index (target_migration ) + 1 ]
7296 return migration_names
7397
7498
75- def _get_sql_commands (migration_name : str ) -> list [str ]:
76- return open (f"migrations/{ migration_name } .sql" ).read ().split ("--DOWN" )
99+ def _get_sql_commands (
100+ migration_name : str , migrations_path : str = "migrations"
101+ ) -> list [str ]:
102+ return open (Path (migrations_path ) / f"{ migration_name } .sql" ).read ().split ("--DOWN" )
77103
78104
79- def _get_sql_up_command (migration_name : str ) -> str :
80- return _get_sql_commands (migration_name )[0 ]
105+ def _get_sql_up_command (migration_name : str , migrations_path : str ) -> str :
106+ return _get_sql_commands (migration_name , migrations_path )[0 ]
81107
82108
83- def _get_sql_down_command (migration_name : str ) -> str :
84- return _get_sql_commands (migration_name )[1 ]
109+ def _get_sql_down_command (migration_name : str , migrations_path : str ) -> str :
110+ return _get_sql_commands (migration_name , migrations_path )[1 ]
85111
86112
87113def _record_migration (client : Client , migration_name : str ) -> None :
0 commit comments