55import sys
66from typing import Any
77import importlib .metadata
8+ from pathlib import Path
89
910import psycopg
1011
@@ -316,6 +317,13 @@ def create_parser() -> argparse.ArgumentParser:
316317
317318 # Parser for the "install" command
318319 parser_install = subparsers .add_parser ("install" , help = "Installs the module." )
320+ parser_install .add_argument (
321+ "-p" ,
322+ "--parameter" ,
323+ nargs = 2 ,
324+ help = "Assign variable for running SQL deltas. Format is name value." ,
325+ action = "append" ,
326+ )
319327
320328 # Parser for the "check" command
321329 parser_check = subparsers .add_parser (
@@ -386,20 +394,13 @@ def create_parser() -> argparse.ArgumentParser:
386394
387395 # Parser for the "upgrade" command
388396 parser_upgrade = subparsers .add_parser ("upgrade" , help = "upgrade db" )
389- parser_upgrade .add_argument ("-t" , "--table" , help = "Upgrades information table" , required = True )
390- parser_upgrade .add_argument (
391- "-d" ,
392- "--dir" ,
393- nargs = "+" ,
394- help = "Delta directories (space-separated)" ,
395- required = True ,
396- )
397+
397398 parser_upgrade .add_argument ("-u" , "--max-version" , help = "upper bound limit version" )
398399 parser_upgrade .add_argument (
399400 "-p" ,
400401 "--parameter" ,
401- nargs = 3 ,
402- help = "Assign variable for running SQL deltas. Format is: (string|float|int) name value." ,
402+ nargs = 2 ,
403+ help = "Assign variable for running SQL deltas. Format is: name value." ,
403404 action = "append" ,
404405 )
405406
@@ -414,32 +415,37 @@ def cli() -> int:
414415 if args .config_file :
415416 config = PumConfig .from_yaml (args .config_file )
416417 else :
417- args_dict = vars (args )
418- config = PumConfig (** args_dict )
418+ config = PumConfig .from_yaml (Path (args .dir ) / ".pum-config.yaml" )
419419
420420 # if no command is passed, print the help and exit
421421 if not args .command :
422422 parser .print_help ()
423423 parser .exit ()
424424
425- # Build variables dict for upgrade/test-and-upgrade commands
426- variables : dict [str , Any ] = {}
427- if args .command in ("upgrade" , "test-and-upgrade" ):
428- for v in args .var or ():
429- if v [0 ] == "float" :
430- variables [v [1 ]] = float (v [2 ])
431- elif v [0 ] == "int" :
432- variables [v [1 ]] = int (v [2 ])
425+ # Build parameters dict for install and upgrade commands
426+ parameters : dict [str , Any ] = {}
427+ parameters_definition = {p ["name" ]: {k : v for k , v in p .items () if k != "name" } for p in config .parameters ()}
428+ if args .command in ("install" , "upgrade" ):
429+ for p in args .parameter or ():
430+ if p [0 ] not in parameters_definition :
431+ print (f"Unknown parameter: { p [0 ]} " )
432+ sys .exit (1 )
433+ if parameters_definition [p [0 ]].get ("type" ) == "float" :
434+ parameters [p [0 ]] = float (p [1 ])
435+ elif parameters_definition [p [0 ]].get ("type" ) == "int" :
436+ parameters [p [0 ]] = int (p [1 ])
433437 else :
434- variables [v [1 ]] = v [2 ]
438+ parameters [p [0 ]] = p [1 ]
439+
440+ logging .debug (f"Parameters: { parameters } " )
435441
436442 pum = Pum (args .pg_service , config )
437443 exit_code = 0
438444
439445 if args .command == "info" :
440446 run_info (args .pg_service , config )
441447 elif args .command == "install" :
442- Upgrader (args .pg_service , config = config , dir = args .dir ).install ()
448+ Upgrader (args .pg_service , config = config , dir = args .dir , parameters = parameters ).install ()
443449 elif args .command == "check" :
444450 success = pum .run_check (
445451 args .pg_service1 ,
@@ -463,7 +469,7 @@ def cli() -> int:
463469 args .pg_service ,
464470 args .table ,
465471 args .dir ,
466- variables ,
472+ parameters ,
467473 args .max_version ,
468474 args .verbose ,
469475 )
0 commit comments