From 1614bb65d23b253487913380c859034a3c304dc1 Mon Sep 17 00:00:00 2001
From: Antoine Lambert <antoine.lambert@inria.fr>
Date: Thu, 13 Jul 2023 14:12:34 +0000
Subject: [PATCH] backend: Move postgresql backend initialization in a separate
 class

Since commit swh/devel/swh-core@89d48572, the "swh db init" command attempts
to instantiate the postgresql backend of a swh module using the get_datastore
function of the module with a "cls" and "db" parameters.

While it works fine for most of swh modules, it does not for the vault as more
parameters are expected by the "get_vault" function and thus the database init
is failing. The issue was spotted in the docker environment after updating the
swh/stack image.

So extract postgresql backend initialization for the vault in a new class named
VaultBackendDataStore and set it as swh.vault.get_datastore attribute value.
---
 swh/vault/__init__.py         |  4 +++-
 swh/vault/backend.py          | 19 ++++++++++++++-----
 swh/vault/cookers/__init__.py |  3 ++-
 3 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/swh/vault/__init__.py b/swh/vault/__init__.py
index 425a5b2..96b9550 100644
--- a/swh/vault/__init__.py
+++ b/swh/vault/__init__.py
@@ -10,6 +10,8 @@ import logging
 from typing import Dict
 import warnings
 
+from swh.vault.backend import VaultDB
+
 logger = logging.getLogger(__name__)
 
 
@@ -57,4 +59,4 @@ def get_vault(cls: str = "remote", **kwargs):
     return Vault(**kwargs)
 
 
-get_datastore = get_vault
+get_datastore = VaultDB
diff --git a/swh/vault/backend.py b/swh/vault/backend.py
index 5c00fa0..a14d349 100644
--- a/swh/vault/backend.py
+++ b/swh/vault/backend.py
@@ -66,18 +66,15 @@ The Software Heritage Developers
 """
 
 
-class VaultBackend:
+class VaultDB:
     """
-    Backend for the Software Heritage Vault.
+    PostgreSQL backend for the Software Heritage Vault.
     """
 
     current_version = 4
 
     def __init__(self, **config):
         self.config = config
-        self.cache = VaultCache(**config["cache"])
-        self.scheduler = get_scheduler(**config["scheduler"])
-        self.storage = get_storage(**config["storage"])
 
         if "db" not in self.config:
             raise ValueError(
@@ -102,6 +99,18 @@ class VaultBackend:
         if db is not self._db:
             db.put_conn()
 
+
+class VaultBackend(VaultDB):
+    """
+    Backend for the Software Heritage Vault.
+    """
+
+    def __init__(self, **config):
+        super().__init__(**config)
+        self.cache = VaultCache(**config["cache"])
+        self.scheduler = get_scheduler(**config["scheduler"])
+        self.storage = get_storage(**config["storage"])
+
     @db_transaction()
     def progress(
         self,
diff --git a/swh/vault/cookers/__init__.py b/swh/vault/cookers/__init__.py
index 9689d96..1053628 100644
--- a/swh/vault/cookers/__init__.py
+++ b/swh/vault/cookers/__init__.py
@@ -12,7 +12,6 @@ from swh.core.config import load_named_config
 from swh.core.config import read as read_config
 from swh.model.swhids import CoreSWHID, ObjectType
 from swh.storage import get_storage
-from swh.vault import get_vault
 from swh.vault.cookers.base import DEFAULT_CONFIG, DEFAULT_CONFIG_PATH, BaseVaultCooker
 from swh.vault.cookers.directory import DirectoryCooker
 from swh.vault.cookers.git_bare import GitBareCooker
@@ -98,6 +97,8 @@ def get_cooker(bundle_type: str, swhid: CoreSWHID):
         EnvironmentError in case the vault configuration reference a non remote class.
 
     """
+    from swh.vault import get_vault
+
     if "SWH_CONFIG_FILENAME" in os.environ:
         cfg = read_config(os.environ["SWH_CONFIG_FILENAME"], DEFAULT_CONFIG)
     else:
-- 
GitLab