Migrate: Migrate Up / Down to Specific Migrations

Created on 1 Jul 2020  路  6Comments  路  Source: prisma/migrate

Problem

To get around the imperative migration issue as stated here https://github.com/prisma/migrate/issues/492
I need to run a database migration, then migrate the data, then proceed with another database migration.
If I test this locally there's no way for me to perform this operation in production as migrate up runs all migrations and I can't do the data migration in between.

For example, if I add a non-nullable Author relation to a Post, I need to

  1. Create a nullable migration
model Post {
  id String @default(cuid()) @id
  author Author? @relation(fields: [authorId], references: [id])
  authorId String?
}
  1. Set up the default data
const posts = await prisma.posts.findMany()
for (const post of posts) {
    await prisma.task.update({
    where: { id: post.id },
    data: {
        author: getDefaultAuthor(),
    },
});
  1. Then migrate the field to be nullable.
model Post {
  id String @default(cuid()) @id
  author Author @relation(fields: [authorId], references: [id])
  authorId String
}

If I perform the 3 steps above locally, then wish to do the same in production, it is not possible as migrate up will execute both steps 1 & 3, and I can't execute step 2 in between.

Suggested solutions

For my needs, the solution here would be great https://github.com/prisma/migrate/issues/492

However, if it's quicker to implement a way to migrate or down a specific number of steps or to a specific version. It is possible to work around the current implementation, and I'm sure it would be useful in other circumstances as well.

Migrate up / down 2 steps

prisma migrate up -steps 2
prisma migrate down -steps 2

Or migrate all migration between the current completed migration and the specified id / timestamp

prisma migrate up -id 20200701123310
prisma migrate down -id 20200701123310

All 6 comments

Hi @ryanking1809

Thanks for the feedback :)

So this below already exists and would help as a workaround like you said:

      # Go up by one migration
      prisma migrate up 1 --experimental

      # Go up to a migration by timestamp
      prisma migrate up 20200205204907 --experimental

      Go up to a migration by name
      prisma migrate up "add first_name field" --experimental

This applies for both up & down commands.

I'm closing as this is already implemented 鉁旓笍

Oh great! Sorry I couldn't find it anywhere! Thanks Jolg42 :)

No problem :)

I double checked just in case and the docs page is here https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-cli/command-reference#examples-5

It's also in the help of the CLI for info npx prisma migrate up -h --experimental

Ah I assumed it would've been in https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-migrate

Thanks again!

I will let the team know about that then!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RafaelKr picture RafaelKr  路  3Comments

hamidb80 picture hamidb80  路  4Comments

mariushab picture mariushab  路  3Comments

chanphiromsok picture chanphiromsok  路  3Comments

ff-anthony-soto picture ff-anthony-soto  路  3Comments