How database migrations work when using Drizzle ORM

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.พ. 2025
  • My Products
    🏗️ WDC StarterKit: wdcstarterkit.com
    📖 ProjectPlannerAI: projectplanner...
    🤖 IconGeneratorAI: icongeneratora...
    📝 ThumbnailCritique: thumbnailcriti...
    Useful Links
    💬 Discord: / discord
    🔔 Newsletter: newsletter.web...
    📁 GitHub: github.com/web...
    📺 Twitch: / webdevcody
    🤖 Website: webdevcody.com
    🐦 Twitter: / webdevcody

ความคิดเห็น • 60

  • @hhhhhhhhhhhhhhhhhhhhhh
    @hhhhhhhhhhhhhhhhhhhhhh 9 หลายเดือนก่อน +12

    The team behind Drizzle actually just added a "migrate" command to drizzle-kit's beta version on April 26th which will likely make this more straightforward if you're not in need of some custom behavior. This way definitely still works too but just figured I'd point it out

    • @WebDevCody
      @WebDevCody  9 หลายเดือนก่อน

      I’ll have to look more into it

    • @ShaneCodes
      @ShaneCodes 9 หลายเดือนก่อน

      Do you have a link to this? Its not in their docs

    • @UbergonMX
      @UbergonMX 8 หลายเดือนก่อน +3

      ​@@WebDevCodyhave you looked into it? is your method still the better way?

    • @kenuahs
      @kenuahs 5 หลายเดือนก่อน

      @@UbergonMX I think it is up to the developer preference, I don't personally think the new way is "better" but some might like only having to deal with drizzle-kit and not writing a small script as this video shows. For my project I chose the small script approach instead of the drizzle-kit migrate command because I have drizzle-kit installed as a dev dependency only, and so in production it isn't available.

    • @UbergonMX
      @UbergonMX 5 หลายเดือนก่อน

      @@kenuahs ah this makes sense

  • @gadgetboyplaysmc
    @gadgetboyplaysmc 9 หลายเดือนก่อน +9

    You've been uploading a ton dude. sick!

  • @iamsomraj
    @iamsomraj 9 หลายเดือนก่อน

    Dude you really made a whole video… thanks man

  • @bnssoftware3292
    @bnssoftware3292 9 หลายเดือนก่อน

    I really like this series of following along with your SaaS template build out. I will definitely be a buyer of it.

  • @rahultech77
    @rahultech77 9 หลายเดือนก่อน

    Thanks for uploading this. I was running db:push manually as i wasn't able to figure out how to run this automatically on CI

  • @mantas9827
    @mantas9827 7 หลายเดือนก่อน

    just came to say this helped me, thanks!

  • @mojo6689
    @mojo6689 9 หลายเดือนก่อน

    Great video. Clarified a lot of things that were confusing me

  • @angelherriv
    @angelherriv หลายเดือนก่อน

    Thanks for your tutorial! Question: what about if you need to do data migrations along schema migrations? also wondering if is it possible to run typescript code and track it so they run just once?

  • @butwhothehellknows
    @butwhothehellknows 9 หลายเดือนก่อน +1

    First!!!! Also, love ya! You’re doing a great job❤

    • @WebDevCody
      @WebDevCody  9 หลายเดือนก่อน +1

      Love you babe, thanks for being first. I'm going to give you something special later tonight

    • @butwhothehellknows
      @butwhothehellknows 9 หลายเดือนก่อน

      @@WebDevCody sir…. Log off the internet for the day please lol

  • @AndrewHardy-v8d
    @AndrewHardy-v8d 5 หลายเดือนก่อน

    Great video.
    I listened to a podcast that recommended Drizzle for allowing you to take what bits you want and leave what bits you don't. I am not a fan of the ACTUAL ORM concept and especially not tying miigrations to the ORM and not going to be convinced really, but I am looking for the type safety, so coming from knex I'd like to be able to write manual migrations using type safe typescript, then in a similar way to what you can do with keysely reverse generate types from the existing database to support type safe writing of quesries in typescript using their query builder that is closest to SQL.
    Just that really - but I am struggling to find the right part of the docs. It all seems to be generating raw SQL migration files FROM the ORM schema (something I don't intend to use after my experience with Prisma and Sequelize)
    Any direction would be greatly appreciated.

  • @Stallion45
    @Stallion45 9 หลายเดือนก่อน

    Would be cool if you did a video on how and why you choose your database options for projects. Drizzle, Prisma, Convex…, mysql, psgr etc. I’ve used mysql for years, and been learning the others, but this is a major frustration point t as I keep second guessing myself and wondering if my choice is the right choice. From video to video on this channel and others it seems the choice is random. Just one of my obstacles that keeps me from finishing projects.

    • @WebDevCody
      @WebDevCody  9 หลายเดือนก่อน

      It depends on scale and data needs. In most cases postgres or mysql is the best bet. Convex if you want an all inclusive baas

    • @Stallion45
      @Stallion45 9 หลายเดือนก่อน

      @@WebDevCody Thanks, as a PHP dev for almost 15 years mysql was my goto, so I've been having a good time just playing with other arms and db's but I am leaning toward Convex more often than not. It's fast, real-time, very slick dev env, and as you said it's kind of a complete package with file storage. Keep up the great videos and would love to see more with Convex.

  • @euanmorgann
    @euanmorgann 9 หลายเดือนก่อน +1

    Awesome video 🙏
    One question though,
    Since this is a monorepo if you had multiple apps in there pointing at the same shared DB, would you still run migrations on start for all of them?

    • @WebDevCody
      @WebDevCody  9 หลายเดือนก่อน

      You’d probably only want one to control running the migrations,or a separate ci cd process maybe

  • @milon27
    @milon27 4 หลายเดือนก่อน +2

    how those hash are created? and how to know which one which migration?

    • @milon27
      @milon27 2 หลายเดือนก่อน

      we can check the when value and created at value in db.

  • @rayrack5416
    @rayrack5416 5 หลายเดือนก่อน

    How are you migrating your triggers and stored procedures in your stack?

  • @elmalleable
    @elmalleable 9 หลายเดือนก่อน +2

    my scripts always have db:generate:seed and db:generate:refresh (refresh to tear down and rebuild my local db)

  • @Flash136
    @Flash136 9 หลายเดือนก่อน +2

    The only annoying thing about Drizzle is their push command when prototyping. With Prisma, the push command will let you keep pushing to the database as long as it isn't going to delete any data. With Drizzle, the push command will ask for your intervention even when a table has been renamed or has been removed from the schema. This is quite annoying when prototyping because any major change to the database structure requires you to manually clear unused tables. Also, Drizzle just doesn't seem to know when data deletion is possible. You have to keep telling it that yes this push operation is safe, go ahead. Prototyping with Prisma is a breeze. I just need to generate migrations IF and WHEN I actually already want/need to.

    • @WebDevCody
      @WebDevCody  9 หลายเดือนก่อน +1

      Prisma feels a bit better honestly. I think drizzle assumes you will do traditional migration scripts and they don’t recommend using push for production deploys

  • @roid1510
    @roid1510 3 หลายเดือนก่อน

    How do you apply migration changes to a table that already has data?

  • @fun_factory880
    @fun_factory880 หลายเดือนก่อน

    hey! Thanks a ton for this video, I am facing a issue regarding running the migrations on Production, I am using docker image with AWS ecr and AWS ECS.
    The Problem I am facing in when and wher to run npx drizzle-kit migrate in production/staging env, so that it updates my db's,
    any help would be great

    • @WebDevCody
      @WebDevCody  หลายเดือนก่อน

      I’m not sure what you mean. You run the command locally or when ecs runs your containers

  • @hugo_dev
    @hugo_dev 9 หลายเดือนก่อน +2

    I have a question. What if, let's say my hello table, already had some data in it, and I added some more columns into the hello schema. How can I migrate the changes but still manage to keep all my existing data in the hello table.

    • @rodjenihm
      @rodjenihm 9 หลายเดือนก่อน +2

      If new columns are nullable, then there is no issue. If you want to make them not nullable you first have to make them nullable, then populate the data for existing records and then make them not nullable after that.

    • @WebDevCody
      @WebDevCody  9 หลายเดือนก่อน +2

      like @rodjenihm said, but also I think you can also just add a default value to the columns.

    • @hugo_dev
      @hugo_dev 9 หลายเดือนก่อน

      @@rodjenihm thank you so much!

    • @hugo_dev
      @hugo_dev 9 หลายเดือนก่อน

      @@WebDevCody thank you so much!

  • @Sanjay54251
    @Sanjay54251 หลายเดือนก่อน

    Hi sir drizzle RM is very good but if you are doing migration if you remove the column from the model class and then you run the migration the column will also deleted in database which comb cause harmful effects how we prevent to harmful effect if you are changed the code accidentally and remove the column

    • @WebDevCody
      @WebDevCody  หลายเดือนก่อน

      You should not be using db push on prod. Usually you’d write the migration scripts and verify they are good

  • @Its-InderjeetSinghGill
    @Its-InderjeetSinghGill 9 หลายเดือนก่อน

    Hi, I love your videos , I have one question about migrations. I followed the pattern showing in your video works great but just to make sure and test each edge case I delete migrations folder form my project after generating and migrating previously and then I re generated migrations and ran migrate script, but it throw an error like SQL_INPUT_ERROR: SQL input error: table `customers` already exists. It looks like it lost the track of changes I made earlier in the schema. A quick answer would be really appreciated.

    • @WebDevCody
      @WebDevCody  9 หลายเดือนก่อน

      I think you either need to do a drizzle kit pull so that your meta directory and journal file is correctly updated, or just keep your migration scripts around even after running them.

    • @Its-InderjeetSinghGill
      @Its-InderjeetSinghGill 9 หลายเดือนก่อน

      @@WebDevCody Thanks man, I really appreciate the help, I have been pulling my hair over this for hours. I read the docs again and found out that I have to run introspect and then make sure my current schema matches with the introspection schema and then I can make changes again to my schema and do migrations with no problem.

  • @DouglasSouza-te9bt
    @DouglasSouza-te9bt 9 หลายเดือนก่อน

    How often do you get discouraged with scheduling? And what do you usually do to deal with it?

    • @WebDevCody
      @WebDevCody  9 หลายเดือนก่อน

      What do you mean?

  • @football-is-divine
    @football-is-divine 3 หลายเดือนก่อน

    When migratiing The serial function is not working. Why?

  • @Mikegdro
    @Mikegdro 7 หลายเดือนก่อน

    Now my question is,
    i have a nodejs microservice which is a backend for frontend, and i'm deploying with coolify, so when i upload de code to gh, a webhook automatically calls my coolify installed in my vps, and this runs the docker i have, this docker firstly builds the project, and then deploys it to a clean machine, so my way of doing automated migrations is copying the migration folder to the new machine with just the JS files, and then run the migrations before i run express, and in case this fails, the app shuts down.
    I had to do it this way because of deploying the clean build files in the dist directory instead of the TS ones and running a dev server on it.
    Is this okay?
    I've been having a hard time learning the best practices, and the deploy process and automation of it is being REALLY like REALLY hard, there's so much stuff to have in mind, and the bare thought of 'Where in the lifecycle of the deploy process should i do the migrations, and how the f*** do i automate them' was really tough.
    I now have it running and it is going flawlessly, i was able to install a pgAdmin in coolify that connects to the PostgreSQL i have, so i could monitor how the migrations were working and everything seems fine, but i lack the expretisse to know if this methodology is okay, so i'd love to know if the workflow i described is good enough so i can sleep at night xdd.

    • @elmalleable
      @elmalleable 5 หลายเดือนก่อน +1

      If it ain't broke and it's working fine. Throw something it or try to break it somehow and probably fix those issues that might easily break the whole process

  • @ogbillity
    @ogbillity 8 หลายเดือนก่อน

    Hi Cody, why did you use text and randomuuid as id in the schema instead of id: uuid("id").primaryKey().defaultRandom() ?

  • @choskas
    @choskas 7 หลายเดือนก่อน

    Bro I have a question I don't know if you can help me. If you are working on a dev branch an run generate and migrate all works fine but if you go to production branch and do the merge and then run the migrate it will do all the migrations that are not actually applied? Or you have to run one by one manually?

    • @elmalleable
      @elmalleable 5 หลายเดือนก่อน

      The migrations will run only if it's not tracked in the migration table. But answer your question. It should run all your migrations automatically

  • @Harish-rz4gv
    @Harish-rz4gv 9 หลายเดือนก่อน

    Did u use arc browser which Theo uses??

    • @WebDevCody
      @WebDevCody  9 หลายเดือนก่อน +1

      No I use chrome

  • @-lumqua-827
    @-lumqua-827 9 หลายเดือนก่อน

    Great

  • @siya.abc123
    @siya.abc123 9 หลายเดือนก่อน +2

    Is something wrong with me? Once I understood Go earlier this year I'm not interested in server side js 😭
    I've always done C# but for side projects I still used node. Now node is for frontend and Go is backend. What's wrong with me?
    The only thing keeping me in js land is the ui components like Shadcn and Aceternity

  • @chiubaca
    @chiubaca 9 หลายเดือนก่อน

    Is WDC starter kit going to be open sourced ?

    • @WebDevCody
      @WebDevCody  9 หลายเดือนก่อน +1

      Nah it’s a paid starter kit

  • @lev1ato
    @lev1ato 9 หลายเดือนก่อน

    maybe a video about next auth pls 🥺🥺

  • @codeguy7309
    @codeguy7309 8 หลายเดือนก่อน

    now it's only generate and not generate:pg

  • @peeei7423
    @peeei7423 7 หลายเดือนก่อน

    has anyone ever told you you look like pewdiepie?

    • @WebDevCody
      @WebDevCody  7 หลายเดือนก่อน

      I don’t think so