drizzle-team / drizzle-team/drizzle-orm
Support snapshot-less migration generation from current database state
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
I’d like to suggest an alternative migration generation mode: generate a migration by diffing the current database schema against the current Drizzle schema in code, without writing `snapshot.json`.
`drizzle-kit generate` currently depends on checked-in snapshot history. That works, but it adds a lot of noise for large schemas, and it can get awkward when migrations from different PRs are applied out of chronological order (with timestamp prefixes).
I’m proposing an explicit alternative where:
- migrations are still applied exactly as they are today
- only the generation baseline changes
- the baseline becomes current live database state instead of previous Drizzle snapshot state (which might get out of sync with the database state, which means drizzle schema is not guaranteed to match database schema, unlike the proposed approach)
- no `snapshot.json` is written
I think this fits code-first projects using migrations, including DB-branching workflows like Neon.
One thing that would likely matter in this mode: `generate` should probably verify that all existing migrations have already been applied before generating a new one, otherwise the live DB may not be a valid baseline.
I’m currently doing this with a small wrapper around `pushSchema()`:
```ts
import { pushSchema } from "drizzle-kit/api-postgres";
import { drizzle } from "drizzle-orm/neon-http";
import * as schema from "./src/schema";
const db = drizzle(process.env.DATABASE_URL!);
// despite the name, `pushSchema` only creates a diff initially, which then has to be `apply()`
const result = await pushSchema(schema, db);
const preStatements = result.hints
.filter((hint) => typeof hint.statement === "string")
.map((hint) => hint.statement!);
const sqlStatements = [...preStatements, ...result.sqlStatements];
```
Then it writes `sqlStatements` into `migration.sql` and does not call `result.apply()`.
Possible shapes for this approach:
- `drizzle-kit generate --from-db`
- `drizzle-kit generate --baseline=database`
- config in `drizzle.config.ts`
This approach has been used by the ORM-that-shall-not-be-named for years. Drizzle should be able to do it too, just without requiring a JSON sidequest every time I add a column.
Contributor guide
Assessment
This issue has not been assessed yet.