# ==============================================
# Knex Migrations Container (Optimized)
# ==============================================
# Note: This container needs TypeScript support since migrations are in TS
# It's a short-lived container, so optimization focus is on build speed
FROM node:22-alpine

WORKDIR /app

# Install only needed dependencies
COPY package*.json ./
RUN npm ci && \
    npm cache clean --force

# Copy TypeScript configuration
COPY tsconfig.json ./

# Copy knexfile and migrations
COPY knexfile.ts ./
COPY migrations ./migrations

# Copy source code (needed for imports in migrations)
COPY src ./src

# Set environment to production
ENV NODE_ENV=production

# Create non-root user
RUN addgroup -g 1001 -S nodejs && \
    adduser -S nodejs -u 1001 && \
    chown -R nodejs:nodejs /app

USER nodejs

# Default command runs migrations
CMD ["npx", "knex", "migrate:latest"]
