|
# retoor <retoor@molodetz.nl>
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from devplacepy.config import DATA_DIR, DATABASE_URL
|
|
from devplacepy_services.base.sqlite_broker import SQLiteFileBroker
|
|
|
|
|
|
def _db_path() -> Path:
|
|
# DEVPLACE_DATABASE_URL is the documented override (tests use a dedicated
|
|
# tempfile for isolation - see tests/conftest.py). DATABASE_URL already
|
|
# falls back to DATA_DIR / "devplace.db" when the env var is unset, so
|
|
# deriving the broker's path from it (instead of hardcoding DATA_DIR
|
|
# directly) keeps the broker and every other DEVPLACE_DATABASE_URL
|
|
# consumer (devplacepy/database/core.py) pointed at the same file.
|
|
if DATABASE_URL.startswith("sqlite:///"):
|
|
raw = DATABASE_URL[len("sqlite:///") :]
|
|
if raw and raw != ":memory:":
|
|
return Path(raw)
|
|
return DATA_DIR / "devplace.db"
|
|
|
|
|
|
_broker = SQLiteFileBroker("main", _db_path())
|
|
|
|
|
|
def get_broker() -> SQLiteFileBroker:
|
|
return _broker
|
|
|
|
|
|
async def startup() -> None:
|
|
await _broker.startup()
|
|
from devplacepy_services.database.db_patch import patch_all_db, set_fallback_db
|
|
|
|
patch_all_db()
|
|
set_fallback_db(_broker._write_db)
|
|
os.environ["DEVPLACE_DB_SERVICE"] = "1"
|
|
from devplacepy.database import init_db
|
|
|
|
init_db()
|
|
from devplacepy.database.settings import internal_gateway_key
|
|
|
|
key = (internal_gateway_key() or "").strip()
|
|
if key:
|
|
os.environ["DEVPLACE_GATEWAY_INTERNAL_KEY"] = key
|
|
(DATA_DIR / ".internal_key").write_text(key + "\n")
|
|
|
|
|
|
async def shutdown() -> None:
|
|
await _broker.shutdown() |