83 lines
2.0 KiB
Bash
Executable File
83 lines
2.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
DB_FILE="/opt/appfactory/data/appfactory/appfactory.db"
|
|
CATALOG_FILE="/opt/appfactory/apps/catalog.yml"
|
|
|
|
python3 - "$DB_FILE" "$CATALOG_FILE" <<'PY'
|
|
import sqlite3
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
db_file = Path(sys.argv[1])
|
|
catalog_file = Path(sys.argv[2])
|
|
|
|
if not db_file.exists():
|
|
raise SystemExit(f"Missing database: {db_file}")
|
|
|
|
if not catalog_file.exists():
|
|
raise SystemExit(f"Missing catalog: {catalog_file}")
|
|
|
|
apps = []
|
|
current = None
|
|
|
|
for raw_line in catalog_file.read_text(encoding="utf-8").splitlines():
|
|
line = raw_line.strip()
|
|
|
|
if line.startswith("- id:"):
|
|
if current:
|
|
apps.append(current)
|
|
current = {"id": line.split(":", 1)[1].strip()}
|
|
elif current and ":" in line:
|
|
key, value = line.split(":", 1)
|
|
current[key.strip()] = value.strip().strip('"')
|
|
|
|
if current:
|
|
apps.append(current)
|
|
|
|
con = sqlite3.connect(db_file)
|
|
|
|
for item in apps:
|
|
app_id = item.get("id")
|
|
if not app_id:
|
|
continue
|
|
|
|
con.execute(
|
|
"""
|
|
INSERT INTO apps (
|
|
id, name, language, version, status, memory, cpus
|
|
)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(id) DO UPDATE SET
|
|
name = excluded.name,
|
|
language = excluded.language,
|
|
version = excluded.version,
|
|
status = excluded.status,
|
|
memory = excluded.memory,
|
|
cpus = excluded.cpus,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
""",
|
|
(
|
|
app_id,
|
|
item.get("name") or app_id,
|
|
item.get("language") or "python",
|
|
item.get("version") or "1.0.0",
|
|
item.get("status") or "deployed",
|
|
item.get("memory") or None,
|
|
item.get("cpus") or None,
|
|
),
|
|
)
|
|
|
|
con.commit()
|
|
|
|
rows = con.execute(
|
|
"SELECT id, language, status, COALESCE(memory, ''), COALESCE(cpus, '') FROM apps ORDER BY id"
|
|
).fetchall()
|
|
|
|
print("Migration completed.")
|
|
for row in rows:
|
|
print(" | ".join(row))
|
|
|
|
con.close()
|
|
PY
|