Giter Site home page Giter Site logo

mongo-csharp-migrations's Introduction

MongoDB Incremental Migrations

Not production code, just a proof of concept.

Points to be demonstrated:

  • what should be done to achieve migration capability without taking database offline?
  • what is the overhead (extra object metadata, any slowdowns during deserialization)?
  • what should be changed on the C# driver?

How?

In order to make the migration process work, [a couple of extension points] (https://github.com/darkiri/mongo-csharp-driver/compare/master...migration) are needed on the official C# driver. It means that micro framework relies on a custom build made from the [forked repository] (https://github.com/darkiri/mongo-csharp-driver/tree/migration).

// setup
BsonSerializer.RegisterSerializationProvider(new MigrationSerializationProvider());

[Migration(typeof (MigrationTo_1_0))]
private class Customer
{
    public ObjectId Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

private class MigrationTo_1_0 : IMigration<Customer>
{
    public Version To
    {
        get { return new Version(1, 0); }
    }

    public void Upgrade(Customer obj, IDictionary<string, object> extraElements)
    {
        var fullName = (string) extraElements["Name"];

        obj.LastName = fullName.Split().Last();
        obj.FirstName = fullName
                .Substring(0, fullName.Length - obj.LastName.Length)
                .Trim();
        extraElements.Remove("Name");
    }
}

In one of the earlier application versions Customer had the only property Name. In v1.0 the Name was splitted into the FirstName and LastName. MigrationTo_1_0 is applied to the Customer and contains code migrating data from all older versions to the version 1.0. Migration class contains an Upgrade method. It means that it is not possible to downgrade the data to the earlier versions.

mongo-csharp-migrations's People

Contributors

darkiri avatar

Watchers

Rasmus Tomi-Gottschau avatar James Cloos avatar

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.