Add portal database layer

This commit is contained in:
AppFactory Bot
2026-05-28 09:44:54 +02:00
parent 182d8d3c4c
commit 3a40eb02a6
3 changed files with 121 additions and 0 deletions
+107
View File
@@ -0,0 +1,107 @@
from app.db.database import get_connection
def get_apps():
con = get_connection()
rows = con.execute(
"""
SELECT
id,
name,
language,
version,
status,
COALESCE(memory, '') AS memory,
COALESCE(cpus, '') AS cpus,
updated_at
FROM apps
ORDER BY id
"""
).fetchall()
con.close()
return [dict(row) for row in rows]
def get_app(app_id: str):
con = get_connection()
row = con.execute(
"""
SELECT
id,
name,
language,
version,
status,
COALESCE(memory, '') AS memory,
COALESCE(cpus, '') AS cpus,
updated_at
FROM apps
WHERE id = ?
""",
(app_id,),
).fetchone()
con.close()
if not row:
return None
return dict(row)
def get_deployments(limit: int = 100):
con = get_connection()
rows = con.execute(
"""
SELECT
id,
app_id,
kind,
status,
started_at,
finished_at,
returncode
FROM deployments
ORDER BY id DESC
LIMIT ?
""",
(limit,),
).fetchall()
con.close()
return [dict(row) for row in rows]
def get_deployment(deployment_id: int):
con = get_connection()
row = con.execute(
"""
SELECT
id,
app_id,
kind,
status,
started_at,
finished_at,
returncode,
stdout,
stderr
FROM deployments
WHERE id = ?
""",
(deployment_id,),
).fetchone()
con.close()
if not row:
return None
return dict(row)
+10
View File
@@ -0,0 +1,10 @@
import sqlite3
from pathlib import Path
DB_FILE = Path("/opt/appfactory/data/appfactory/appfactory.db")
def get_connection():
con = sqlite3.connect(DB_FILE)
con.row_factory = sqlite3.Row
return con