database: add support for the data folder
Deprecate the "database_path" option. If the database does not exist, however the "database_path" does, it will be used as a fallback. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
480ebfac8e
commit
524552eb84
|
@ -5,7 +5,7 @@
|
||||||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import os
|
import pathlib
|
||||||
import json
|
import json
|
||||||
import struct
|
import struct
|
||||||
import operator
|
import operator
|
||||||
|
@ -78,10 +78,29 @@ class MoonrakerDatabase:
|
||||||
self.eventloop = self.server.get_event_loop()
|
self.eventloop = self.server.get_event_loop()
|
||||||
self.namespaces: Dict[str, object] = {}
|
self.namespaces: Dict[str, object] = {}
|
||||||
self.thread_lock = ThreadLock()
|
self.thread_lock = ThreadLock()
|
||||||
self.database_path = os.path.expanduser(config.get(
|
app_args = self.server.get_app_args()
|
||||||
'database_path', "~/.moonraker_database"))
|
dep_path = config.get("database_path", None, deprecate=True)
|
||||||
if not os.path.isdir(self.database_path):
|
db_path = pathlib.Path(app_args["data_path"]).joinpath("database")
|
||||||
os.mkdir(self.database_path)
|
if (
|
||||||
|
app_args["is_default_alias"] and
|
||||||
|
app_args["is_default_data_path"] and
|
||||||
|
not (dep_path is None and db_path.exists())
|
||||||
|
):
|
||||||
|
# Allow configured DB fallback
|
||||||
|
dep_path = dep_path or "~/.moonraker_database"
|
||||||
|
legacy_db = pathlib.Path(dep_path).expanduser().resolve()
|
||||||
|
try:
|
||||||
|
same = legacy_db.samefile(db_path)
|
||||||
|
except Exception:
|
||||||
|
same = False
|
||||||
|
if not same and legacy_db.exists():
|
||||||
|
self.server.add_warning(
|
||||||
|
f"Reverting to legacy database path: {db_path}"
|
||||||
|
)
|
||||||
|
db_path = legacy_db
|
||||||
|
if not db_path.is_dir():
|
||||||
|
db_path.mkdir()
|
||||||
|
self.database_path = str(db_path)
|
||||||
self.lmdb_env = lmdb.open(self.database_path, map_size=MAX_DB_SIZE,
|
self.lmdb_env = lmdb.open(self.database_path, map_size=MAX_DB_SIZE,
|
||||||
max_dbs=MAX_NAMESPACES)
|
max_dbs=MAX_NAMESPACES)
|
||||||
with self.lmdb_env.begin(write=True, buffers=True) as txn:
|
with self.lmdb_env.begin(write=True, buffers=True) as txn:
|
||||||
|
|
Loading…
Reference in New Issue