15
15
import asyncio
16
16
import importlib .util
17
17
import logging
18
- import os
19
18
import sys
19
+ from pathlib import Path
20
20
from types import ModuleType
21
+ from typing import Iterable , Optional
21
22
22
23
from configmanager import Config
23
24
@@ -61,6 +62,14 @@ def cli_parse() -> argparse.Namespace:
61
62
type = str ,
62
63
help = "Path to the private key file, if any. Only used to upgrade the key to the latest format." ,
63
64
)
65
+ parser .add_argument (
66
+ "--filter-scripts" ,
67
+ action = "store" ,
68
+ required = False ,
69
+ type = str ,
70
+ help = "A filter for migration scripts. If specified, only the files "
71
+ "matching the provided glob expression will be run." ,
72
+ )
64
73
parser .add_argument (
65
74
"--verbose" ,
66
75
"-v" ,
@@ -94,6 +103,18 @@ def import_module_from_path(path: str) -> ModuleType:
94
103
return migration_module
95
104
96
105
106
+ def list_migration_scripts (
107
+ migrations_dir : Path , glob_expression : Optional [str ]
108
+ ) -> Iterable [Path ]:
109
+ migration_scripts = set (migrations_dir .glob ("*.py" ))
110
+ if glob_expression :
111
+ migration_scripts = migration_scripts & set (
112
+ migrations_dir .glob (glob_expression )
113
+ )
114
+
115
+ return migration_scripts
116
+
117
+
97
118
async def main (args : argparse .Namespace ):
98
119
log_level = logging .DEBUG if args .verbose else logging .INFO
99
120
setup_logging (log_level )
@@ -102,16 +123,16 @@ async def main(args: argparse.Namespace):
102
123
config = init_config (args .config )
103
124
init_db_globals (config = config )
104
125
105
- migration_scripts_dir = os . path . join ( os . path . dirname ( __file__ ), "scripts" )
126
+ migration_scripts_dir = Path ( __file__ ). parent / "scripts"
106
127
migration_scripts = sorted (
107
- f for f in os . listdir (migration_scripts_dir ) if f . endswith ( ".py" )
128
+ list_migration_scripts (migration_scripts_dir , args . filter_scripts )
108
129
)
109
130
110
131
command = args .command
111
132
112
133
for migration_script in migration_scripts :
113
- migration_script_path = os . path . join ( migration_scripts_dir , migration_script )
114
- migration_module = import_module_from_path (migration_script_path )
134
+ migration_script_path = migration_scripts_dir / migration_script
135
+ migration_module = import_module_from_path (str ( migration_script_path ) )
115
136
116
137
if args .verbose :
117
138
LOGGER .info (f"%s: %s" , migration_script , migration_module .__doc__ )
0 commit comments