fix(deploy): apply drizzle migrations to prod on container startup

The deploy pipeline was building the app image with only `dist/` inside
and rsync'ing only `dist package*.json docker-compose.yml Dockerfile`
to the server. The drizzle migrations folder never reached prod, so
new schema changes (Wave 1 added `tier`, `boosty_verified_at`, and 5
tables) silently went missing. App requests then failed with
"column does not exist" errors at runtime.

Wave-1 CI step `npm run db:migrate` ran against the in-job postgres
service, not against prod — so it never helped.

Changes:
- src/db/migrate.ts -> src/db/migrate.mjs: plain JS so the prod image
  can run it via `node` without devDependencies (no tsx needed).
- Dockerfile: COPY drizzle and src/db/migrate.mjs into the image,
  prepend `node ./src/db/migrate.mjs &&` to the CMD. Container fails
  to start if migrations fail — better than serving with stale schema.
- .github/workflows/deploy.yml: rsync now also sends `drizzle` and
  `src` so the build context on the server has what the Dockerfile
  COPYs reference.
- package.json: `db:migrate` script switched to `node src/db/migrate.mjs`.
- eslint.config.mjs: enable node globals for the migrator script.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
emil
2026-05-16 00:29:32 +03:00
co-authored by Claude Opus 4.7
parent 4b78e9edad
commit e6dec1eb95
5 changed files with 29 additions and 12 deletions
+3 -1
View File
@@ -50,9 +50,11 @@ jobs:
- name: Deploy Node.js app
run: |
# drizzle/ and src/db/migrate.mjs are needed inside the Docker
# build context so the image can apply pending migrations on startup.
rsync -avz --delete \
-e "ssh -i ~/.ssh/deploy_key" \
dist package*.json docker-compose.yml Dockerfile \
dist drizzle src package*.json docker-compose.yml Dockerfile \
${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:~/www/randify.pro/
- name: Restart app
+5 -1
View File
@@ -3,7 +3,11 @@ WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY dist ./dist
COPY drizzle ./drizzle
COPY src/db/migrate.mjs ./src/db/migrate.mjs
EXPOSE 4321
ENV HOST=0.0.0.0
ENV PORT=4321
CMD ["node", "./dist/server/entry.mjs"]
# Run pending migrations on startup, then start the app.
# Fails the container if migrations fail (so we don't serve with a stale schema).
CMD ["sh", "-c", "node ./src/db/migrate.mjs && node ./dist/server/entry.mjs"]
+8
View File
@@ -19,4 +19,12 @@ export default [
"@typescript-eslint/no-unused-vars": "off",
},
},
{
files: ["src/db/migrate.mjs"],
languageOptions: {
globals: {
...globals.node,
},
},
},
];
+1 -1
View File
@@ -17,7 +17,7 @@
"format": "prettier --write .",
"format:check": "prettier --check .",
"db:generate": "drizzle-kit generate",
"db:migrate": "tsx src/db/migrate.ts",
"db:migrate": "node src/db/migrate.mjs",
"db:studio": "drizzle-kit studio"
},
"dependencies": {
+12 -9
View File
@@ -1,21 +1,24 @@
import { drizzle } from 'drizzle-orm/node-postgres';
import { migrate } from 'drizzle-orm/node-postgres/migrator';
import { Pool } from 'pg';
import pg from 'pg';
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
const connectionString = process.env.DATABASE_URL;
if (!connectionString) {
console.error('Migration aborted: DATABASE_URL is not set');
process.exit(1);
}
const pool = new pg.Pool({ connectionString });
const db = drizzle(pool);
async function runMigrations() {
try {
console.log('Running migrations...');
await migrate(db, { migrationsFolder: './drizzle' });
console.log('Migrations completed.');
} catch (err) {
console.error('Migration failed:', err);
await pool.end();
process.exit(1);
}
runMigrations().catch((err) => {
console.error('Migration failed:', err);
process.exit(1);
});
await pool.end();