From c016469507e36daa81acfbc1e8f29f1b2f5a567a Mon Sep 17 00:00:00 2001 From: David Douard <david.douard@sdfa3.org> Date: Tue, 29 Oct 2024 11:24:13 +0100 Subject: [PATCH] db: Fix `swh db create` and `init-admin` commands for 'package:cls' syntax The cls of a module given as 'package:cls' was wrongly hardwired to "postgresql", and the `db create` was not properly parsing the given module for the 'package:cls' syntax, like any other commands do. --- swh/core/cli/db.py | 17 +++++++++++------ swh/core/db/tests/test_cli.py | 15 ++++++++------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/swh/core/cli/db.py b/swh/core/cli/db.py index 23de878..a993408 100755 --- a/swh/core/cli/db.py +++ b/swh/core/cli/db.py @@ -82,12 +82,16 @@ def db_create(ctx, module, dbname, template): """ from swh.core.db.db_utils import create_database_for_package - if dbname is None: - cfg = ctx.obj["config"].get(module, {}) - dbname, cfg = get_dburl_from_config(cfg) + args = handle_cmd_args( + cfg=ctx.obj["config"], + module=module, + do_all=False, + dbname=dbname, + is_path=False, + ) - logger.debug("db_create %s dn_name=%s", module, dbname) - create_database_for_package(module, dbname, template) + for package, fullmodule, backend_class, dbname, cfg in args: + create_database_for_package(fullmodule, dbname, template) @db.command(name="init-admin", context_settings=CONTEXT_SETTINGS) @@ -823,8 +827,9 @@ def handle_cmd_args( cls = "postgresql" dbcfg = {"cls": cls, "db": dbname} fullmodule, backend_class = get_swh_backend_module( - swh_package=module, cls="postgresql" + swh_package=module, cls=cls ) + package = module else: if is_path: # read the db access for module 'module' from the config file diff --git a/swh/core/db/tests/test_cli.py b/swh/core/db/tests/test_cli.py index 9b0399b..49ae4eb 100644 --- a/swh/core/db/tests/test_cli.py +++ b/swh/core/db/tests/test_cli.py @@ -86,15 +86,16 @@ def craft_conninfo(test_db, dbname=None) -> str: return f"postgresql://{db_params.user}@{db_params.host}:{db_params.port}/{dbname}" +@pytest.mark.parametrize( + "module_table", + [("test", "origin"), ("test:postgresql", "origin"), ("test:cli2", "origin2")], +) def test_cli_swh_db_create_and_init_db( - cli_runner, - postgresql, - mock_get_swh_backend_module, + cli_runner, postgresql, mock_get_swh_backend_module, module_table ): """Create a db then initializing it should be ok""" - module_name = "test" - - conninfo = craft_conninfo(postgresql, "new-db") + module_name, table = module_table + conninfo = craft_conninfo(postgresql, f"db-{module_name}") # This creates the db and installs the necessary admin extensions result = cli_runner.invoke(swhdb, ["create", module_name, "--dbname", conninfo]) assert_result(result) @@ -106,7 +107,7 @@ def test_cli_swh_db_create_and_init_db( # the origin value in the scripts uses a hash function (which implementation wise # uses a function from the pgcrypt extension, installed during db creation step) with BaseDb.connect(conninfo).cursor() as cur: - cur.execute("select * from origin") + cur.execute(f"select * from {table}") origins = cur.fetchall() assert len(origins) == 1 -- GitLab