Imperative Migrations with a TypeScript DSL
See original GitHub issueProblem
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:
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`)
}
})
Issue Analytics
- State:
- Created 3 years ago
- Reactions:121
- Comments:59 (9 by maintainers)
Top Results From Across the Web
The Database as Code Landscape - Bytebase
In Liquibase, a schema migration unit is encapsulated in a Change Set ... approach (declarative), in that the DSL describes the desired end ......
Read more >these things fall short the moment you need real "production ...
I actually much prefer writing migrations by hand. It's actually pretty simple, and it gives you complete control over the schema.
Read more >Documentation - Migrating from JavaScript - TypeScript
During our JS to TS migration, we'll need to separate our input files to prevent TypeScript from overwriting them. If your output files...
Read more >The Complete ORM for Node.js & TypeScript - Prisma
Data modeling, schema migrations and writing database queries are common tasks application developers deal with every day. At Prisma, we found ...
Read more >Coding Styles: Imperative, Declarative and DSL
Can we write a javascript generator function in it? Well is it even meant for it. For rubyists: Here in this database migration,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
TL;DR
From my own user perspective, and for a very basic first version of the feature we are discussing:
Detailed point of view
I completely understand the problem with using the client to manipulate data across migrations, as typings may change over time and invalidate existing migrations.
That’s why I suggested a first basic approach of the issue, where this feature would have nothing to do with the Prisma client. The API we are talking about can be really simple and only go down to generated SQL statements. For instance:
could go down to
I agree this is not a big improvement, but that can become one once I can do stuff like:
The problem is not about writing structure migrations in SQL, I mean it’s totally fine to write
ALTER TABLE
statements, orUPDATE
statements for scalar columns. However, things always get messy once I try to modify columns with a complex data structure (as I mentionned,JSONb
that’s you I’m looking at 🤬).Hi,
So one last thing: I made a solution compatible with Prisma Migrations:
If anybody is interested, I will create a sample repo or something like that.
See ya!