#!/bin/bash
# Manually apply Drizzle migrations to test database
# This is a workaround for drizzle-kit migrate not working properly with local PostgreSQL

set -e

echo "Running migrations manually..."

# Get migration files in order
MIGRATION_DIR="server/database/migrations"

if [ ! -d "$MIGRATION_DIR" ]; then
  echo "Error: Migration directory $MIGRATION_DIR not found"
  exit 1
fi

# Run each .sql file in order
for file in "$MIGRATION_DIR"/*.sql; do
  if [ -f "$file" ]; then
    filename=$(basename "$file")
    echo "  → Applying $filename"
    docker exec -i harmonic-test-db psql -U test -d test_db < "$file" 2>&1 | \
      grep -v "already exists" | \
      grep -v "NOTICE" || true
  fi
done

echo "✓ All migrations applied successfully"
