fix: entrypoint.sh set -e crash, __main__.py PostgreSQL check, cli.py seed placeholders

- entrypoint.sh: use NEEDS_INIT variable instead of exit codes (set -e was killing the script)
- __main__.py: skip db_path.exists() check when using PostgreSQL
- cli.py: use dual-backend placeholders for _seed_context (was using SQLite ? syntax)
- cli.py: use 'admin' instead of 'system' for seed updated_by (FK constraint)
This commit is contained in:
2026-06-25 00:20:54 +00:00
parent 218a05a25c
commit 2fdaaa1356
3 changed files with 42 additions and 15 deletions
+26 -5
View File
@@ -1,11 +1,32 @@
#!/bin/bash #!/bin/bash
set -e set -e
# Initialize if database doesn't exist # Initialize if needed
if [ ! -f "$CTXD_HOME/ctxd.db" ]; then if [ -n "$DATABASE_URL" ]; then
echo "ctxd: initializing database at $CTXD_HOME" # PostgreSQL mode — check if schema exists
python3 -m ctxd init --home "$CTXD_HOME" NEEDS_INIT=$(python3 -c "
import psycopg, os, sys
try:
conn = psycopg.connect(os.environ['DATABASE_URL'])
cur = conn.execute(\"SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname='public' AND tablename='users')\")
if not cur.fetchone()[0]:
print('yes')
else:
print('no')
except Exception:
print('yes')
" 2>/dev/null || echo "yes")
if [ "$NEEDS_INIT" = "yes" ]; then
echo "ctxd: initializing PostgreSQL database"
python3 -m ctxd init --home "$CTXD_HOME"
fi
else
# SQLite mode — check for db file
if [ ! -f "$CTXD_HOME/ctxd.db" ]; then
echo "ctxd: initializing database at $CTXD_HOME"
python3 -m ctxd init --home "$CTXD_HOME"
fi
fi fi
echo "ctxd: starting daemon on 0.0.0.0:9091" echo "ctxd: starting daemon on ${CTXD_HOST:-0.0.0.0}:${CTXD_PORT:-9091}"
exec python3 -m ctxd exec python3 -m ctxd
+7 -2
View File
@@ -4,7 +4,10 @@ import sys
if len(sys.argv) > 1 and sys.argv[1] in ('init', 'serve', 'project-list', 'project-create', if len(sys.argv) > 1 and sys.argv[1] in ('init', 'serve', 'project-list', 'project-create',
'read', 'cat', 'edit', 'search', 'sync', 'audit', 'read', 'cat', 'edit', 'search', 'sync', 'audit',
'user-list', 'user-create', 'import-vault'): 'user-list', 'user-create', 'import-vault',
'user-set-password', 'oauth-client-create',
'oauth-client-list', 'oauth-client-revoke',
'file-list', 'file-read'):
from ctxd.cli import cli_entry from ctxd.cli import cli_entry
cli_entry() cli_entry()
else: else:
@@ -12,7 +15,9 @@ else:
from ctxd.server import serve_sync from ctxd.server import serve_sync
cfg = CtxConfig.from_home() cfg = CtxConfig.from_home()
if not cfg.db_path.exists(): # When using PostgreSQL, the DB is external — skip the file check.
# When using SQLite, verify the db file exists.
if not cfg.use_postgres and not cfg.db_path.exists():
print("Not initialized. Run 'ctx init' first.", file=sys.stderr) print("Not initialized. Run 'ctx init' first.", file=sys.stderr)
sys.exit(1) sys.exit(1)
serve_sync(cfg) serve_sync(cfg)
+9 -8
View File
@@ -64,14 +64,15 @@ Multi-camera remote monitoring system.
def _seed_context(conn): def _seed_context(conn):
"""Insert seed project context with real newlines.""" """Insert seed project context with real newlines."""
conn.execute( from ctxd.db import _is_pg, _ph
"INSERT OR IGNORE INTO project_context (project_id, content, version, updated_by) VALUES (?, ?, 0, 'system')", ph = _ph(conn, 2)
('welcome', _WELCOME_CONTENT), placeholders = ph.split(", ")
) if _is_pg(conn):
conn.execute( sql = f"INSERT INTO project_context (project_id, content, version, updated_by) VALUES ({placeholders[0]}, {placeholders[1]}, 0, 'admin') ON CONFLICT DO NOTHING"
"INSERT OR IGNORE INTO project_context (project_id, content, version, updated_by) VALUES (?, ?, 0, 'system')", else:
('remote-rig', _REMOTE_RIG_CONTENT), sql = f"INSERT OR IGNORE INTO project_context (project_id, content, version, updated_by) VALUES ({placeholders[0]}, {placeholders[1]}, 0, 'admin')"
) conn.execute(sql, ('welcome', _WELCOME_CONTENT))
conn.execute(sql, ('remote-rig', _REMOTE_RIG_CONTENT))
def cmd_init(args): def cmd_init(args):