How to handle Schema changes with Prisma?
Is there some established process to handle schema changes (i.e. user has first name + last name, but now we want to add the 'fullName' attribute for some reason)?
In this example we would need to run a migration job, which iterates over all the existing users, and adds the new field.
But where would we run this job? Are there some guidelines / tips?
7 Replies
You can add the full name as a nullable
Run the script to merge the names
Drop the name columns and make fullname non nullable
Your issue isn't related to prisma only
Not related to prisma, but with raw query you can merge the fields without using a prisma seed file
Thanks for the tip, this is what I imagined, too!
Would you embed the merge script in the application code itself, or just run it via the db url, for example from your local computer?
with something like
Prisma
Seeding your database
Learn how to seed your database using Prisma's integrated seeding functionality and Prisma Client
create field update code to use the field move from the columns to new column (seed file) drop the two columns
To safely migrate your production database i recommend a multi-stage-process called the expand and contract pattern. Here is a great resource: https://www.prisma.io/dataguide/types/relational/expand-and-contract-pattern
Prisma's Data Guide
Using the expand and contract pattern | Prisma's Data Guide
In this article, we introduce the expand and contract pattern to help migrate data and clients to a new schema.
Thanks a lot! I will gladly checkout your suggestions!