NetMongoMigrator.Core 0.3.0

dotnet add package NetMongoMigrator.Core --version 0.3.0
                    
NuGet\Install-Package NetMongoMigrator.Core -Version 0.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetMongoMigrator.Core" Version="0.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NetMongoMigrator.Core" Version="0.3.0" />
                    
Directory.Packages.props
<PackageReference Include="NetMongoMigrator.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NetMongoMigrator.Core --version 0.3.0
                    
#r "nuget: NetMongoMigrator.Core, 0.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NetMongoMigrator.Core@0.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NetMongoMigrator.Core&version=0.3.0
                    
Install as a Cake Addin
#tool nuget:?package=NetMongoMigrator.Core&version=0.3.0
                    
Install as a Cake Tool

NetMongoMigrator

MongoDB migration library for .NET

Getting Started

Migrator executes script-based migrations defined by classes implementing interface IMigration:

using MongoDB.Driver;
using NetMongoMigrator.Core;

namespace NetMongoMigrator.Console.Example.Migrations
{
    internal class Migration_0001_Example : IMigration
    {
        public int Id => 1;

        public async Task Up(IMongoDatabase mongoDatabase, CancellationToken cancellationToken)
        {
            await mongoDatabase.CreateCollectionAsync("TestCollection1", cancellationToken: cancellationToken);
            await mongoDatabase.CreateCollectionAsync("TestCollection2", cancellationToken: cancellationToken);
            await mongoDatabase.CreateCollectionAsync("TestCollection3", cancellationToken: cancellationToken);
        }

        public async Task Down(IMongoDatabase mongoDatabase, CancellationToken cancellationToken)
        {
            await mongoDatabase.DropCollectionAsync("TestCollection1", cancellationToken: cancellationToken);
            await mongoDatabase.DropCollectionAsync("TestCollection2", cancellationToken: cancellationToken);
            await mongoDatabase.DropCollectionAsync("TestCollection3", cancellationToken: cancellationToken);
        }
    }
}

Migration definition:

  • Id - have to be unique. Migrator executes migrations sorting them ascending by id.
  • Up(IMongoDatabase mongoDatabase, CancellationToken cancellationToken) - method called when migration is executed up.
  • Down(IMongoDatabase mongoDatabase, CancellationToken cancellationToken) - method called when migration is executed down.

Currently library allows you to launch migrations in two ways.

  • Using core component directly
  • Using console implementation

I. Using core component directly

Install nuget package NetMongoMigrator.Core.

Prepare migrator configuration class MigratorConfiguration:

var configuration = new MigratorConfiguration
{
    MigrationsTableName = "Example Migrations Table Name",
    ConnectionString = "Example Connection String",
    DatabaseName = "Example Database Name"
};

Build migrator:

var migrator = new MigratorBuilder()
    .SetConfiguration(configuration)
    .SetMigrations(new [] { new Migration() })
    .SetLogger(logger)
    .Build();

You can also scan assembly for migrations:

var migrator = new MigratorBuilder()
    .SetConfiguration(configuration)
    .AddMigrationsFromAssembly(typeof(Program).Assembly)
    .SetLogger(logger)
    .Build();

⚠️ If you not specify configuration, migrations or logger code will throw MigratorNotConfiguredCorrectlyException.

Then run migrator up to latest version:

await migrator.ExecuteMigrationsUp();

Or up to specified migration id:

await migrator.ExecuteMigrationsUp(5);

You can also run migrator down to latest version:

await migrator.ExecuteMigrationsDown();

Or down to specified migration id:

await migrator.ExecuteMigrationsDown(5);

II. Using console implementation

Create new console project and install nuget package NetMongoMigrator.Console.

Inside Program.cs launch migrator:

using NetMongoMigrator.Console;

var consoleMigrator = new ConsoleMigrator();
return consoleMigrator.Run(args, typeof(Program).Assembly);

Migrator will automaticly scan for migrations inside specified assembly.

You can find example in Examples/NetMongoMigrator.Console.Example.

To launch migrator compile your console app and execute it:

migratorApp.exe up [connection string] [database name]

You can also migrate to specified migration id:

migratorApp.exe up [connection string] [database name] -v <migration id>

And migrate down:

migratorApp.exe down [connection string] [database name] -v <migration id>

Plans for future:

  1. Handling migrations exceptions
  2. NetMongoMigrator.MongoDbDriverExtensions - extensions for MongoDb.Driver making migrations easier to implement
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on NetMongoMigrator.Core:

Package Downloads
NetMongoMigrator.Console

Console implementation of NetMongoMigrator - MongoDB migration library for .NET

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.0 196 3/8/2024
0.2.0 298 10/23/2023
0.1.0 200 10/14/2023