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
24.72k stars 652 forks source link

Enable DDL statement logging during `migrate` command #3410

Open DougSchmidt-Leeward opened 6 months ago

DougSchmidt-Leeward commented 6 months ago

Logging the DDL statements executed during a migration will greatly help troubleshooting, debugging, and manually recovering from a failed migration.

The current behaviour just appears to log the last error received from the database driver. If the error contains some identifiable context, maybe that helps, but if the error is more generic, we are completely lost.

Current behaviour when an error occurs:

I suggest that when config.verbose is false, the migrate command should just log the name of each migration file before its DDL statements are executed. When config.verbose is true, the migrate command should also log each DDL right before it is executed. Then the migration command output should be detailed enough to troubleshoot the problem.

Here is an example error (caused by a manual edit of a migration file which was pushed without a local test)

npx drizzle-kit migrate --config=drizzle.config.ts
drizzle-kit: v0.21.2
drizzle-orm: v0.30.10

Custom config path was provided, using 'drizzle.config.ts'
Reading config file '{redacted}/drizzle.config.ts'
Using 'pg' driver for database querying
[⣷] applying migrations...error: type "bermy" does not exist
    at /{redacted}/node_modules/drizzle-kit/bin.cjs:62266:21
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at <anonymous> ({redacted}/node_modules/src/pg-core/dialect.ts:89:7)
    at NodePgSession.transaction ({redacted}/node_modules/src/node-postgres/session.ts:155:19)
    at PgDialect.migrate ({redacted}/node_modules/src/pg-core/dialect.ts:82:3)
    at migrate ({redacted}/node_modules/src/node-postgres/migrator.ts:10:2) {
  length: 93,
  severity: 'ERROR',
  code: '42704',
  detail: undefined,
  hint: undefined,
  position: '209',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'parse_type.c',
  line: '271',
  routine: 'typenameType'
}

My drizzle.config.ts is:

import 'dotenv/config';
import type { Config } from 'drizzle-kit';

export default {
  schema: './src/db/schema/schema.ts',
  out: './src/db/migrations',
  dialect: 'postgresql',
  dbCredentials: {
    url: process.env.DATABASE_URL as string,
  },
  verbose: true,
  strict: true,
} satisfies Config;

Thanks for the great work so far!

DougSchmidt-Leeward commented 6 months ago

Maybe this is simply achieved (at least the DDL statement logging) by setting the { logger: true } option when creating the db dialect.

AlexBlokh commented 6 months ago

yes, that's a very valid point