csharp-mongodb-migrations
0.0.0-preview.0.32
This is a prerelease version of csharp-mongodb-migrations.
This package has a SemVer 2.0.0 package version: 0.0.0-preview.0.32+build.42.
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.32
NuGet\Install-Package csharp-mongodb-migrations -Version 0.0.0-preview.0.32
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.32" />
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.32
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.32"
#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.32&prerelease
// Install csharp-mongodb-migrations as a Cake Tool
#tool nuget:?package=csharp-mongodb-migrations&version=0.0.0-preview.0.32&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 | net5.0 was computed. net5.0-windows was computed. 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 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.7.2
- 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)
- System.Collections.Immutable (>= 7.0.0)
-
.NETStandard 2.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)
- System.Collections.Immutable (>= 7.0.0)
-
.NETStandard 2.1
- 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)
- System.Collections.Immutable (>= 7.0.0)
-
net6.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)
-
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 | 158 | 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 |