-
Hi! I was surprised to see rollbacks fail, due to match sqlx::migrate!("./migrations").run(&mut *tx).await {
Ok(()) => {}
Err(MigrateError::VersionMissing(e)) => {
warn!("Skipping migrations as we're behind the last-applied version: {e}");
return Ok(());
}
Err(e) => return Err(Error::from(e)),
}; Is this the recommended way to handle this issue? I couldn't find any other references to it. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
There shouldn't be much cause for surprise. You're rolling back to a version of the application that doesn't have the migration. If the migration just failed silently then it could lead to data loss or other runtime errors when the database schema doesn't match what the application was built for. The prevailing advice is to not actually roll back deployments, but to patch and roll forward. This works much better with how Kubernetes handles application lifecycles. We have support for reversible migrations because people demanded it, but we don't use them in practice. The problem is, migrations are almost always destructive. Most of the time, you can't just undo them without suffering some form of data loss. Even if a migration just added a column, rolling back means dropping the data in that column, which is probably important because otherwise why store it in the database in the first place? If a migration dropped data, or moved it around, or transformed it in a lossy manner, you can't just magically reconstruct the information that was lost. Even with reversible migrations, you'd have to roll back using the broken version of the application before downgrading the image, which means your application has to know when you're rolling back. You could implement a If we had the bandwidth, we'd create a guide to cover this all, but in general you should have a plan for when things go wrong, before they do. Hopefully you have backups. Assuming the migration wasn't too destructive, you should be able to resolve this manually. You can delete the row from the |
Beta Was this translation helpful? Give feedback.
Yeah, you buried the lede pretty badly there. My original reply sounds really condescending now.
Sorry if you were expecting a prescriptive answer here, but that's a judgement call you're going to have to make. I looked at a lot of sources, mainly ones for Java apps, and I got the distinct impression that this is far from a solved problem. There's not a ton of good advice out there about how to manage database lifecycles with a Kubernetes application besides either "do it very carefully," or... "don't".
In the first camp, the prevailing advice is to ensure that you always maintain N-1 compatibility (I de…