text
stringlengths
0
2.35k
**Mike Fridman:** I guess maybe we should - maybe, as a suggestion - take it one step back and understand why we're doing this in the first place, before we get to a tool... So before you even jump into a tool, as a developer, you're usually developing some backend application, and you wanna make incremental changes to...
So some of the tools that you would use would be Golang Migrate - I think that's the most popular one, probably because of SEO... I think there's other ones such as Dbmate... Me and Vojtech maintain one called Pressly Goose. But at a very high level, the way these things work is you usually have a migrations directory,...
And then these tools - their only responsibility is to read those files and determine whether those migrations have been applied or not, and then keep track of that with usually a metadata or a schema history table in a database.
**Jon Calhoun:** So when you talk about like a history table in a database, this means -- and I think a lot of people don't quite realize this... But this would mean that actually your tool is gonna create a table in their database, that keeps track of these things. So whenever it's actually running the migrations, it'...
**Mike Fridman:** Yeah, that's pretty much what it does... Depending on the tool. Different tools work slightly differently, but conceptually, that's what happens - you read your files, you read where you are in terms of diversion that you're currently at, and then any new migrations should be applied.
**Jon Calhoun:** So presumably, this could lead to a case where -- I guess one way to look at it is I think I've seen people change migrations that have already run, and that can be confusing to them... And I think that's in part because they don't quite understand how they work... And I suppose this could also lead to...
**Mike Fridman:** Yeah, so usually when you write these SQL files, you'll usually create an up migration. So that'll be something like "Create table, alter table, add an index", maybe you even drop an index; that could be your up migration. And then you provide the corresponding way to roll that change back. So it'll b...
So depending on the tool you use - for example, Golang Migrate, you have two separate files. So you'll have something like "01 create table up", and then you'll have "01 create table down". And when you do your up migrations, that creates the table. When you do your down migration, that drops the table. So you kind of ...
**Jon Calhoun:** \[08:09\] So when we're looking at these migrations, another question that I've seen pop up is "Is it possible for them to run not directly in the order that's listed?" If you're sorting (I guess) alphabetically... I'm not sure what -- basically, they're sorted by the number, like the 01, 02, 03... Is ...
**Mike Fridman:** It depends on the tool. This is actually an interesting one, because the number one requested feature in Goose was being able to apply out of order migrations. Because from what I understand - and it's been like eight years since I've done any Rails work... In Rails you have time-based migrations, and...
So depending on the tool, if you're tracking every single migration that you have historically applied, you can start to determine whether you have out-of-order migrations. And the way you get ot out-of-order migrations can be, for example, two developers checking out the repo on the same day, creating let's say a sequ...
Depending on the tool, you'll get conflicts. If you're trying to apply migration 26 when another developer already applied 26, the tool will tell you to fail. But the tool can also detect that you have out of order migrations, and allow you to apply those migrations if you want to. Or if you don't supply the correct fl...
**Jon Calhoun:** Okay.
**Vojtech Vitek:** Personally, I think it's a better idea to use the sequential versions. I think there are pros and cons to both approaches. If you have the timestamp migrations, I think it's a little bit easier for the developers to work with it, because you generate the file just once, the timestamp will be generate...
But then if you have multiple people working on different feature branches at the same time, some of the feature branches might take much longer to get merged into the main branch, and eventually you will end up in a state where perhaps you push something into development, or staging environment, sooner than it would g...
I prefer the sequential increments for the versioning, just because I like to think about this history for the schema linearly. So every single change has to go in order, and this is ensured by the sequential numbers.
**Jon Calhoun:** I think it's one of those things that -- like, out of order sort of made sense with Rails, where people expect things to kind of magically work. They don't wanna have to go in and merge some conflict where two people both generated Migration 24, or something... Because when you're using migrations that...
\[12:04\] But there's definitely cases that that could be an issue, or it's definitely a bad idea to have something where you've only tested it in one order on your computer, and then when you go to push it to production, it's gonna be running in a different order and you don't actually know for sure that's gonna work.
**Vojtech Vitek:** That's exactly right. So this out of order thing - it may have some edge cases, but you're very unlikely to hit them. But once you hit them, it's gonna be a big problem, and you will have a hard time dealing with some production issue. But there's one more thing that I wanted to mention - I think the...
If for example the fourth one failed, with the timestamp migrations the tool would have to somehow track it in the database, so you can roll back the right ones, because you cannot really rely on the number anymore. So you need to rely on some other mechanism. With sequential numbers, it's quite easy. If you apply four...
**Jon Calhoun:** Yeah, I think that's a good thing to keep in mind, because while most of the time migrations hopefully go well, the few times that they don't, especially if you're trying to deploy, or something, that's not a time when you want to be trying to figure out your tooling or how it works... I know I've had ...
**Vojtech Vitek:** I call this "Panic mode."
**Jon Calhoun:** It is panic mode sometimes.
**Vojtech Vitek:** Because you cannot think straight when there are people calling you that "Production is down. What am I gonna do?"
**Jon Calhoun:** So talking about that - when you talked about migrations that go bad, I think different tools sort of handled this differently... So I know one of the approaches is to run the entire migration in a transaction, and then other tools I guess sometimes don't... What's the difference, for people out there ...
**Vojtech Vitek:** So Goose is actually giving you the option to run a migration within the transaction, or not. Which is good. Because not all the SQL queries can be run within the transaction. By default, it is run in transaction, and it's good, because if something fails, you know that it will roll back to the previ...
There's also one more thing, which is -- yeah, that's what I wanted to mention about Golang Migrate. It is using a database locking mechanism. So for example, when you run this, it will lock the database first, and then it will start to apply all the migrations, and then it will commit the transaction, and then it will...
So that's one of the differences between Golang Migrate and Pressly Goose. Pressly Goose, on the other hand, doesn't use any locking mechanism, because it supports lots of different database drivers, and not all of them can do locking... And it actually defers this problem to the executor. So you as a developer or ops ...
\[16:05\] If you do the locking, that's fine too, but I actually ran into some issues with that when I locked the whole database, and then I had to ask a DBA team to go into the database to fix it for me, because the database was in a locked state and I couldn't fix it myself.
**Jon Calhoun:** Okay. So when we're talking about these things running in migrations, and you guys have also mentioned that it's possible to undo a migration, but in reality there are migrations that you can't truthfully undo, if that makes sense... So can you give some examples of what those types of migrations might...
**Vojtech Vitek:** I guess you cannot really undo stuff like deleting tables, deleting columns, right? I think a good practice in general is don't delete stuff prematurely. You are better off leaving the table for a week or two, and then delete it, when you know for a fact that it's not being used anywhere. Because if ...
**Jon Calhoun:** Okay. I was gonna say, deletes are the ones that I've seen most common. There's sometimes some alters in data and things like that that are also hard to revert, but it kind of depends on what the alter is.
**Vojtech Vitek:** I think any type of data manipulation would be another example, too. So if you're changing the data, let's say you're using some JSON data structure in MySQL or JSON postgres and you're manipulating the data, you're better off to, again, do some backups first, because there's no way back.
**Jon Calhoun:** It makes sense.
**Break:** \[17:48\]
**Jon Calhoun:** So when we're talking about migrations, do you have advice on how to setup that process? Because at times we do need to eventually delete data, or maybe we need to add a new column, then we need to deploy some code that uses that column... So do you have advice to some people, what process they should ...
**Mike Fridman:** I could probably describe what we did, and I think that's what a lot of folks do... You typically wanna decouple your migration steps from your application. So this is the one thing that a lot of folks try to do, and it may or may not work out well - in your runtime, for example, you're trying to add ...
In our case, for example, we have Kubernetes, so that's doing a rolling upgrade where we sequentially apply the migration steps first, and then we do a rolling upgrade of the nodes. Now, the thing to remember there is that those nodes are going to have an old version and a new version of your application running at one...
\[19:56\] So some migrations you actually have to split into two steps. So you can't do it all in one step. The way that usually works is write your migration, update your code, deploy that changeset to production, and then write another migration and/or more code changes, and then deploy that again. The main reason yo...
It's easy when you can just say "Oh, I'm gonna turn the application into maintenance mode", and then you don't have to worry about writes happening to your database and you can go nuts. But if you have a high-available system, you have to be careful with how you structure that. And the way I explain it is I think the w...
**Jon Calhoun:** That makes sense. It's one of those things that it's hard, and sometimes it's more painful to do, because you have to take work and basically split it up over several deploys, and that can sort of feel like a drag... But at the same time, I've seen it save my butt several times, that's how I'd put it, ...
Speaking about that, can you think of any other mistakes or pitfalls that people might make whenever they're setting up migrations? I know one example that we got from Twitter was that Nate had -- uh I forget who specifically, but I have Nate in my notes... Nate had mentioned that his team likes to use just 01-migratio...
**Mike Fridman:** I think if you're using tooling, you can typically catch those things. For example Goose offers a way to just create those files without you having to think about it. So it'll look in your Migrations folder, see that 76 was the last migration, and it'll create 77, does it like 007-77, with a name so t...
**Jon Calhoun:** Okay. So when you talk about the tooling, would you suggest that people who want to learn a little bit more about this process actually spend some time maybe building a really simple migration tool on their own?
**Mike Fridman:** Oh, 100%. And this is one of those things I wanted to point out. The real heroes here are the folks writing the database drivers. Thank you to those people for writing those drivers, because none of this would be possible. These migration tools that we mentioned - Goose, Golang Migrate, Dbmate and a p...
So definitely worth spending some time thinking through like how would you write a migration tooling... You'll come to the consensus that it's actually not that bad.
**Jon Calhoun:** It's actually something I did once on my own, and I did it in Go as well, and I've found the version in Go was a lot cleaner than I expected it to be. I think you can basically get something working in like a hundred lines of code, or something like that. I think you had mentioned earlier that Goose su...
**Vojtech Vitek:** Yes, it does.
**Jon Calhoun:** Okay. So even supporting your own custom functions as migrations wasn't that bad, in the sense that you could basically just write a function that reads a SQL file and turns that into a Go function. And then you can also just pass Go functions into like "Here's my list of migration steps, here's the ID...
I've showed it to some people who have had some confusions around migrations in the past, and it's kind of crazy how enlightening that is, just to see "Oh, there's nothing too magical going on. It's really just following some series of steps." And not to say there's not more going on with your tooling, because there's ...