diff --git a/Dockerfile b/Dockerfile index d96a6ff..774fa47 100644 --- a/Dockerfile +++ b/Dockerfile @@ -47,6 +47,7 @@ COPY --from=builder /app/pnpm-lock.yaml ./pnpm-lock.yaml COPY --from=builder /app/next.config.js ./next.config.js COPY --from=builder /app/prisma ./prisma COPY --from=builder /app/prisma.config.ts ./prisma.config.ts +COPY --from=builder /app/scripts ./scripts ENV DATABASE_URL="file:/tmp/build.db" @@ -61,15 +62,8 @@ ENV DATABASE_URL="file:/app/data/dev.db" RUN mkdir -p /app/data /app/public/uploads /app/public/uploads/backgrounds && \ chown -R nextjs:nodejs /app/data /app/public/uploads -RUN echo '#!/bin/sh' > /app/entrypoint.sh && \ - echo 'set -e' >> /app/entrypoint.sh && \ - echo 'mkdir -p /app/data' >> /app/entrypoint.sh && \ - echo 'mkdir -p /app/public/uploads' >> /app/entrypoint.sh && \ - echo 'mkdir -p /app/public/uploads/backgrounds' >> /app/entrypoint.sh && \ - echo 'pnpm dlx prisma migrate deploy || true' >> /app/entrypoint.sh && \ - echo 'exec pnpm start' >> /app/entrypoint.sh && \ - chmod +x /app/entrypoint.sh && \ - chown nextjs:nodejs /app/entrypoint.sh +COPY --chown=nextjs:nodejs scripts/docker-entrypoint.sh /app/entrypoint.sh +RUN chmod +x /app/entrypoint.sh USER nextjs diff --git a/scripts/docker-entrypoint.sh b/scripts/docker-entrypoint.sh new file mode 100755 index 0000000..024fdbb --- /dev/null +++ b/scripts/docker-entrypoint.sh @@ -0,0 +1,58 @@ +#!/bin/sh + +# Create necessary directories +mkdir -p /app/data +mkdir -p /app/public/uploads +mkdir -p /app/public/uploads/backgrounds + +# Function to deploy migrations with automatic failure resolution +deploy_migrations() { + echo "Deploying migrations..." + + # Try to deploy migrations + if pnpm dlx prisma migrate deploy 2>&1 | tee /tmp/migrate.log; then + echo "Migrations deployed successfully" + return 0 + fi + + # Check if error is about failed migrations (P3009) + if ! grep -q "P3009" /tmp/migrate.log && ! grep -q "failed migrations" /tmp/migrate.log; then + echo "Migration error is not about failed migrations" + return 1 + fi + + echo "Found failed migrations, attempting to resolve..." + + # Extract migration name from error (format: migration `20251210120000_add_event_feedback`) + # Using sed instead of grep -P for Alpine Linux compatibility + MIGRATION_NAME=$(grep "migration \`" /tmp/migrate.log | sed -n "s/.*migration \`\([0-9_]*[a-z_]*\)\`.*/\1/p" | head -1) + + if [ -z "$MIGRATION_NAME" ]; then + echo "Could not extract migration name from error" + return 1 + fi + + echo "Resolving failed migration: $MIGRATION_NAME" + + # Try to resolve as applied (migration might have partially succeeded) + if pnpm dlx prisma migrate resolve --applied "$MIGRATION_NAME" 2>&1; then + echo "Migration resolved successfully, retrying deploy..." + # Retry migration deploy + pnpm dlx prisma migrate deploy || { + echo "Migration deploy still failed after resolution, continuing anyway..." + return 0 + } + return 0 + else + echo "Failed to resolve migration, but continuing..." + return 0 + fi +} + +# Deploy migrations (don't fail if it errors - app might still work) +deploy_migrations || { + echo "Migration deployment had issues, but continuing to start app..." +} + +# Start the application +exec pnpm start