diff --git a/swh/core/cli/db.py b/swh/core/cli/db.py index 5786348a2c427b0ab293cb42cda58462b1752795..23de878c6628b643c2dde750ceac499ee875829e 100755 --- a/swh/core/cli/db.py +++ b/swh/core/cli/db.py @@ -162,9 +162,9 @@ def db_init_admin( \b $ swh db -C conf.yml init-admin storage - The module can be given as a 'path' in the configuration file where the - configuration entry for the targeted database connection string can be - found. For example:: + With '-p', the module is interpreted as a 'path' in the configuration file + where the configuration entry for the targeted database connection string + can be found. For example:: \b $ cat conf.yml @@ -180,10 +180,7 @@ def db_init_admin( cls: memory \b - $ swh db -C conf.yml init-admin -p storage.steps.2.db - - Warning: the 'path' must target the connection string entry in the config - file. + $ swh db -C conf.yml init-admin -p storage.steps.2 The --all option allows to execute superuser-level initialization steps for all the datasabases found in the config file for @@ -306,10 +303,7 @@ def db_init( $ # to initialize the "main" storage db (expected to be the last element $ # of a pipeline config), $ # or to initialize the masking_db: - $ swh db -C conf.yml init -p storage.steps.0.masking_db - - Note that the 'path' in the configuration file must target the connection - string entry itself. + $ swh db -C conf.yml init -p storage.steps.0 Usage of --module-config-key is now deprecated in favor of "full-path" module/config entry. @@ -732,18 +726,27 @@ def get_dburl_from_config_key(cfg, path): break cfg = cfg[swhmod] - for key_e in cfgpath[:-1]: + for key_e in cfgpath: if isinstance(cfg, list): cfg = cfg[int(key_e)] else: cfg = cfg[key_e] - if isinstance(cfg, list): - dburl = cfg[int(cfgpath[-1])] + assert isinstance(cfg, dict) + if "db" in cfg: + cnxstr = cfg["db"] else: - dburl = cfg[cfgpath[-1]] + # TODO: kill this when possible + for key in cfg: + if key.endswith("_db"): + cnxstr = cfg[key] + break + else: + raise ValueError( + f"no database connection string found in the configuration at {path}" + ) - return swhmod, cfg, dburl + return swhmod, cfg, cnxstr def handle_cmd_args( diff --git a/swh/core/config.py b/swh/core/config.py index 090d6304281502c7ad27cdd11bbd5a862e93740f..4ce88d16ccf517ef61747fc2aafc2ac518c8fb20 100644 --- a/swh/core/config.py +++ b/swh/core/config.py @@ -380,7 +380,7 @@ def list_db_config_entries(cfg) -> Generator[Tuple[str, str, dict, str], None, N if "cls" in cfg: for key, value in cfg.items(): if key == "db" or key.endswith("_db"): - yield f"{path}.{key}", cfg, value + yield path, cfg, value elif isinstance(value, list): for i, subcfg in enumerate(value): yield from look(subcfg, path=f"{path}.{key}.{i}") diff --git a/swh/core/db/tests/test_cli.py b/swh/core/db/tests/test_cli.py index 069bd8ff3f64fd97c4542789e596ef832630419d..dd95d06a7f1204e7b611f04a6319f0dbded7ebfd 100644 --- a/swh/core/db/tests/test_cli.py +++ b/swh/core/db/tests/test_cli.py @@ -457,8 +457,8 @@ test: assert_result(result) else: for config_path in ( - "test.backend.steps.0.db", - "test.backend.steps.1.backend.cli_db", + "test.backend.steps.0", + "test.backend.steps.1.backend", ): result = cli_runner.invoke( swhdb, ["-C", cfgfile, "init-admin", "-p", config_path] @@ -517,8 +517,8 @@ test: assert ( result.output == f"""\ -test.backend.steps.0.db cli {conninfo} -test.backend.steps.1.backend.cli_db cli2 {conninfo2} +test.backend.steps.0 cli {conninfo} +test.backend.steps.1.backend cli2 {conninfo2} """ ) @@ -549,7 +549,7 @@ test: - cls: cli backend: cls: cli2 - cli_db: {conninfo2} + db: {conninfo2} """ ) result = cli_runner.invoke(swhdb, ["-C", cfgfile, "init-admin", "-a", "test"]) @@ -564,7 +564,7 @@ test: # but we can ask for each entry result = cli_runner.invoke( - swhdb, ["-C", cfgfile, "version", "-p", "test.backend.steps.0.db"] + swhdb, ["-C", cfgfile, "version", "-p", "test.backend.steps.0"] ) assert_result(result) assert ( @@ -578,7 +578,7 @@ version: 3 ) result = cli_runner.invoke( - swhdb, ["-C", cfgfile, "version", "-p", "test.backend.steps.1.backend.cli_db"] + swhdb, ["-C", cfgfile, "version", "-p", "test.backend.steps.1.backend"] ) assert_result(result) assert ( @@ -652,8 +652,8 @@ test: assert swh_db_version(conninfo2) == 1 for module_name, config_path, cnxstr in ( - ("test:cli", "test.backend.steps.0.db", conninfo), - ("test:cli2", "test.backend.steps.1.backend.cli_db", conninfo2), + ("test:cli", "test.backend.steps.0", conninfo), + ("test:cli2", "test.backend.steps.1.backend", conninfo2), ): current_version = 1 # XXX hack hack hack: change the current test (pytest.)marker's @@ -763,7 +763,7 @@ test: - cls: cli backend: cls: cli2 - cli_db: {conninfo2} + db: {conninfo2} """ )