drizzle-team / drizzle-team/drizzle-orm

Enable DDL statement logging during `migrate` command

Open
#3,410 4 comments 15 reactions 0 assignees View on GitHub
drizzle/kit improvement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

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 ({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:
```ts
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!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.