Skip to content

hyperremix/Mongo.Migration

 
 

Repository files navigation

Build status Coverage Status

Mongo.Migration

Mongo.Migration is designed for the MongoDB C# Driver to migrate your documents easily and on-the-fly. No more downtime for schema-migrations. Just write small and simple migrations.

**PLEASE NOTE** that updates, aggregation pipeline and projections are not handled in the current version, because they don’t use serialization. You have to handle them yourself.

Installation

Install via nuget https://www.nuget.org/packages/Mongo.Migration

PM> Install-Package Mongo.Migration

How to use

  1. Initialize MongoMigration behind the MongoClient. (Mongo2Go)

    // Init MongoDB
    var runner = MongoDbRunner.Start(); // Mongo2Go
    var client = new MongoClient(runner.ConnectionString);
    
    // Init MongoMigration
    MongoMigration.Initialize();
  2. Implement IDocument or add Document to your entities to provide the DocumentVersion. (Optional) Add the CurrentVersion attribute to mark the current version of the document. So you have the possibility to downgrade in case of a rollback.

    [CurrentVersion("0.1.1")]
    public class Car : IDocument
    {
        public ObjectId Id { get; set; }
    
        public string Type { get; set; }
    
        public int Doors { get; set; }
    
        public DocumentVersion Version { get; set; }
    }
  3. Create a Migration<TDocument> and mark it with the MigrationMarker attribute. The versioning should be done in Semantic Versioning. But it is up to you, wether you just use the patch version to count the number of migrations. If there is a duplicate for a specific type an exception is thrown on initialization.

    [MigrationMarker]
    public class M001_RenameDorsToDoors : Migration<Car>
    {
        public M001_RenameDorsToDoors()
            : base("0.0.1")
        {
        }
    
        public override void Up(BsonDocument document)
        {
            var doors = document["Dors"].ToInt32();
            document.Add("Doors", doors);
            document.Remove("Dors");
        }
    
        public override void Down(BsonDocument document)
        {
            var doors = document["Doors"].ToInt32();
            document.Add("Dors", doors);
            document.Remove("Doors");
        }
    }
  4. (Optional) If you want to put your migrations into an extra project make sure you reference it in the main project and name the project something like "*.MongoMigrations". By convention Mongo.Migration collects all .dlls with the suffix ".MongoMigrations" in your bin folder.

Compile, run and enjoy!

Demo

Inside of the repository you can find a Mongo.Migration.Demo which is a simple demo to show how to use Mongo.Migration.

  1. Compile and run the demo application.
  2. Now you should see the following output in the console.
	Migrate from:
	{ "_id" : ObjectId("59624d5beb5bb330386cd859"), "Dors" : 3, "Type" : "Cabrio", "UnnecessaryField" : "" }

	{ "_id" : ObjectId("59624d5beb5bb330386cd85a"), "Dors" : 5, "Type" : "Combi", "UnnecessaryField" : "" }

	{ "_id" : ObjectId("59624d5beb5bb330386cd85b"), "Doors" : 3, "Type" : "Truck", "UnnecessaryField" : "", "Version" : "0.0.1" }

	{ "_id" : ObjectId("59624d5beb5bb330386cd85c"), "Doors" : 5, "Type" : "Van", "Version" : "0.1.1" }

	To:
	{ "_id" : ObjectId("59624d5beb5bb330386cd859"), "Type" : "Cabrio", "Doors" : 3, "Version" : "0.1.1" }

	{ "_id" : ObjectId("59624d5beb5bb330386cd85a"), "Type" : "Combi", "Doors" : 5, "Version" : "0.1.1" }

	{ "_id" : ObjectId("59624d5beb5bb330386cd85b"), "Type" : "Truck", "Doors" : 3, "Version" : "0.1.1" }

	{ "_id" : ObjectId("59624d5beb5bb330386cd85c"), "Type" : "Van", "Doors" : 5, "Version" : "0.1.1" }

	New Car was created with version: 0.1.1

	Press any Key to exit...
  1. (Optional) Run Mongo.Migration.Demo.Performance.Console

Suggestions

Deploy the migrations in a separate artifact. Otherwise you lose the ability to downgrade in case of a rollback.

Performance

The performance is measured on every push to the repository with a small performance-test. It measures the time MongoDB needs to insert and read n documents (5000) with and without Mongo.Migration. The difference is asserted and should be not higher than a given tolerance (150ms).

Example output of the automated test:

MongoDB: 73ms, Mongo.Migration: 168ms, Diff: 95ms (Tolerance: 150ms), Documents: 5000, Migrations per Document: 2

MongoDB: 88ms, Mongo.Migration: 109ms, Diff: 21ms (Tolerance: 70ms), Documents: 1500, Migrations per Document: 2

MongoDB: 62ms, Mongo.Migration: 63ms, Diff: 1ms (Tolerance: 40ms), Documents: 100, Migrations per Document: 2

MongoDB: 48ms, Mongo.Migration: 50ms, Diff: 2ms (Tolerance: 10ms), Documents: 10, Migrations per Document: 2

After bigger changes the code is analyzed with profiling tools to check for performance or memory problems.

Next Feature/Todo

1. Automatically insert after migrating. So migration is done only once. (MongoDB has its performance on read. I will test if it has big performance issues with the automatically insert.)
2. Tool to upgrade all documents in the database, to the current version. Maybe this is needet for long life applications.
3. Intercept updates, aggregation pipeline and projections.

Copyright

Copyright © 2017 Sean Roddis

License

Mongo.Migration is licensed under MIT. Refer to license.txt for more information.

About

On-the-fly migrations with MongoDB C# Driver

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 98.6%
  • Other 1.4%