Skip to content

Commit e16b5c3

Browse files
docs: document CLI flag changes for prisma db execute and migrate diff in v7 (#7403)
* docs: document --schema and --url removal from prisma db execute * doc: document migrate diff changes in prisma 7
1 parent e748d8d commit e16b5c3

File tree

5 files changed

+243
-58
lines changed

5 files changed

+243
-58
lines changed

content/200-orm/300-prisma-migrate/300-workflows/60-generating-down-migrations.mdx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,37 @@ You will need to create the down migration first, before creating the correspond
105105

106106
There are two potential options for specifying the 'to' state:
107107

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:
109109

110110
```terminal wrap
111+
# Prisma 6
111112
npx prisma migrate diff \
112113
--from-schema prisma/schema.prisma \
113114
--to-migrations prisma/migrations \
114115
--shadow-database-url $SHADOW_DATABASE_URL \
115116
--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
116123
```
117124

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:
119126

120127
```terminal wrap
128+
# Prisma 6
121129
npx prisma migrate diff \
122130
--from-schema prisma/schema.prisma \
123131
--to-schema-datasource prisma/schema.prisma \
124132
--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
125139
```
126140

127141
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
140154

141155
To apply the down migration on your production database after a failed up migration:
142156

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`):
144158

145159
```terminal
146-
npx prisma db execute --file ./down.sql --schema prisma/schema.prisma
160+
npx prisma db execute --file ./down.sql
147161
```
148162

149163
2. Use `migrate resolve` to record that you rolled back the up migration named `add_profile`:

content/200-orm/300-prisma-migrate/300-workflows/70-patching-and-hotfixing.mdx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ The following example demonstrates how to manually complete the steps of a migra
123123

124124
## Fixing failed migrations with `migrate diff` and `db execute`
125125

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+
126132
To help with fixing a failed migration, Prisma ORM provides the following commands for creating and executing a migration file:
127133

128134
- [`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
187193
- Run the following `prisma migrate diff` command:
188194

189195
```terminal wrap
190-
npx prisma migrate diff \
196+
# Prisma 6
197+
npx prisma migrate diff \
191198
--from-url "$DATABASE_URL_PROD" \
192199
--to-migrations ./prisma/migrations \
193200
--shadow-database-url $SHADOW_DATABASE_URL \
194201
--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
195209
```
196210

197211
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).
199212

200213
- Run the following `prisma db execute` command:
201214

202215
```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
204221
```
205222

206223
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
225242
- Run the following `prisma migrate diff` command:
226243

227244
```bash
228-
245+
# Prisma 6
229246
npx prisma migrate diff --from-url "$DATABASE_URL_PROD" --to-schema schema.prisma --script > forward.sql
230247

248+
# Prisma 7 (with production config)
249+
npx prisma migrate diff --from-config-datasource --to-schema schema.prisma --config prisma.config.prod.ts --script > forward.sql
231250
```
232251

233252
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.
234253

235254
- Run the following `prisma db execute` command:
236255

237256
```bash
257+
# Prisma 6
238258
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
239262
```
240263

241264
This applies the changes in the SQL script against the target database without interacting with the migrations table.

0 commit comments

Comments
 (0)