csharp-mongodb-migrations
0.0.0-preview.0.16
This is a prerelease version of csharp-mongodb-migrations.
This package has a SemVer 2.0.0 package version: 0.0.0-preview.0.16+build.39.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package csharp-mongodb-migrations --version 0.0.0-preview.0.16
NuGet\Install-Package csharp-mongodb-migrations -Version 0.0.0-preview.0.16
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.16" />
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.16
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: csharp-mongodb-migrations, 0.0.0-preview.0.16"
#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.16&prerelease // Install csharp-mongodb-migrations as a Cake Tool #tool nuget:?package=csharp-mongodb-migrations&version=0.0.0-preview.0.16&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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 ofAddMigrations
.
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 IMigration
s between version 0 and 1 and so on.
- The alias used for the
MigrationAttribute
mut match the name inIDatabaseMigratable.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 | Versions 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. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.1)
- MongoDB.Driver (>= 2.22.0)
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 | 206 | 12/21/2023 |
1.1.1 | 138 | 11/5/2023 |
1.1.0 | 88 | 11/4/2023 |
1.0.0 | 65 | 11/3/2023 |
0.1.4 | 75 | 11/3/2023 |
0.1.3-preview.0.8 | 57 | 11/3/2023 |
0.0.0-preview.0.32 | 44 | 11/3/2023 |
0.0.0-preview.0.28 | 54 | 11/3/2023 |
0.0.0-preview.0.16 | 50 | 11/3/2023 |