IssueRaiser

Imperative Migrations with a TypeScript DSL

## Problem Not all migrations can be declarative. For example, we need to add a custom index for performance reasons. We also need this as an escape hatch for anything prisma schema doesn't support. Lastly, we also need this for data migrations. This is very important for any serious apps using prisma migrate. ## Suggested solution A new command: `prisma migrate create` This will create the necessary migration files. Similar as `prisma migrate save` but the files are just stubbed out. Then the user will open the stubbed migration file and add their custom migration code. Perhaps the imperative migration file can look something like this: ```ts import {migrate} from '@prisma/migrate` migrate({ async up(prisma) { // SQL migration await prisma.executeRaw(`some SQL stuff`) // Data migration await prisma.someModel.create(/*..*/) }, async down(prisma) { await prisma.someModel.delete(/*..*/) await prisma.executeRaw(`undo some SQL stuff`) } }) ```

Posted by flybayer on Jul 1, 2020, 3:19 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments

reliability (Other)

Imperative Migrations with a TypeScript DSL

flybayerJul 1, 2020, 3:19 AM

## Problem Not all migrations can be declarative. For example, we need to add a custom index for performance reasons. We also need this as an escape hatch for anything prisma schema doesn't support. Lastly, we also need this for data migrations. This is very important for any serious apps using prisma migrate. ## Suggested solution A new command: `prisma migrate create` This will create the necessary migration files. Similar as `prisma migrate save` but the files are just stubbed out. Then the user will open the stubbed migration file and add their custom migration code. Perhaps the imperative migration file can look something like this: ```ts import {migrate} from '@prisma/migrate` migrate({ async up(prisma) { // SQL migration await prisma.executeRaw(`some SQL stuff`) // Data migration await prisma.someModel.create(/*..*/) }, async down(prisma) { await prisma.someModel.delete(/*..*/) await prisma.executeRaw(`undo some SQL stuff`) } }) ```

Comments (0)

No comments yet. Start the discussion.

Comment on this issue