Hyperbee.Migrations.Providers.MongoDB
2.0.0
dotnet add package Hyperbee.Migrations.Providers.MongoDB --version 2.0.0
NuGet\Install-Package Hyperbee.Migrations.Providers.MongoDB -Version 2.0.0
<PackageReference Include="Hyperbee.Migrations.Providers.MongoDB" Version="2.0.0" />
paket add Hyperbee.Migrations.Providers.MongoDB --version 2.0.0
#r "nuget: Hyperbee.Migrations.Providers.MongoDB, 2.0.0"
// Install Hyperbee.Migrations.Providers.MongoDB as a Cake Addin #addin nuget:?package=Hyperbee.Migrations.Providers.MongoDB&version=2.0.0 // Install Hyperbee.Migrations.Providers.MongoDB as a Cake Tool #tool nuget:?package=Hyperbee.Migrations.Providers.MongoDB&version=2.0.0
Hyperbee Migrations MongoDB Provider
Welcome to Hyperbee.Migrations.Providers.MongoDB
, an extension to the foundational Hyperbee.Migrations
library. This extension is specifically designed to incorporate support for MongoDB.
Introduction
The Hyperbee.Migrations.Providers.MongoDB
library is an tool for developers seeking to perform database migrations in a MongoDB environment using the Hyperbee.Migrations
library. This library furnishes a comprehensive suite of tools and functionalities that integrate effortlessly with Hyperbee.Migrations
, thereby enabling developers to harness the robust capabilities of MongoDB in their applications.
The Hyperbee.Migrations.Providers.MongoDB
library is equipped to assist developers in creating, updating, and managing their MongoDB databases. With the aid of this library, developers can concentrate their efforts on the development of their application, while the library handles the intricacies of database management.
We are committed to continuous improvement and feature enhancement. We appreciate your interest and look forward to your valuable feedback.
Please see Hyperbee Migrations' Read Me for non-database specific usage.
Concepts
Every migration has several elements you need to be aware of.
- You can create a StartMethod method that resolves to Task <bool>, in order to tell the runner when to start.
- You can create a StopMethod method that resolves to Task <bool>, in order to tell the runner when to stop.
- You can set whether or not you want to journal the migration.
Configuration
Add MongoDB Services
// In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Configure MongoDB
services.AddTransient<IMongoClient, MongoClient>( _ => new MongoClient( connectionString ) );
// Add the MigrationRunner
services.AddMongoDBMigrations(...);
}
public void Configure(IApplicationBuilder app, ...)
{
// Run pending migrations.
var migrationService = app.ApplicationServices.GetRequiredService<MigrationRunner>();
migrationService.Run();
}
Preventing simultaneous migrations
By default, Hyperbee Migrations prevents parallel migration runner execution. If you have 2 instances of your app running, and both try to run migrations, Hyperbee Migrations will prevent the second instance from running migrations and will log a warning.
Hyperbee Migrations accomplishes this by using a distributed lock at the database layer. The default implementation is based on the provider and uses a timeout and an auto-renewal interval to prevent orphaned locks.
If you want to change this behavior you can override the default options:
services.AddMongoDBMigrations( options =>
{
// Locking is on by default. Set to false to allow simultaneous runners - but don't be that guy.
options.LockingEnabled = false;
// You can change locking behavior. Defaults shown.
options.LockMaxLifetime = TimeSpan.FromMinutes( 1 ); // max time-to-live
options.LockName = "ledger"
});
Migrations
Hyperbee Migrations relies on dependency injection to pass services to your migration.
[Migration(1)]
public class MyMigration : Migration
{
private IClusterProvider _clusterProvider;
private ILogger _logger;
// Injected services registered with the container
public MyMigration( IClusterProvider clusterProvider, ILogger<MyMigration> logger )
{
_clusterProvider = clusterProvider;
_logger = logger;
}
public async override Task UpAsync( CancellationToken cancellationToken = default )
{
// do something with clusterProvider
}
}
Dependency Injection
Hyperbee Migrations relies on dependency injection to pass services to your migration. For MongoDB you can directly use the IMongoClient
and interact with MongoDB directly.
[Migration(1)]
public class MyMigration : Migration
{
private IMongoClient _mongoClient;
private ILogger _logger;
// Injected services registered with the container
public MyMigration( IMongoClient mongoClient, ILogger<MyMigration> logger )
{
_mongoClient = mongoClient;
_logger = logger;
}
public async override Task UpAsync( CancellationToken cancellationToken = default )
{
// do something with mongoClient
}
}
The MongoDB provider also provides a MongoDBResourceRunner<MyMigration>
that adds helpful functionality when using embedded resources.
DocumentsFromAsync
inserts documents into database/collections within MongoDB. This is normally use for pre seeding the database.StartMethod
determines when the migration should start (optional)StopMethod
determines when the migration should stop (optional)false
determines if you want to journal (default = true)
[Migration(1, "StartMethod", "StopMethod", false)]
public class MyMigration : Migration
{
private readonly MongoDBResourceRunner<MyMigration> _resourceRunner;
public CreateInitialBuckets( MongoDBResourceRunner<MyMigration> resourceRunner )
{
_resourceRunner = resourceRunner;
}
public override async Task UpAsync( CancellationToken cancellationToken = default )
{
// run a `resource` migration to create initial state.
await resourceRunner.DocumentsFromAsync( [
"administration/users/user.json"
], cancellationToken );
}
public Task<bool> StartMethod()
{
//create process here
}
public Task<bool> StopMethod()
{
//create process here
}
}
Profiles
There are times when you may want to scope migrations to specific environments. To allow this Hyperbee Migrations supports profiles. For instance, some migrations might only run during development. By decorating your migration with the profile of "development" and setting options to include only that profile, you can control which migrations run in which environments.
[Migration(3, "development")]
public class DevelopmentOnlyMigration : Migration
{
public async override Task UpAsync( CancellationToken cancellationToken = default )
{
// do something nice for local developers
}
}
...
// In Startup.cs
public void ConfigureServices( IServiceCollection services )
{
services.AddMongoDBMigrations( options =>
{
// Configure to only run development migrations
options.Profiles = new[] { "development" } };
});
}
A migration may belong to multiple profiles.
[Migration(3, "development", "staging")]
public class TargetedMigration : Migration
{
// ...
}
Cron Settings
[Migration(3, "StartMethod", "StopMethod")]
public class DevelopmentOnlyMigration : Migration
{
public async override Task UpAsync( CancellationToken cancellationToken = default )
{
// do something nice for local developers
}
public async Task<bool> StartMethod()
{
var helper = new MigrationCronHelper();
var results = await helper.CronDelayAsync( "* * * * *" );
return results;
}
public Task<bool> StopMethod()
{
var helper = new MigrationCronHelper();
var results = await helper.CronDelayAsync( "4 * * * *" );
return results;
}
}
Journaling
Journaling is a bool indicator. Null indicates there are no start or stop methods.
[Migration(3, null, null, false)]
public class DevelopmentOnlyMigration : Migration
{
public async override Task UpAsync( CancellationToken cancellationToken = default )
{
// do something nice for local developers
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. |
-
net9.0
- Hyperbee.Migrations (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Hosting (>= 9.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- MongoDB.Bson (>= 3.0.0)
- MongoDB.Driver (>= 3.0.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 |
---|---|---|
2.0.0 | 32 | 11/21/2024 |
2.0.0-develop.241121151205 | 27 | 11/21/2024 |
2.0.0-develop.241121145735 | 29 | 11/21/2024 |
1.2.7 | 119 | 4/30/2024 |
1.2.7-develop.240430153808 | 60 | 4/30/2024 |
1.2.7-develop.240430124929 | 48 | 4/30/2024 |