Skip to content
This repository was archived by the owner on Aug 25, 2021. It is now read-only.
This repository was archived by the owner on Aug 25, 2021. It is now read-only.

Migrate Up / Down to Specific Migrations #494

@ryanking1809

Description

@ryanking1809

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions