You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/200-orm/300-prisma-migrate/300-workflows/60-generating-down-migrations.mdx
+18-4Lines changed: 18 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -105,23 +105,37 @@ You will need to create the down migration first, before creating the correspond
105
105
106
106
There are two potential options for specifying the 'to' state:
107
107
108
-
- Using `--to-migrations`: this makes a comparison to the state of the migrations given in the migrations directory. This is the preferred option, as it is more robust, but it requires a [shadow database](/orm/prisma-migrate/understanding-prisma-migrate/shadow-database). To use this option, run:
108
+
- Using `--to-migrations`: this makes a comparison to the state of the migrations given in the migrations directory. This is the preferred option, as it is more robust. To use this option, run:
109
109
110
110
```terminal wrap
111
+
# Prisma 6
111
112
npx prisma migrate diff \
112
113
--from-schema prisma/schema.prisma \
113
114
--to-migrations prisma/migrations \
114
115
--shadow-database-url $SHADOW_DATABASE_URL \
115
116
--script > down.sql
117
+
118
+
# Prisma 7
119
+
npx prisma migrate diff \
120
+
--from-schema prisma/schema.prisma \
121
+
--to-migrations prisma/migrations \
122
+
--script > down.sql
116
123
```
117
124
118
-
- Using `--to-schema-datasource`: this makes a comparison to the state of the database. This does not require a shadow database, but it does rely on the database having an up-to-date schema. To use this option, run:
125
+
- Using `--to-config-datasource` (Prisma 7) or `--to-schema-datasource` (Prisma 6): this makes a comparison to the state of the database. This does not require a shadow database, but it does rely on the database having an up-to-date schema. To use this option, run:
119
126
120
127
```terminal wrap
128
+
# Prisma 6
121
129
npx prisma migrate diff \
122
130
--from-schema prisma/schema.prisma \
123
131
--to-schema-datasource prisma/schema.prisma \
124
132
--script > down.sql
133
+
134
+
# Prisma 7
135
+
npx prisma migrate diff \
136
+
--from-schema prisma/schema.prisma \
137
+
--to-config-datasource \
138
+
--script > down.sql
125
139
```
126
140
127
141
3. Generate and apply the up migration with a name of `add_profile`:
@@ -140,10 +154,10 @@ If your previous up migration failed, you can apply your down migration on your
140
154
141
155
To apply the down migration on your production database after a failed up migration:
142
156
143
-
1. Use `db execute` to run your `down.sql` file on the database server:
157
+
1. Use `db execute` to run your `down.sql` file on the database server (using the database URL configured in `prisma.config.ts`):
144
158
145
159
```terminal
146
-
npx prisma db execute --file ./down.sql --schema prisma/schema.prisma
160
+
npx prisma db execute --file ./down.sql
147
161
```
148
162
149
163
2. Use `migrate resolve` to record that you rolled back the up migration named `add_profile`:
Copy file name to clipboardExpand all lines: content/200-orm/300-prisma-migrate/300-workflows/70-patching-and-hotfixing.mdx
+27-4Lines changed: 27 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -123,6 +123,12 @@ The following example demonstrates how to manually complete the steps of a migra
123
123
124
124
## Fixing failed migrations with `migrate diff` and `db execute`
125
125
126
+
:::info
127
+
128
+
**Prisma 7 note**: The `--url` flag has been removed from `prisma db execute`. To run these commands against a production database, you'll need to configure the production database URL in your `prisma.config.ts` file before running `db execute`. You can create a separate config file for production (e.g., `prisma.config.prod.ts`) and use `--config prisma.config.prod.ts` to specify it.
129
+
130
+
:::
131
+
126
132
To help with fixing a failed migration, Prisma ORM provides the following commands for creating and executing a migration file:
127
133
128
134
-[`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) which diffs two database schema sources to create a migration taking one to the state of the second. You can output either a summary of the difference or a sql script. The script can be output into a file via `> file_name.sql` or be piped to the `db execute --stdin` command.
@@ -187,20 +193,31 @@ In this case, you need to create a migration that takes your production database
187
193
- Run the following `prisma migrate diff` command:
188
194
189
195
```terminal wrap
190
-
npx prisma migrate diff \
196
+
# Prisma 6
197
+
npx prisma migrate diff \
191
198
--from-url "$DATABASE_URL_PROD" \
192
199
--to-migrations ./prisma/migrations \
193
200
--shadow-database-url $SHADOW_DATABASE_URL \
194
201
--script > backward.sql
202
+
203
+
# Prisma 7 (with production config)
204
+
npx prisma migrate diff \
205
+
--from-config-datasource \
206
+
--to-migrations ./prisma/migrations \
207
+
--config prisma.config.prod.ts \
208
+
--script > backward.sql
195
209
```
196
210
197
211
This will create a SQL script file containing all changes necessary to take your production environment from its current failed state to the target state defined by your migrations history.
198
-
Note that because we're using `--to-migrations`, the command requires a [shadow database](/orm/prisma-migrate/understanding-prisma-migrate/shadow-database).
199
212
200
213
- Run the following `prisma db execute` command:
201
214
202
215
```bash
203
-
npx prisma db execute --url "$DATABASE_URL_PROD" --file backward.sql
216
+
# Prisma 6
217
+
npx prisma db execute --url "$DATABASE_URL_PROD" --file backward.sql
218
+
219
+
# Prisma 7 (with production config)
220
+
npx prisma db execute --config prisma.config.prod.ts --file backward.sql
204
221
```
205
222
206
223
This applies the changes in the SQL script against the target database without interacting with the migrations table.
@@ -225,17 +242,23 @@ In this case, you need to fix the non-unique data and then go ahead with the res
225
242
- Run the following `prisma migrate diff` command:
This will create a SQL script file containing all changes necessary to take your production environment from its current failed state to the target state defined in your `schema.prisma` file.
234
253
235
254
- Run the following `prisma db execute` command:
236
255
237
256
```bash
257
+
# Prisma 6
238
258
npx prisma db execute --url "$DATABASE_URL_PROD" --file forward.sql
259
+
260
+
# Prisma 7 (with production config)
261
+
npx prisma db execute --config prisma.config.prod.ts --file forward.sql
239
262
```
240
263
241
264
This applies the changes in the SQL script against the target database without interacting with the migrations table.
0 commit comments