diff --git a/swh/core/cli/db.py b/swh/core/cli/db.py --- a/swh/core/cli/db.py +++ b/swh/core/cli/db.py @@ -79,6 +79,7 @@ """ from swh.core.db.db_utils import create_database_for_package + logger.debug("db_create %s dn_name=%s", module, dbname) create_database_for_package(module, dbname, template) @@ -117,6 +118,7 @@ """ from swh.core.db.db_utils import init_admin_extensions + logger.debug("db_init_admin %s dbname=%s", module, dbname) init_admin_extensions(module, dbname) @@ -128,13 +130,14 @@ "--db-name", "-d", help="Database name.", - default="softwareheritage-dev", - show_default=True, + default=None, + show_default=False, ) @click.option( "--flavor", help="Database flavor.", default=None, ) -def db_init(module, dbname, flavor): +@click.pass_context +def db_init(ctx, module, dbname, flavor): """Initialize a database for the Software Heritage . Example:: @@ -154,6 +157,18 @@ """ from swh.core.db.db_utils import populate_database_for_package + 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, {}) + dbname = get_dburl_from_config(cfg) + + if not dbname: + raise click.BadParameter( + "Missing the postgresql connection configuration. Either fix your " + "configuration file or use the --dbname option." + ) + logger.debug("db_init %s flavor=%s dbname=%s", module, flavor, dbname) initialized, dbversion, dbflavor = populate_database_for_package(