database: fix insert_batch()

If a namespace does not exist insert batch must create a new one rather
than raise an exception.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-02-16 17:11:42 -05:00
parent bb39a3c1b2
commit 11e299558d
1 changed files with 4 additions and 1 deletions

View File

@ -355,7 +355,10 @@ class MoonrakerDatabase:
namespace: str, namespace: str,
records: Dict[str, Any] records: Dict[str, Any]
) -> None: ) -> None:
db = self._get_db(namespace) if namespace not in self.namespaces:
self.namespaces[namespace] = self.lmdb_env.open_db(
namespace.encode())
db = self.namespaces[namespace]
with self.lmdb_env.begin(write=True, buffers=True, db=db) as txn: with self.lmdb_env.begin(write=True, buffers=True, db=db) as txn:
for key, val in records.items(): for key, val in records.items():
ret = txn.put(key.encode(), self._encode_value(val)) ret = txn.put(key.encode(), self._encode_value(val))