Updated feature / fix

This commit is contained in:
Niranjan
2026-04-07 03:35:32 +05:30
parent ca6142c39a
commit 3492c441ee
3 changed files with 39 additions and 12 deletions

View File

@@ -1,4 +1,6 @@
"""YakPanel - Database configuration"""
import os
import sqlite3
from pathlib import Path
from sqlalchemy.engine.url import make_url
@@ -22,10 +24,28 @@ def _resolve_database_url(raw_url: str) -> str:
path = (backend_dir / path).resolve()
else:
path = path.resolve()
path.parent.mkdir(parents=True, exist_ok=True)
# POSIX path for SQLite URL (backslashes break aiosqlite on unexpected setups)
posix = path.as_posix()
return str(url.set(database=posix))
data_dir = path.parent
data_dir.mkdir(parents=True, exist_ok=True)
try:
os.chmod(data_dir, 0o755)
except OSError:
pass
if not os.access(data_dir, os.W_OK):
raise RuntimeError(
f"SQLite directory not writable: {data_dir} "
f"(fix ownership or permissions; check SELinux if enabled)."
)
abs_path = path.as_posix()
try:
test = sqlite3.connect(abs_path)
test.close()
except sqlite3.Error as e:
raise RuntimeError(
f"SQLite refused to open {abs_path} (from DATABASE_URL). {e}. "
f"Parent dir: {data_dir} writable={os.access(data_dir, os.W_OK)}."
) from e
# Unix absolute file: sqlite+aiosqlite:////abs/path (four slashes total after the colon)
return f"sqlite+aiosqlite:///{abs_path}"
settings = get_settings()