drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.57k stars 490 forks source link

[BUG]: drizzle-kit migrate doesn't exit 1 on error #2354

Closed cb1kenobi closed 1 month ago

cb1kenobi commented 1 month ago

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.21.2

Describe the Bug

When running drizzle-kit migrate, if the migration fails, the command does not exit with a non-zero exit code. It exits with code 0. This is critical because if migrations fail, we need a non-zero exit code to stop CI from continuing.

In our case, we managed to get the migration to fail when Drizzle attempts to create an index on a column that does not exist. Please note that the error below is not related to this issue.

$ pnpm drizzle-kit migrate
drizzle-kit: v0.21.2
drizzle-orm: v0.30.10

No config path provided, using default path
Reading config file '/Users/user/myproject/drizzle.config.js'
Using 'pg' driver for database querying
[âąż] applying migrations...error: column "type" does not exist
    at /Users/user/myproject/node_modules/.pnpm/drizzle-kit@0.21.2/node_modules/drizzle-kit/bin.cjs:62266:21
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at <anonymous> (/Users/user/myproject/node_modules/.pnpm/drizzle-orm@0.30.10_@neondatabase+serverless@0.9.3_@types+react@18.3.2_pg@8.11.5_react@18.3.1/node_modules/src/pg-core/dialect.ts:89:7)
    at NodePgSession.transaction (/Users/user/myproject/node_modules/.pnpm/drizzle-orm@0.30.10_@neondatabase+serverless@0.9.3_@types+react@18.3.2_pg@8.11.5_react@18.3.1/node_modules/src/node-postgres/session.ts:155:19)
    at PgDialect.migrate (/Users/user/myproject/node_modules/.pnpm/drizzle-orm@0.30.10_@neondatabase+serverless@0.9.3_@types+react@18.3.2_pg@8.11.5_react@18.3.1/node_modules/src/pg-core/dialect.ts:82:3)
    at migrate (/Users/user/myproject/node_modules/.pnpm/drizzle-orm@0.30.10_@neondatabase+serverless@0.9.3_@types+react@18.3.2_pg@8.11.5_react@18.3.1/node_modules/src/node-postgres/migrator.ts:10:2) {
  length: 94,
  severity: 'ERROR',
  code: '42703',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'indexcmds.c',
  line: '1884',
  routine: 'ComputeIndexAttrs'
}
$ echo $?
0

As a workaround, we use a script that calls migrate() and use a catch to exit 1.

Expected behavior

If a migration fails, the command should exit with a non-zero exit code such as 1.

Environment & setup

This issue is reproducible on macOS 14.4.1, Node.js 20.11.0, and using Neon as well as on our GitHub workflow using Ubuntu.

AndriiSherman commented 1 month ago

This should be fixed in drizzle-kit@0.21.3. If the issue persists, feel free to reopen it!

Weixuanf commented 1 month ago

Hi I'm still experiencing this issue on drizzle-kit: v0.22.1 drizzle-orm: v0.31.0

Before I upgrade to 0.22.1, this issue doesn't occur, I was at: drizzle-kit: v0.21.2 drizzle-orm: v0.30.10

After upgrading, this issue happened.

$ npx drizzle-kit migrate
drizzle-kit: v0.22.1
drizzle-orm: v0.31.0

No config path provided, using default path
Reading config file '/Users/weixuan/git/peach-icon/drizzle.config.ts'
Using 'pg' driver for database querying
[⣟] applying migrations...error: column "authorid" does not exist
    at /Users/weixuan/git/peach-icon/node_modules/drizzle-kit/bin.cjs:77692:15
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at <anonymous> (/Users/weixuan/git/peach-icon/node_modules/src/pg-core/dialect.ts:89:7)
    at NodePgSession.transaction (/Users/weixuan/git/peach-icon/node_modules/src/node-postgres/session.ts:155:19)
    at PgDialect.migrate (/Users/weixuan/git/peach-icon/node_modules/src/pg-core/dialect.ts:82:3)
    at migrate (/Users/weixuan/git/peach-icon/node_modules/src/node-postgres/migrator.ts:10:2) {
  length: 98,
  severity: 'ERROR',
  code: '42703',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'indexcmds.c',
  line: '1884',
  routine: 'ComputeIndexAttrs'
}
gggfox commented 1 week ago

Hi I'm also experiencing this issue on drizzle-kit: v0.22.7 drizzle-orm: v0.31.2

  [ 7/10] COPY ./drizzle.config.ts ./ [done: 50ms]
     [ 8/10] RUN npx tsc [done: 1.82s]
     [ 9/10] RUN npx drizzle-kit generate
       → drizzle-kit: v0.22.7
       → drizzle-orm: v0.31.2
       → 
       → No config path provided, using default 'drizzle.config.ts'
       → Reading config file '/usr/src/app/drizzle.config.ts'
       → node:internal/modules/cjs/loader:1212
       →   const err = new Error(message);
       →               ^
       → 
       → Error: Cannot find module './src/main.js'
       → Require stack:
       → - /usr/src/app/drizzle.config.ts
       → - /usr/src/app/node_modules/drizzle-kit/bin.cjs
       →     at Module._resolveFilename (node:internal/modules/cjs/loader:1212:15)
       →     at Module._resolveFilename (/usr/src/app/node_modules/drizzle-kit/bin.cjs:19960:40)
       →     at Module._load (node:internal/modules/cjs/loader:1038:27)
       →     at wrapModuleLoad (node:internal/modules/cjs/loader:212:19)
       →     at Module.require (node:internal/modules/cjs/loader:1297:12)
       →     at require (node:internal/modules/helpers:123:16)
       →     at Object.<anonymous> (/usr/src/app/drizzle.config.ts:3:33)
       →     at Module._compile (node:internal/modules/cjs/loader:1460:14)
       →     at Module._compile (/usr/src/app/node_modules/drizzle-kit/bin.cjs:17464:30)
       →     at Module._extensions..js (node:internal/modules/cjs/loader:1544:10) {
       →   code: 'MODULE_NOT_FOUND',
       →   requireStack: [
       →     '/usr/src/app/drizzle.config.ts',
       →     '/usr/src/app/node_modules/drizzle-kit/bin.cjs'
       →   ]
       → }
       → 
       → Node.js v22.3.0

     ERROR IN: [ 9/10] RUN npx drizzle-kit generate

Build Failed: ImageBuild: process "/bin/sh -c npx drizzle-kit generate" did not complete successfully: exit code: 1
█