It’s fine, Rewind: Revert a migration without losing data
planetscale.com
It’s fine, Rewind: Revert a migration without losing data
1–10 of 39 posts
Re: It’s fine, Rewind: Revert a migration without losing data
#2Re: It’s fine, Rewind: Revert a migration without losing data
#3This is the link where they go into detail about the mechanism that enables this feature.
https://vitess.io/docs/13.0/reference/vreplication/vreplicat...
Maybe GitHub should look into migrating to PlanetScale for their mysql1 cluster that keeps going down this week? Unless PlanetScale uses GitHub and that would introduce a circular dependency. Eh, it’s turtles the whole way down either way I suppose.
Re: It’s fine, Rewind: Revert a migration without losing data
#4Does it only work if no data has been written to the new structure? E.g. drop a column, and new record comes in without that column? What then?
In your scenario, you drop a column, populate some new rows in the new structure. Then, you regret the migration and rewind. You get the column back with all the pre-dropped values, AND you get to keep all the new rows which you've inserted.
The values for the now-restored column for the newly inserted rows is the DEFAULT value per column definition.
Re: It’s fine, Rewind: Revert a migration without losing data
#5Does it only work if no data has been written to the new structure? E.g. drop a column, and new record comes in without that column? What then?
Engineer at PlanetScale here: it _does_ work if data has been written to the new structure! In your scenario, you drop a column, populate some new rows in the new structure. Then, you regret the migration and rewind. You get the column back with all the pre-dropped values, AND you get to keep all the new rows which you've inserted. The values for the now-restored column for the newly inserted rows is the DEFAULT valu…
Re: It’s fine, Rewind: Revert a migration without losing data
#6Does it only work if no data has been written to the new structure? E.g. drop a column, and new record comes in without that column? What then?
Engineer at PlanetScale here: it _does_ work if data has been written to the new structure! In your scenario, you drop a column, populate some new rows in the new structure. Then, you regret the migration and rewind. You get the column back with all the pre-dropped values, AND you get to keep all the new rows which you've inserted. The values for the now-restored column for the newly inserted rows is the DEFAULT valu…
Re: It’s fine, Rewind: Revert a migration without losing data
#7Earlier quoted context omitted.
Engineer at PlanetScale here: it _does_ work if data has been written to the new structure! In your scenario, you drop a column, populate some new rows in the new structure. Then, you regret the migration and rewind. You get the column back with all the pre-dropped values, AND you get to keep all the new rows which you've inserted. The values for the now-restored column for the newly inserted rows is the DEFAULT valu…
that’s awesome but when would it be useful? wouldn’t that lead to data loss?
Re: It’s fine, Rewind: Revert a migration without losing data
#8Re: It’s fine, Rewind: Revert a migration without losing data
#9Earlier quoted context omitted.
Engineer at PlanetScale here: it _does_ work if data has been written to the new structure! In your scenario, you drop a column, populate some new rows in the new structure. Then, you regret the migration and rewind. You get the column back with all the pre-dropped values, AND you get to keep all the new rows which you've inserted. The values for the now-restored column for the newly inserted rows is the DEFAULT valu…
that’s awesome but when would it be useful? wouldn’t that lead to data loss?
If you will indulge a realistic story; I've been through this process multiple times in production.
You change a large table via ALTER TABLE; you possibly change a data type, or drop a column, or modify an index. The change takes 5 hours to complete - and things go bad. Testing in staging was good, but as it turns out the production environment cannot cope with the changes and still needs the previous schema. Some traffic is still able to pass through, but some requests are erroring.
What do you do?
One option is to run another ALTER TABLE that takes you back into the original schema. This will take yet another 5 hours, during which your app may be degraded or altogether down. Plus you'll be unable to recover lost data (such as in a DROP COLUMN scenario). Another option is to do a point in time recovery for your entire database. This will both take time, but more importantly you will lose all the data you've accumulated since the migration completed. Any new user account, any new artifact, any new event - will be lost. Rows that were deleted suddenly reappear. Data that should not be available anymore suddenly is.
Most people will try a third option: do a point in time recovery on an offline server, and extract/copy just the specific table and copy it onto production. Typically this involves a lot of juggling and most environments will not have the infrastructure to automate the entire process. But even once this is done, you're still hit with the unfortunate implication: your data set is now both incomplete as well as inconsistent.
It is incomplete because data is missing from the restored table. Any rows accumulated since the point in time recovery point - are lost. It is inconsistent, because in many cases, due to the natural relational design of your schema, other tables will have rows that relate to the missing restored table's rows. You may try to then manually backfill those missing rows into the restored table (or remove rows previously deleted) , but in reality some processes will already have manipulated the data on the restored table even while you're trying to resolve the situation, leading to more conflicts.
It seems like the only safe way is to take everything offline, disable any writes to the broken tables as well as some of, or all tables, associated with it, resolve all conflicts, then restore data onto production and enable writes again. Or, you choose to lose data, track down any known conflicts, reach out to users and inform them of the data loss. Either way this has a significant impact on your service.
And so Rewind offers an instant fall back to your previous schema, while still retaining any data you've accumulated since the time of incident. Rewind resolves the differences between previous and current schema, and adapts the latest data changes onto the old schema. As you rewind the migration your table still has the same amount of rows, and maintains all incoming or outgoing references from and to other tables. It all happens on your production environment and does not require an offline server.
Here's a technical description of how Rewind works: https://planetscale.com/blog/behind-the-scenes-how-we-built-...
Re: It’s fine, Rewind: Revert a migration without losing data
#10Is this similar to Temporal Tables in MSSQL?
It's also not something you need to activate ahead of time, like you do in MSSQL temporal tables; it is activated on your behalf for any schema change you deploy.