From b023fc7c2c0024387a10ca32656b063e33db4c53 Mon Sep 17 00:00:00 2001 From: Bobby <30593201+luciferreeves@users.noreply.github.com> Date: Wed, 11 Mar 2026 00:23:11 +0530 Subject: refactor: remove libsql support and clean up database handling --- scripts/entrypoint.sh | 72 +++++++-------------------------------- scripts/seed.sh | 94 ++++----------------------------------------------- 2 files changed, 18 insertions(+), 148 deletions(-) (limited to 'scripts') diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 2d894c9..6e68488 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -1,68 +1,20 @@ #!/bin/bash set -e -if [ "$SEED" = "true" ]; then - echo "[entrypoint] SEED=true, wiping database..." - if [ "$DB_DRIVER" = "sqlite" ]; then - rm -f "$DSN" - echo "[entrypoint] Deleted $DSN" - elif [ "$DB_DRIVER" = "libsql" ]; then - TURSO_URL=$(echo "$DSN" | sed 's|^libsql://|https://|; s|?.*||') - TURSO_TOKEN=$(echo "$DSN" | sed -n 's/.*authToken=\(.*\)/\1/p') - INDEXES=$(curl -s "${TURSO_URL}/v2/pipeline" \ - -H "Authorization: Bearer ${TURSO_TOKEN}" \ - -H "Content-Type: application/json" \ - -d '{"requests":[{"type":"execute","stmt":{"sql":"SELECT name FROM sqlite_master WHERE type='\''index'\'' AND name NOT LIKE '\''sqlite_%'\''"}},{"type":"close"}]}' \ - | grep -o '"type":"text","value":"[^"]*"' | sed 's/.*"value":"//;s/"//') - TABLES=$(curl -s "${TURSO_URL}/v2/pipeline" \ - -H "Authorization: Bearer ${TURSO_TOKEN}" \ - -H "Content-Type: application/json" \ - -d '{"requests":[{"type":"execute","stmt":{"sql":"SELECT name FROM sqlite_master WHERE type='\''table'\'' AND name NOT LIKE '\''sqlite_%'\'' AND name NOT LIKE '\''_litestream%'\''"}},{"type":"close"}]}' \ - | grep -o '"type":"text","value":"[^"]*"' | sed 's/.*"value":"//;s/"//') - STMTS="" - for INDEX in $INDEXES; do - echo "[entrypoint] Dropping index: $INDEX" - STMTS="${STMTS}{\"type\":\"execute\",\"stmt\":{\"sql\":\"DROP INDEX IF EXISTS ${INDEX}\"}}," - done - for TABLE in $TABLES; do - echo "[entrypoint] Dropping table: $TABLE" - STMTS="${STMTS}{\"type\":\"execute\",\"stmt\":{\"sql\":\"DROP TABLE IF EXISTS ${TABLE}\"}}," - done - if [ -n "$STMTS" ]; then - STMTS="${STMTS%,}" - curl -s "${TURSO_URL}/v2/pipeline" \ - -H "Authorization: Bearer ${TURSO_TOKEN}" \ - -H "Content-Type: application/json" \ - -d "{\"requests\":[${STMTS},{\"type\":\"close\"}]}" > /dev/null - echo "[entrypoint] All indexes and tables dropped" - else - echo "[entrypoint] Nothing to drop" - fi - fi -fi - -echo "[entrypoint] Starting shrine server..." ./shrine & SHRINE_PID=$! -if [ "$SEED" = "true" ]; then - echo "[entrypoint] Waiting for server on port ${PORT:-3000}..." - RETRIES=0 - MAX_RETRIES=30 - until bash -c "echo > /dev/tcp/localhost/${PORT:-3000}" 2>/dev/null; do - RETRIES=$((RETRIES + 1)) - if [ "$RETRIES" -ge "$MAX_RETRIES" ]; then - echo "[entrypoint] Server failed to start after ${MAX_RETRIES}s" - exit 1 - fi - echo "[entrypoint] Waiting... ($RETRIES/$MAX_RETRIES)" - sleep 1 - done - echo "[entrypoint] Server is up, running seed script..." - bash scripts/seed.sh - echo "[entrypoint] Seed complete" -else - echo "[entrypoint] SEED not set, skipping seed" -fi +RETRIES=0 +MAX_RETRIES=30 +until bash -c "echo > /dev/tcp/localhost/${PORT:-3000}" 2>/dev/null; do + RETRIES=$((RETRIES + 1)) + if [ "$RETRIES" -ge "$MAX_RETRIES" ]; then + echo "[entrypoint] Server failed to start after ${MAX_RETRIES}s" + exit 1 + fi + sleep 1 +done + +bash scripts/seed.sh wait $SHRINE_PID \ No newline at end of file diff --git a/scripts/seed.sh b/scripts/seed.sh index 0dcd042..488ac90 100755 --- a/scripts/seed.sh +++ b/scripts/seed.sh @@ -2,87 +2,16 @@ set -euo pipefail SEED_DIR="seed" -DB_DRIVER="${DB_DRIVER:-sqlite}" -DSN="${DSN:-shrine/pagoda.db}" - -if [ "$DB_DRIVER" = "libsql" ]; then - TURSO_URL=$(echo "$DSN" | sed 's|^libsql://|https://|; s|?.*||') - TURSO_TOKEN=$(echo "$DSN" | sed -n 's/.*authToken=\(.*\)/\1/p') -fi +DSN="${DSN:-pagoda.db}" exec_sql_file() { - if [ "$DB_DRIVER" = "sqlite" ]; then - sqlite3 "$DSN" < "$1" - elif [ "$DB_DRIVER" = "libsql" ]; then - local BATCH_SIZE=10 - local STMTS="" - local COUNT=0 - local BATCH_NUM=0 - while IFS= read -r line; do - line=$(echo "$line" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') - [ -z "$line" ] && continue - [ "$line" = "BEGIN TRANSACTION;" ] && continue - [ "$line" = "COMMIT;" ] && continue - local ESCAPED - ESCAPED=$(printf '%s' "$line" | sed 's/\\/\\\\/g; s/"/\\"/g') - STMTS="${STMTS}{\"type\":\"execute\",\"stmt\":{\"sql\":\"${ESCAPED}\"}}," - COUNT=$((COUNT + 1)) - if [ "$COUNT" -ge "$BATCH_SIZE" ]; then - BATCH_NUM=$((BATCH_NUM + 1)) - STMTS="${STMTS%,}" - echo " Sending batch $BATCH_NUM ($BATCH_SIZE statements)..." - RESULT=$(curl -s -w "\n%{http_code}" "${TURSO_URL}/v2/pipeline" \ - -H "Authorization: Bearer ${TURSO_TOKEN}" \ - -H "Content-Type: application/json" \ - -d "{\"requests\":[${STMTS},{\"type\":\"close\"}]}") - HTTP_CODE=$(echo "$RESULT" | tail -1) - if [ "$HTTP_CODE" != "200" ]; then - echo " Turso batch $BATCH_NUM failed with HTTP $HTTP_CODE" - echo "$RESULT" | sed '$d' | head -5 - exit 1 - fi - echo " Batch $BATCH_NUM OK" - STMTS="" - COUNT=0 - fi - done < "$1" - if [ "$COUNT" -gt 0 ]; then - BATCH_NUM=$((BATCH_NUM + 1)) - STMTS="${STMTS%,}" - echo " Sending final batch $BATCH_NUM ($COUNT statements)..." - RESULT=$(curl -s -w "\n%{http_code}" "${TURSO_URL}/v2/pipeline" \ - -H "Authorization: Bearer ${TURSO_TOKEN}" \ - -H "Content-Type: application/json" \ - -d "{\"requests\":[${STMTS},{\"type\":\"close\"}]}") - HTTP_CODE=$(echo "$RESULT" | tail -1) - if [ "$HTTP_CODE" != "200" ]; then - echo " Turso final batch failed with HTTP $HTTP_CODE" - echo "$RESULT" | sed '$d' | head -5 - exit 1 - fi - echo " Batch $BATCH_NUM OK" - fi - echo " All $BATCH_NUM batches sent successfully" - fi + sqlite3 "$DSN" < "$1" } query_sql() { - if [ "$DB_DRIVER" = "sqlite" ]; then - sqlite3 "$DSN" "$1" - elif [ "$DB_DRIVER" = "libsql" ]; then - curl -sf "${TURSO_URL}/v2/pipeline" \ - -H "Authorization: Bearer ${TURSO_TOKEN}" \ - -H "Content-Type: application/json" \ - -d "{\"requests\":[{\"type\":\"execute\",\"stmt\":{\"sql\":\"$1\"}},{\"type\":\"close\"}]}" \ - | sed -n 's/.*"value":"\([^"]*\)".*/\1/p' | head -1 - fi + sqlite3 "$DSN" "$1" } -if [ "$DB_DRIVER" = "sqlite" ] && [ ! -f "$DSN" ]; then - echo "Database not found at $DSN" - exit 1 -fi - EXISTING=$(query_sql "SELECT COUNT(*) FROM users;") if [ -n "$EXISTING" ] && [ "$EXISTING" -gt 0 ] 2>/dev/null; then echo "Database already has $EXISTING users, skipping seed" @@ -226,19 +155,8 @@ echo "BEGIN TRANSACTION;" > "$SQL_FILE" OWNER_BIO_ESC=$(escape_sql "$OWNER_BIO") OWNER_SIG_ESC=$(escape_sql "$OWNER_SIG") -cat >> "$SQL_FILE" << OWNERSQL -INSERT OR IGNORE INTO users ( - username, email, password_hash, display_name, role, email_verified, - jade, honor, pronouns, location, bio, signature, birthday, last_seen_at, - ip, created_at, updated_at -) VALUES ( - 'master', 'master@pagoda.local', '${HASH}', 'Master', 'owner', 1, - 1000, 500, 'sol/solis', 'The Cloud', - '${OWNER_BIO_ESC}', '${OWNER_SIG_ESC}', - '1904-03-15T00:00:00Z', '${OWNER_SEEN}', - '127.0.0.1', '${OWNER_DATE}', '${OWNER_DATE}' -); -OWNERSQL +printf "INSERT OR IGNORE INTO users (username, email, password_hash, display_name, role, email_verified, jade, honor, pronouns, location, bio, signature, birthday, last_seen_at, ip, created_at, updated_at) VALUES ('master', 'master@pagoda.local', '%s', 'Master', 'owner', 1, 1000, 500, 'sol/solis', 'The Cloud', '%s', '%s', '1904-03-15T00:00:00Z', '%s', '127.0.0.1', '%s', '%s');\n" \ + "$HASH" "$OWNER_BIO_ESC" "$OWNER_SIG_ESC" "$OWNER_SEEN" "$OWNER_DATE" "$OWNER_DATE" >> "$SQL_FILE" echo "Generating $CITIZEN_COUNT citizens..." @@ -347,7 +265,7 @@ done echo "COMMIT;" >> "$SQL_FILE" -echo "Inserting into database ($DB_DRIVER)..." +echo "Inserting into database..." exec_sql_file "$SQL_FILE" TOTAL=$(query_sql "SELECT COUNT(*) FROM users;") -- cgit v1.2.3