This is a golang library for on-the-fly/lazy migrations.
When doing migrations, it can be difficult to execute stop-the-world migrations and migrate all the data at once. This is a library that shall enable the migration when the data is retrieved/stored, and even a second time to convert the remaining data:
flowchart TB
Initial[Before migration]--Deploy code-->Progressive[Migrate entity when created/updated];
Progressive-->Progressive
Progressive--If completion needed-->Full[Migrate remaining data];
Full-->Migrated;
Let's have an example with a migration for a Person entity from v1 to v2:
// From
PersonV1{
FirstName string
}
// To
PersonV2{
FirstName string
LastName string
}
If we take a look at a data which is updated from the database, show as
Migrate entity when created/updated
in the previous diagram:
flowchart TB
OldData["{'firstName':'John','__schema_version':1}"]--Migration to last version-->Code
Code["PersonV2{\nFirstName string\nLastName string\n}"]--Save with actual version-->NewData["{'firstName':'John','lastName:'','__schema_version':2}"]
- BSON
- JSON
go get github.com/lerenn/lazy-schema-migration
This how-to is based on the JSON version.
// Create a new migrator
mig := csm.NewMigratorJSON[EntityV3](migrations)
Here, the data is written but could come from a database.
// Importing an old object, do some modifications and save it as last version
data := []byte(`{"FullName":"John Robert Reddington","__schema_version":1}`)
migratedEntity, _ := mig.Import(data)
// Exporting the object with the version field
data, _ = mig.Export(migratedEntity)
Now the data can be saved in the database.