Categorygithub.com/lerenn/lazy-schema-migration
modulepackage
0.0.0-20250305213025-f517e6370fde
Repository: https://github.com/lerenn/lazy-schema-migration.git
Documentation: pkg.go.dev

# README

NoSQL Lazy Schema Migration Library

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}"]

Supported formats

  • BSON
  • JSON

Install the library

go get github.com/lerenn/lazy-schema-migration

How-To

This how-to is based on the JSON version.

Setting up the migrator

// Create a new migrator
mig := csm.NewMigratorJSON[EntityV3](migrations)

Importing (and migrating) data from JSON

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 data to JSON with version

// Exporting the object with the version field
data, _ = mig.Export(migratedEntity)

Now the data can be saved in the database.

Examples

# Packages

No description provided by the author
No description provided by the author

# Functions

NewMigratorBSON creates a new BSON migrator with given type and migrations.
NewMigratorJSON creates a new JSON migrator with given type and migrations.
WrapperBSON is function that will unwrap a BSON object into a type A, run a callback that should transform the type A object into type B, and wrap the resulting object of type B into a BSON.
WrapperJSON is function that will unwrap a JSON object into a type A, run a callback that should transform the type A object into type B, and wrap the resulting object of type B into a JSON.

# Constants

VersionFieldKey is the key that is used to store the schema version in the objects.

# Variables

ErrGeneric is the generic error that can be used to isolate lazy-schema-migration errors.
ErrInvalidVersionFormat happens when the version is not an integer.
ErrNoVersion happens when no version is detected.
ErrRunningMigration happens when a migration fails.
ErrVersionNotFound happens when the given version is not found in migrations.

# Structs

MigratorBSON is the structure that will contains the migration and apply them on BSON to object T type object, and revert.
MigratorJSON is the structure that will contains the migration and apply them on JSON to object T type object, and revert.

# Interfaces

Migrator is the interface that every migrator should satisfy to respect libraries engagements.

# Type aliases

MigrationBSON is the function signature for BSON object migration.
MigrationJSON is the function signature for JSON object migration.