#!/bin/sh
set -e

echo "Running database migrations..."

# Run migrations using the drizzle migration files
node -e "
const { drizzle } = require('drizzle-orm/postgres-js');
const postgres = require('postgres');
const { migrate } = require('drizzle-orm/postgres-js/migrator');

async function runMigrations() {
  const migrationClient = postgres(process.env.DATABASE_URL, { max: 1 });
  const db = drizzle(migrationClient);

  console.log('Starting migration...');
  await migrate(db, { migrationsFolder: './drizzle' });
  console.log('Migration completed successfully!');

  await migrationClient.end();
}

runMigrations().catch((err) => {
  console.error('Migration failed:', err);
  process.exit(1);
});
"

echo "Starting application..."
exec "$@"
