csharp-mongodb-migrations 0.0.0-preview.0.28

This is a prerelease version of csharp-mongodb-migrations.
This package has a SemVer 2.0.0 package version: 0.0.0-preview.0.28+build.40.
There is a newer version of this package available.
See the version list below for details.
dotnet add package csharp-mongodb-migrations --version 0.0.0-preview.0.28
NuGet\Install-Package csharp-mongodb-migrations -Version 0.0.0-preview.0.28
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="csharp-mongodb-migrations" Version="0.0.0-preview.0.28" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add csharp-mongodb-migrations --version 0.0.0-preview.0.28
#r "nuget: csharp-mongodb-migrations, 0.0.0-preview.0.28"
#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.
// Install csharp-mongodb-migrations as a Cake Addin
#addin nuget:?package=csharp-mongodb-migrations&version=0.0.0-preview.0.28&prerelease

// Install csharp-mongodb-migrations as a Cake Tool
#tool nuget:?package=csharp-mongodb-migrations&version=0.0.0-preview.0.28&prerelease

Banner

MongoDB Migration

A database migration system for MongoDB.Driver using ASP.NET.

Quick start

The following steps are required to implement migrations for your solution.

Inject services

Inject AddMigration into your services.

  • If migraitons are not in the Assembly.GetEntryAssembly, then manually specify the assemblies in the parameters of AddMigrations.
services
    .Configure<MyDatabaseSettings>(builder.Configuration.GetSection("Database:MyDatabaseSettings"))
    .AddMigrations()

Update settings

Implement IDatabaseMigratable for your database settings.

  • Options must be configured before services are injected.
  • A Database.Alias must be unqiue: The name used to identify the database during runtime.
  • A Database.Name must be unqiue: The name of the actual MongoDB database.
  • The value DatabaseMigrationSettings.Database.Alias must be a unqiue constant.
public sealed record MyDatabaseSettings : IOptions<MyDatabaseSettings>, IDatabaseMigratable
{
    public required string ConnectionString { get; init; }
    public required string DatabaseName { get; init; }
    public required string MyCollectionName { get; init; }

    MyDatabaseSettings IOptions<MyDatabaseSettings>.Value => this;

    public DatabaseMigrationSettings GetMigrationSettings()
    {
        return new()
        {
            ConnectionString = ConnectionString,
            Database = new("MyDatabase", DatabaseName),
            MirgrationStateCollectionName = "DATABASE_MIGRATIONS"
        };
    }
}

Await migrations

Wait for migrations to complete using IMigrationCompletion before accessing the database at any point.

  • The call is fast when the migration is completed, or the database is not configured.
  • IMigrationCompletion.WaitAsync may not ever be called in constructors.
sealed class MyRepository(IOptions<MyDatabaseSettings> databaseSettings, IMigrationCompletion migrationCompletion) {
    private readonly IMongoCollection<MyModel> _myCollection = new MongoClient(databaseSettings.ConnectionString)
        .GetDatabase(databaseSettings.DatabaseName)
        .GetCollection<MyModel>(databaseSettings.MyCollectionName);

    public async Task<MyModel> GetOrSetAsync(MyModel insertModel, CancellationToken cancellationToken = default) {
        _ = await migrationCompletion.WaitAsync(databaseSettings.Value, cancellationToken).ConfigureAwait(false);
        // ... do stuff
    }
}

Implement migrations

Add IMigrations between version 0 and 1 and so on.

  • The alias used for the MigrationAttribute mut match the name in IDatabaseMigratable.GetMigrationSettings().Database.Alias.
[Migration("MyDatabase", 0, 1, Description = "Add composite index to MyCollection")]
public sealed class PoeNinjaAddCompositeIndexMigration(IOptions<MyDatabaseSettings> optionsAccessor) : IMigration
{
    public async Task DownAsync(IMongoDatabase database, CancellationToken cancellationToken = default)
    {
    }

    public async Task UpAsync(IMongoDatabase database, CancellationToken cancellationToken = default)
    {
    }
}

ToDO

  • Allow upgrading the database to the latest version.
  • Allow upgrading the database to a specific verison.
  • Allow downgrading the database to a specific verison.
  • Add test cases.
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 156 12/21/2023
1.1.1 116 11/5/2023
1.1.0 68 11/4/2023
1.0.0 46 11/3/2023
0.1.4 56 11/3/2023
0.1.3-preview.0.8 41 11/3/2023
0.0.0-preview.0.32 29 11/3/2023
0.0.0-preview.0.28 39 11/3/2023
0.0.0-preview.0.16 35 11/3/2023