Coder Social home page Coder Social logo

lerenn / lazy-schema-migration Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 22 KB

Golang library for continuous/on-the-fly/lazy schema migration

License: Apache License 2.0

Makefile 3.15% Go 96.85%
data database golang json lazy migration on-the-fly trickle

lazy-schema-migration's Introduction

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

lazy-schema-migration's People

Contributors

lerenn avatar

Stargazers

 avatar

Watchers

 avatar

lazy-schema-migration's Issues

with NAT ?

I was wondering if you use this with NATS ?

The use case I was thinking is that because NATS is a site and forearm system to the Edge, you can end up with data at the edges, and so when you deploy a new binary to any edge, you need to do a migration.

Maybe even is all the data is in a NATS cluster, you want to take the old event data and migrate it too ?

Would be really good to know the patterns you use this for ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.