drizzle-team / drizzle-team/drizzle-orm
[BUG]: Drizzle migration folder get affected/out of sync!
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Report hasn't been filed before.
- [x] I have verified that the bug I'm about to report hasn't been filed before.
### What version of `drizzle-orm` are you using?
0.42.0
### What version of `drizzle-kit` are you using?
0.31.0
### Other packages
_No response_
### Describe the Bug
Hello team, hope everyone doing well. If anyone can clear following issues with some my questions-
So, I am trying to start new projects with drizzle and neon-postgre.
### **Case 1:**
Very first time, I create a table schema like following-
```
import { pgTable, uuid, text, timestamp, pgEnum } from "drizzle-orm/pg-core";
export const paymentStatus = pgEnum("status", ["PAID", "UNPAID"]);
export const payment = pgTable("payments", {
id: uuid("id").notNull().primaryKey().defaultRandom().unique(),
status: paymentStatus("status").default("UNPAID"),
})
```
and I migrate it successfully by running following two command-
```
npx drizzle-kit generate
npx drizzle-kit migrate
```
And then I create another table schemas, on this new schema, I put pgEnum with same name-
`export const trailStatus = pgEnum("status", ["TRAILED", "SUBSCRIBED"]);`
and then when I run same command for generating and migrating, it generated perfectly, in migrating stage, it just throw an error like already exist.
That's perfect, because I do wrong in my schema. When I realize that did wrong, I just fix the code like renaming the enum.
Steps:
1. After renaming the enum, I just run same command
2. I am getting some unknown error.
3. After sometime later, I just delete the migration folder and create it again, this time migration not worked because the table already exist on that database instance.
4. Then I drop the database instance and create new one, and run same command and this time it successfully migrate.
### **Case 2:**
I create a table like following code-
```
export const users = pgTable("user", {
id: uuid("id").primaryKey().defaultRandom().unique(),
subscribed: text("subscribed").notNull()
})
```
And then I successfully migrate it running following two command-
```
npx drizzle-kit generate
npx drizzle-kit migrate
```
It migrated successfully. But After migrating I realize, I need to use `timestamp` instead `text`
```
export const users = pgTable("user", {
id: uuid("id").primaryKey().defaultRandom().unique(),
subscribed: timestamp("subscribed", { withTimezone: true }).notNull()
})
```
After editing my code, when I run same command, the drizzle folder get affected and just throwing error. By seeing that, I just make my code as previous (I mean change timestamp to text again), but every time I am getting some unknown error(Error is not formatted).
### **Most of time, I see If I get one error by chance, it just keeping throwing error, even I fix the error. Only solve is, deleting drizzle folder and create a new database instance will solve the problem.**
So, I can understand, something was wrong on that migration folder `history`. That's why I need to create refresh migration folder and migrate it to new database instance (Previous database instance not working because, that instance already have some previously migrated table as new migration folder has no history now)
But I don't want to reset database instance or create new database instance because the database can contain real data.
### **Additionally:**
After reading drizzle docs, I get a command for pulling the migration states from database as drizzle keep it save in database-
`npx drizzle-kit pull`
It create a migration folder successfully from database state. But then when I want to migrate this folder to see everything is under sync, it just throwing error like-
`applying migrations...error: relation "payments_id_unique" already exists`
So `pulling` and `migrating` also not working. After researching google and with AI, I get nothing about this use case, even I ask the same question on drizzle discussions, no one pick it.
So, following is my questions if anyone take it please for me-
1. What if I accidentally delete (or lost or it get affected with some error) migration folder?
**Details:** I found a solutions that I need to totally reset database instance or create new database instance and run generate and migrate command again to be working. But it would not be perfect for me, because, my database is production database, exporting and importing to new database is hardworking.
3. How can I get a totally syncing migration folder if I lost it or it broke or it affected by any chance without reseting database instance or creating new database instance?
Following is `drizzle.config.ts`-
```
import { config } from "dotenv";
import { defineConfig } from "drizzle-kit";
config({ path: ".env" });
export default defineConfig({
schema: "./src/schema.ts",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL!,
},
});
```
Contributor guide
Research direction
Start with drizzle.config.ts and src/schema.ts, then reproduce the reported sequence using drizzle-kit generate, migrate, and pull against an existing PostgreSQL database. Compare the generated migration folder and database migration state, including the payments_id_unique error, and define done as a clear diagnosis and safe recovery path without resetting production data.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, postgresql, typescript
- Domain
- databases, tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100