#!/bin/bash # Bootstrap Directus schema using the directus CLI (schema apply) # This approach bypasses the REST API permission issues entirely set -e DIRECTUS_URL="${DIRECTUS_URL:-http://directus:8055}" DIRECTUS_TOKEN="${DIRECTUS_TOKEN:-}" DIRECTUS_ADMIN_EMAIL="${DIRECTUS_ADMIN_EMAIL:-admin@example.com}" DIRECTUS_ADMIN_PASSWORD="${DIRECTUS_ADMIN_PASSWORD:-change-me}" SNAPSHOT_PATH="/app/directus/schema-snapshot.json" echo "========================================" echo "Directus Bootstrap (CLI-based)" echo "========================================" # Check if snapshot exists if [ ! -f "$SNAPSHOT_PATH" ]; then echo "❌ ERROR: Schema snapshot not found at $SNAPSHOT_PATH" exit 1 fi echo "✓ Found schema snapshot at $SNAPSHOT_PATH" # Determine authentication method if [ -z "$DIRECTUS_TOKEN" ]; then echo "⚠️ DIRECTUS_TOKEN not set; will attempt email/password login" LOGIN_METHOD="email" else echo "✓ DIRECTUS_TOKEN is set; will use bearer token" LOGIN_METHOD="token" fi # Wait for Directus to be fully ready echo "⏳ Waiting for Directus to be ready..." max_retries=30 retry=0 while [ $retry -lt $max_retries ]; do if curl -s "$DIRECTUS_URL/server/health" > /dev/null 2>&1; then echo "✓ Directus is ready" break fi retry=$((retry + 1)) if [ $retry -eq $max_retries ]; then echo "❌ ERROR: Directus did not become ready after ${max_retries} seconds" exit 1 fi sleep 1 done # Apply schema snapshot using directus CLI echo "" echo "📋 Applying schema snapshot..." echo " This will create all collections, fields, and relations." echo "" # The directus CLI is available inside the directus container # We'll use it via npx since directus is installed globally there export DIRECTUS_URL export DIRECTUS_TOKEN export DIRECTUS_ADMIN_EMAIL export DIRECTUS_ADMIN_PASSWORD # Execute the schema apply command # Note: The CLI will prompt for missing credentials if needed if npx directus schema apply "$SNAPSHOT_PATH" --yes 2>&1; then echo "" echo "✅ SUCCESS: Schema snapshot applied successfully!" echo "" echo "Created collections:" echo " - tags" echo " - news" echo " - events" echo " - notices" echo " - fuel_prices" echo " - documents" echo " - contacts" echo " - news_tags (M2M junction)" echo " - events_tags (M2M junction)" echo "" echo "All fields and relations have been initialized." exit 0 else echo "" echo "❌ ERROR: Schema apply failed" exit 1 fi