Prisma Migrate Early Access Demo

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to this tutorial um showing you how the new prisma migrate is working and for that demo i'm going to use a a simple example blog example that we have which is using next.js and i'm going to make a simple change to my schema which is basically splitting this username into first name and last name and you're going to see throughout the that tutorial how the new migrate helps you in the process of doing these kinds of expanded contract migrations first off i need to install the the early access version of prisma migrate so i will do that using npm install the 2.11 with the integration branch and the cli version of that too i'm just checking that my packages and gets updated yes updated um so i'm all good i'm going to check that indeed this is the right version so by running npx prisma migrate h i can see here i have the early access feature enabled so everything is all set another preparation and that is that is due to the current state of the early access program of migrate is that i need to baseline my database right now this database has no sort of awareness of migrate and migrate doesn't like considers it as an empty database by default which is not the case um i can show you what what's currently in there using prisma studio and you can see i already have data and i already have model so for migrate to be aware of that what i'm going to have to do is baseline that database um so how can i do that i will just run prisma migrate draft and so what draft does and it basically creates an empty migration in which i will add my seed script so in it seed all right so now i'm taking my seed script data i'm going that init seed migration sql you can see here my scheme has been created and i just add the seed data just below this is the first customization i'm doing so that i'm i'm clear that once running migrate it's going to recreate the tables and recreate data and not just the tables once i'm done with that i can run migrate no draft just migrate and the database is not empty so you realize that yes i have have something to do here um i just delete it just to recreate it it's a sqlite database so migrate will actually recreate it by default up um so just checking that everything worked fine i'm just going to run again prisma studio to make sure i should have the exact same thing yes looks looks like that right everything is in there cool so now i baseline my database so that devdb is aware and has the history of migration to recreate it at the initial state so i can really get started with the changes i wanted to do in my schema so as i said i wanted to to split that name into first name and last name so i'm just gonna edit my schema and create first and last name both optional because my name is optional so i just need to make sure i i will also have optional names in there and then same as before i'm just gonna run migrate and this time is expand name the name i want to give to that migration i can check that expand name is basically just adding these two new cons and that's that's exactly what i needed to do i can also go to check in studio that i indeed have my two new columns all right so now i want to pre-populate the first name with the name because the new columns don't have any data in them and since i want to eventually remove the name column i need to make sure i copy around my the the user names into first names so that everything is fine so what i'm going to do there is again create a draft migration and this is transform data and in that migration i'm going to run this very simple sql command on the user set first name equals name save this and i apply it all right just run studio again double check everything is done yes data is copied around so i did go create that migration so that anyone who would pick up my changes would be would be applying the same things to their own data sets so i could have just manually changed the values but it would have worked only for me the the benefit of using migrate for that reason is that that transformation is available to everyone i did a very simplistic sql script uh you you could you could go a bit fancier and use the sql if you're if you're in the line database um this is again an early access version we understand that writing sql for doing transformations is not necessarily the way you want it you want that to to be done you could also consider doing transformations using your own script and just hook that into your ci system but for the sake of simplicity felt like just doing that to sequel would do the trick all right so now we have the columns we have everything what i'm going to do now is make changes to the code so that it picks up the first name and last name i'm going to do that in well in a time lapse fashion so you don't have to worry too much about these changes i don't think they make they add a lot of value for you to understand exactly i'm gonna um i'm doing this so yeah i'm gonna speed that up a bit okay all right i think with these i should be good to go so let's just test that all right yes works sign up now i can see first name i can sign up with my new user i can create a draft yep all right so now i can tell my application works um what i would typically do is then commit these changes and uh share them with my colleagues so they can just do the same the last thing i want to do before that is just contract so this contraction can be done by just removing the name and running migrate again and some data will be lost but i'm fine with that because i know i tested everything so contract all right and running that again yep works you can see my you can see my drafts from last time everything's fine cool all right so now i'm done finished my split my application works i can share all of that and i can see all my migrations will be reproducing all the steps that i'm doing so what did i do again i have first initialized my database with the c script so that everything is baseline properly this will be made way easier except for this heating that you will have to to also create yourself because we cannot infer the seed but we will make the base lining of the database easier in the future so you don't have to to do all these steps manually then i expanded the name so i just added these new columns and transformed the data i just put the contents of name into first name and finally i contracted the the column so that that's the way migrate does this [Music] and yeah i hope you liked it and you can give yourself a try with the new migraine ciao
Info
Channel: Hervé Labas
Views: 249
Rating: 5 out of 5
Keywords:
Id: fRunGwAiC28
Channel Id: undefined
Length: 12min 36sec (756 seconds)
Published: Mon Nov 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.