Skip to content
Snippets Groups Projects
Verified Commit 5c23616d authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

core.cli.db: Allow module_config_key to read db information from

We now have dotted modules whose section in the configuration file no longer match their
name. The `db upgrade` cli had been evolved to deal with it but not the `db init` cli.
parent c5f184ba
No related branches found
No related tags found
1 merge request!384core.cli.db: Allow module_config_key to read db information from
Pipeline #9268 passed
......@@ -140,13 +140,19 @@ def db_init_admin(module: str, dbname: str) -> None:
help="Database flavor.",
default=None,
)
@click.option("--module-config-key", help="Module config key to lookup.", default=None)
@click.pass_context
def db_init(ctx, module, dbname, flavor):
def db_init(ctx, module, dbname, flavor, module_config_key):
"""Initialize a database for the Software Heritage <module>.
The database connection string can come from the --dbname option, or from
the configuration file (see option ``--config-file`` in ``swh db --help``)
in the section named after the MODULE argument.
in the section named after the MODULE argument in most cases.
For the case of the configuration key entry does not match the module name (e.g.
module <storage.proxies.blocking> with a <blocking_admin> configuration key entry),
use the --module-config-key flag to explicit the expected key entry to read the `db`
information from (e.g. --module-config-key=blocking_admin).
Example::
......@@ -175,7 +181,7 @@ def db_init(ctx, module, dbname, flavor):
if dbname is None:
# use the db cnx from the config file; the expected config entry is the
# given module name
cfg = ctx.obj["config"].get(module, {})
cfg = ctx.obj["config"].get(module_config_key or module, {})
dbname = get_dburl_from_config(cfg)
else:
cfg = {"cls": "postgresql", "db": dbname}
......
# Copyright (C) 2019-2022 The Software Heritage developers
# Copyright (C) 2019-2024 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
......@@ -155,12 +155,18 @@ def test_cli_swh_db_initialization_with_env(
swhdb, ["init-admin", module_name, "--dbname", db_params.dbname]
)
assert result.exit_code == 0, f"Unexpected output: {result.output}"
result = cli_runner.invoke(
swhdb, ["init", module_name, "--dbname", db_params.dbname]
swhdb,
[
"init",
module_name,
"--dbname",
db_params.dbname,
],
)
assert result.exit_code == 0, f"Unexpected output: {result.output}"
# the origin values in the scripts uses a hash function (which implementation wise
# uses a function from the pgcrypt extension, init-admin calls installs it)
with BaseDb.connect(postgresql.info.dsn).cursor() as cur:
......@@ -204,8 +210,14 @@ def test_cli_swh_db_initialization_idempotent(
assert len(origins) == 1
@pytest.mark.parametrize("with_module_config_key", [True, False])
def test_cli_swh_db_create_and_init_db_new_api(
cli_runner, postgresql, mock_import_swhmodule, mocker, tmp_path
cli_runner,
postgresql,
mock_import_swhmodule,
mocker,
tmp_path,
with_module_config_key,
):
"""Create a db then initializing it should be ok for a "new style" datastore"""
module_name = "test.cli"
......@@ -217,8 +229,11 @@ def test_cli_swh_db_create_and_init_db_new_api(
cfgfile.write_text(yaml.dump({module_name: {"cls": "postgresql", "db": conninfo}}))
result = cli_runner.invoke(swhdb, ["init-admin", module_name, "--dbname", conninfo])
assert result.exit_code == 0, f"Unexpected output: {result.output}"
result = cli_runner.invoke(swhdb, ["-C", cfgfile, "init", module_name])
cli_cmd = ["-C", cfgfile, "init", module_name]
if with_module_config_key:
cli_cmd.extend(["--module-config-key", module_name])
result = cli_runner.invoke(swhdb, cli_cmd)
assert (
result.exit_code == 0
), f"Unexpected output: {traceback.print_tb(result.exc_info[2])}"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment