#!/bin/sh set -e echo "Starting Market Data Service API container..." # Wait for database to be ready if [ -f /usr/local/bin/wait-for-db.sh ]; then /usr/local/bin/wait-for-db.sh else echo "Waiting for database using pg_isready..." host="${DB_HOST:-db}" port="${DB_PORT:-5432}" user="${DB_USER:-postgres}" max_attempts=30 attempt=0 while [ $attempt -lt $max_attempts ]; do if PGPASSWORD="${DB_PASSWORD}" pg_isready -h "$host" -p "$port" -U "$user" >/dev/null 2>&1; then echo "Database is ready!" break fi attempt=$((attempt + 1)) echo "Waiting for database... ($attempt/$max_attempts)" sleep 2 done if [ $attempt -eq $max_attempts ]; then echo "ERROR: Database failed to become ready" exit 1 fi fi # Sync models first to create tables (if they don't exist) echo "Syncing database models..." node -e " const { sequelize } = require('./src/models'); sequelize.sync({ alter: true, force: false }) .then(() => { console.log('Database models synced successfully.'); process.exit(0); }) .catch((error) => { console.error('Error syncing models:', error); process.exit(1); }); " || { echo "WARNING: Model sync failed, continuing with migrations..." } # Run database migrations (if any exist, excluding archive folder) echo "Running database migrations (if any)..." if [ -d "migrations" ] && [ "$(find migrations -maxdepth 1 -name '*.js' -not -path '*/archive/*' 2>/dev/null | wc -l)" -gt 0 ]; then npx sequelize-cli db:migrate || { echo "WARNING: Database migration failed, but continuing..." } echo "Migrations completed" else echo "No migrations found, skipping migration step. Database schema created by model sync." fi # Start the application echo "Starting Node.js application..." exec "$@"