How to Drizzle Migrate?

How to do migration in drizzle? for prod we should create migrate.ts file instead of drizzle-kit but how then how should we run the .ts file? dockerfile
FROM node:20-bookworm AS base

# Install dependencies only when needed
FROM base AS deps
WORKDIR /app

COPY package.json package-lock.json .npmrc ./
RUN npm ci

# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENV NEXT_TELEMETRY_DISABLED 1

RUN npm run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/package-lock.json ./package-lock.json

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

# Copy migration files and scripts
COPY --from=builder --chown=nextjs:nodejs /app/db ./db
COPY --from=builder --chown=nextjs:nodejs /app/drizzle.config.ts ./drizzle.config.ts
COPY --chown=nextjs:nodejs start.sh /app/start.sh
RUN chmod +x /app/start.sh

USER nextjs
EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

CMD ["/app/start.sh"]
FROM node:20-bookworm AS base

# Install dependencies only when needed
FROM base AS deps
WORKDIR /app

COPY package.json package-lock.json .npmrc ./
RUN npm ci

# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENV NEXT_TELEMETRY_DISABLED 1

RUN npm run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/package-lock.json ./package-lock.json

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

# Copy migration files and scripts
COPY --from=builder --chown=nextjs:nodejs /app/db ./db
COPY --from=builder --chown=nextjs:nodejs /app/drizzle.config.ts ./drizzle.config.ts
COPY --chown=nextjs:nodejs start.sh /app/start.sh
RUN chmod +x /app/start.sh

USER nextjs
EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

CMD ["/app/start.sh"]
start.sh
#!/bin/sh
set -e
npm run migrate
exec node server.js
#!/bin/sh
set -e
npm run migrate
exec node server.js
error
sh: 1: drizzle-kit: not found
Running migrations...

> migrate
> drizzle-kit migrate
sh: 1: drizzle-kit: not found
Running migrations...

> migrate
> drizzle-kit migrate
9 Replies
Kuba
Kuba3mo ago
You cannot run ts files without compiling them first, technically. But there are many tools that allow you to do it with a bit of magic in the background. My favourite is to use tsx for that. I personally setup my programmatic migrations with mjs to avoid this exact compilation problem
tsx
tsx
tsx (TypeScript Execute) - The easiest way to run TypeScript in Node.js
Kuba
Kuba3mo ago
Also it seems that in your case drizzle-kit: not found it is not installed - I would look into whether npm ci installs dev-dependencies or not. I'm assuming that you have drizzle-kit as a dev-dep
0xsh
0xshOP3mo ago
i'd appreciate the response, thanks! could you please explain a bit more about mjs and how can i run the migrate.ts file?
Kuba
Kuba3mo ago
With tsx - you install tsx and just run tsx migrate.ts, as simple as that With mjs, it's just a regular js file that utilizes ESM modules, so you can use import { ... } from "..." as usual the .mjs is just a convention to tell that a given JS file uses ESM-import syntax, instead of node's require Besides that, MJS is just a regular JS file - and since it is, you can run it without compilation step that ts files require
Kuba
Kuba3mo ago
0xsh
0xshOP3mo ago
thank you! i finally made it to work with drizzle-kit but i'm copying the node_modules into the docker which is not ideal because drizzle-kit or tsx are in the node_modules i was wondering if it's possible to just do it in a better (efficient) way
FROM node:latest AS base

FROM base AS deps
WORKDIR /app

COPY package.json package-lock.json .npmrc ./

RUN npm ci

FROM base AS builder
WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENV NEXT_TELEMETRY_DISABLED 1

RUN npm run build

FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

RUN mkdir .next
RUN chown nextjs:nodejs .next

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

COPY --from=builder --chown=nextjs:nodejs /app/db ./db
COPY --from=builder --chown=nextjs:nodejs /app/drizzle.config.ts ./drizzle.config.ts

COPY --from=deps /app/node_modules ./node_modules # any possible way to remove this?

ENV HOME=/home/nextjs
RUN mkdir -p /home/nextjs/.local/share && chown -R nextjs:nodejs /home/nextjs # this is for drizzle-kit, it needs this dir for drizzle studio. definitely a bug from drizzle-kit

RUN echo '#!/bin/sh' > /app/start.sh && \
echo 'set -e' >> /app/start.sh && \
echo 'npm run migrate' >> /app/start.sh && \ # it runs "drizzle-kit migrate" be better if i could just run migrate.ts/.mjs with node
echo 'exec node server.js' >> /app/start.sh && \
chmod +x /app/start.sh

USER nextjs

EXPOSE 3000

ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

CMD ["/app/start.sh"]
FROM node:latest AS base

FROM base AS deps
WORKDIR /app

COPY package.json package-lock.json .npmrc ./

RUN npm ci

FROM base AS builder
WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENV NEXT_TELEMETRY_DISABLED 1

RUN npm run build

FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

RUN mkdir .next
RUN chown nextjs:nodejs .next

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

COPY --from=builder --chown=nextjs:nodejs /app/db ./db
COPY --from=builder --chown=nextjs:nodejs /app/drizzle.config.ts ./drizzle.config.ts

COPY --from=deps /app/node_modules ./node_modules # any possible way to remove this?

ENV HOME=/home/nextjs
RUN mkdir -p /home/nextjs/.local/share && chown -R nextjs:nodejs /home/nextjs # this is for drizzle-kit, it needs this dir for drizzle studio. definitely a bug from drizzle-kit

RUN echo '#!/bin/sh' > /app/start.sh && \
echo 'set -e' >> /app/start.sh && \
echo 'npm run migrate' >> /app/start.sh && \ # it runs "drizzle-kit migrate" be better if i could just run migrate.ts/.mjs with node
echo 'exec node server.js' >> /app/start.sh && \
chmod +x /app/start.sh

USER nextjs

EXPOSE 3000

ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

CMD ["/app/start.sh"]
Kuba
Kuba3mo ago
Move it from dev-dep to normal deps then Ah, I think I misunderstood. Instead of copying full node_modules, you can install these two packages on the stages you need them, I can't really think about any other solution Or, another idea - you can create a one-shot container, that would contain just drizzle-kit, run the migrations from it docker-compose has depends or something similar, so you could utilize that Also, since you're using multi-stage build - you might want to use slimmer base image instead of just node
0xsh
0xshOP3mo ago
thank you mate, i tried to do it with the depends but made it a lot complex so i left it to do full copy node modules for now hopefully i find a better solution
Kuba
Kuba3mo ago
If you ever do - please, let us know - you might help someone who will have similar setup. Good luck!
Want results from more Discord servers?
Add your server