An ABP Module that simulates EntityFramework's Migration flow for MongoDB.
Can be used in applications or modules.
- Import via nuget: https://www.nuget.org/packages/Bdaya.Abp.MongoDBMigrator/
- in your
.MongoDB
module, depend on it:[DependsOn(typeof(BdayaAbpMongoDBMigratorModule))]
Implement IBdayaAbpMigratableMongoDbContext
in the contexts
using Bdaya.Abp.MongoDBMigrator;
public class MyDBContext : AbpMongoDbContext, IBdayaAbpMigratableMongoDbContext
{
//your usual collections...
//Define a context Id to be persisted in the database to uniquely identify the
//current version of the context
public string ContextId { get; } = "MyContext";
//This collection stores version history of all contexts
public IMongoCollection<BdayaAbpMongoDBMigrationHistoryEntry> MigrationHistory => Collection<BdayaAbpMongoDBMigrationHistoryEntry>();
protected override void CreateModel(IMongoModelBuilder modelBuilder)
{
//This configures the `MigrationHistory` collection to be named "_MigrationHistory"
modelBuilder.ConfigureMigrations();
//your usual configuration...
}
}
in your .MongoDB
layer project, create a folder that contains all your migrations (similar to EF).
Assume you want to define migrations for MyDBContext
:
public class MyDBContext_Initial_V0 : BdayaAbpMongoDBVersionedMigratorBase<MyDBContext>, IScopedDependency
{
///An incremental version for your migration
public override int? BaseVersion { get; } = 0;
///An optional name for this version
public override string VersionName { get; } = "Initial";
public override async Task Up(MyDBContext context, IClientSessionHandle session)
{
//Executed when upgrading the database to the next version
//Suitable for creating indexes
}
public override async Task UpTransactioned(MyDBContext context, IClientSessionHandle session)
{
//Executed when upgrading the database to the next version using a transaction
//Suitable for adding data
}
public override async Task Down(MyDBContext context, IClientSessionHandle session)
{
//Executed when downgrading the database to the previous version
//Suitable for deleting indexes
}
public override async Task DownTransactioned(MyDBContext context, IClientSessionHandle session)
{
//Executed when downgrading the database to the previous version using a transaction
//Suitable for removing data
}
}
all startup projects come with a predefined SchemaMigrator class for mongodb MongoDb[PROJECTNAME]DbSchemaMigrator
, change it like this:
- get the migrator instance via Dependency Injection
private readonly IBdayaAbpMongoDBDatabaseMigrator _mongoDBMigrator;
public MongoDbBLCIRMDbSchemaMigrator(IBdayaAbpMongoDBDatabaseMigrator mongoDBMigrator, /*your other imports...*/)
{
//your other assignments...
_mongoDBMigrator = mongoDBMigrator;
}
- call the migrator
public async Task MigrateAsync()
{
//other migration code...
//add this to the very bottom
await _mongoDBMigrator.MigrateFromConfig(dbContexts.OfType<IBdayaAbpMigratableMongoDbContext>());
}
in your .DBMigrator
module, you can control how the migration is going to work.
this package supports 3 behaviors of migrations:
Up
: upgrades the database to the latest versionDown
: downgrades the database one version lowerVersion
: updates the database (either by upgrading or downgrading) to a specific version
you can control which behavior you want by configuring the BdayaAbpMongoDBMigratorOptions
object by adding this to your [ProjectName]DbMigratorModule.ConfigureServices
:
//configure dynamically by using appsettings.json
var section = context.Services.GetConfiguration().GetSection("Migrations");
Configure<BdayaAbpMongoDBMigratorOptions>(section);
//OR configure statically
Configure<BdayaAbpMongoDBMigratorOptions>(opt =>
{
// supports Up, Down or Version
opt.Behavior = BdayaAbpMongoDBMigratorBehaviors.Up;
// the context Ids to include in migration, if set to null or empty, will migrate all contexts
opt.ContextIds = new() { "MyContext", "MyOtherContext" };
// only usable when behavior is set to Version
//opt.Version = 5;
});
-
The difference between normal and
Transactioned
methods, is that when an operation in a transactioned context fails, it will roll back all other operations done in the same method.This doesn't work for some mongodb operations (e.g. operations that invlove Indexes).
-
You can define any number of migrations to any number of contexts
-
a single migration can handle multiple contexts, but you will have to implment
IBdayaAbpMongoDBVersionedMigrator
instead ofBdayaAbpMongoDBVersionedMigratorBase<TContext>
.