MongoDBMigrations 1.0.1
All versions 1.*.* are deprecated and no longer supported. Please use version 2.0.0 and higher. They contain brand new fluent API, lots of improvements. Migrations are from elder versions 100% compatible with the latest version. So don't afraid to upgrade.
See the version list below for details.
dotnet add package MongoDBMigrations --version 1.0.1
NuGet\Install-Package MongoDBMigrations -Version 1.0.1
<PackageReference Include="MongoDBMigrations" Version="1.0.1" />
paket add MongoDBMigrations --version 1.0.1
#r "nuget: MongoDBMigrations, 1.0.1"
// Install MongoDBMigrations as a Cake Addin #addin nuget:?package=MongoDBMigrations&version=1.0.1 // Install MongoDBMigrations as a Cake Tool #tool nuget:?package=MongoDBMigrations&version=1.0.1
MongoDBMigrations
MongoDBMigrations using the official MongoDB C# Driver to migrate your documents in database
No more downtime for schema-migrations. Just write small and simple migrations
.
We need migrations when:
1. Rename collections
2. Rename keys
3. Manipulate data types
4. Index creation
5. Removing collections / data
New Features!
- Fixed: Search assemble with migrations when method
LookInAssemblyOfType<T>()
doesn't used - Fixed: Runner crash when
runner.UpdateTo()
called without result handling - Fixed: Behavior when target migration not found
- Added: Testable migrations
- Added: Overload for
LookInAssemblyOfType
method - Added: Fields in
MigrationResult
for progress handling - See more...
Next Feature/Todo
- Diff calculation
- Auto generated migrations
- Migration as part of CI
- Async implementation
Installation
MongoDBMigrations tested with .NET Core 2.0+
https://www.nuget.org/packages/MongoDBMigrations/
PM> Install-Package MongoDBMigrations -Version 1.0.1
How to use
Create a migration by impelmeting the interface IMigration
. Best practice for the version is to use Semantic Versioning but ultimately it is up to you. You could simply use the patch version to count the number of migrations. If there is a duplicate for a specific type an exception is thrown on initialization.
This is the simple migration template. Method Up
used for migrate your database forward and Down
to rollback thus these methods must do the opposite things. Please keep it in mind.
//Create migration
public class MyTestMigration : IMigration
{
public Verstion Version => new Version(1,1,0);
public string Name => "Some descrioption about this migration.";
public void Up(IMongoDatabase database)
{
// ...
}
public void Down(IMongoDatabase database)
{
// ...
}
}
Use next code for initialize MigrationRunner
and start migration.
var options = new MigrationRunnerOptions
{
ConnectionString = CONNECTION_STRING,
DatabaseName = DATABASE
};
//Create instance of runner
var runner = new MigrationRunner(options);
//Find and set assembly with our migrations. If you don't call this method, runner try to find migrations in assembly from which the call is made
runner.Locator.LookInAssemblyOfType<MyTestMigration>();
//Start migration to version 1.1.0 when you don't need result
runner.UpdateTo(new Version(1,1,0));
//Start migration to version 1.0.0 and getting result of each migration between current and target versions
var result = runner.UpdateTo(new Version(1,0,0));
You also can get progress of migration process, just subscribe to MigrationApplied
event
runner.MigrationApplied += Handle;
var result = runner.UpdateTo(new Version(1, 1, 0));
runner.MigrationApplied -= Handle;
where Handle
is:
private void Handle(object sender, MigrationResult result)
{
//Result handling
Debug.WriteLine(result.Message);
}
If you not test your migration yet, mark it by IgnoreMigration
attribute, and runner will skip it.
You can't check if database is outdated by calling runner.Status.IsNotLatestVersion(newestVersion))
or runner.Status.ThrowIfNotLatestVersion(newestVersion)
. The last one throw DatabaseOutdatedExcetion
when database is outdated.
Tips
- Use {migrationVerstion}_{migrationName}.cs pattern of you migration classes.
- Save you migrations in non-production assamblies and use method
LookInAssemblyOfType<T>()
ofMigratiotionLocator
for find them. - Keep migrations as simple as possible
- Do not couple migrations to your domain types, they will be brittle to change, and the point of a migration is to update the data representation when your model changes.
- Stick to the mongo BsonDocument interface or use javascript based mongo commands for migrations, much like with SQL, the mongo javascript API is less likely to change which might break migrations
- Add an application startup check that the database is at the correct version (I plan to implement helpers in feature releases)
- Write tests of your migrations, TDD them from existing data scenarios to new forms. Use
IgnoreMigration
attribute while WIP. - Automate the deployment of migrations (I plan to implement helpers in feature releases)
License
MongoDbMigrations is licensed under MIT. Refer to license.txt for more information.
Free Software, Hell Yeah!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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.1 is compatible. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
-
.NETCoreApp 2.1
- MongoDB.Driver (>= 2.7.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on MongoDBMigrations:
Package | Downloads |
---|---|
PiBox.Plugins.Persistence.MongoDb
PiBox is a `service hosting framework` that allows `.net devs` to `decorate their services with behaviours or functionality (think of plugins) while only using minimal configuration`. |
|
POKA.Utils.Infrastructure.MongoDb
Package Description |
|
FappCommon.Mongo4Test
Declare set of class to easily use MongoDb (ussing Mongo2Go) for testing purpose |
GitHub repositories
This package is not used by any popular GitHub repositories.