Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable DDL statement logging during migrate command #418

Open
DougSchmidt-Leeward opened this issue May 17, 2024 · 2 comments
Open

Enable DDL statement logging during migrate command #418

DougSchmidt-Leeward opened this issue May 17, 2024 · 2 comments

Comments

@DougSchmidt-Leeward
Copy link

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:

  • We don't know which migration files were executed (ie. 0012_funky_horse_calipers.sql worked but 0013_sad_panda_noises.sql failed)
  • We don't know which DDL statement within a migration fail caused the error.

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
Copy link
Author

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

@AlexBlokh
Copy link
Contributor

yes, that's a very valid point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants