![]() |
VOOZH | about |
dotnet add package MongoDBMigrations --version 2.2.0
NuGet\Install-Package MongoDBMigrations -Version 2.2.0
<PackageReference Include="MongoDBMigrations" Version="2.2.0" />
<PackageVersion Include="MongoDBMigrations" Version="2.2.0" />Directory.Packages.props
<PackageReference Include="MongoDBMigrations" />Project file
paket add MongoDBMigrations --version 2.2.0
#r "nuget: MongoDBMigrations, 2.2.0"
#:package MongoDBMigrations@2.2.0
#addin nuget:?package=MongoDBMigrations&version=2.2.0Install as a Cake Addin
#tool nuget:?package=MongoDBMigrations&version=2.2.0Install as a Cake Tool
You can support me in the development of this useful library. I have big plans you can find them in todo below. I will appreciate pizza and beer;)
Just follow the link, and donate me any amount you want 😃
MongoDBMigrations using the official MongoDB C# Driver to migrate your documents in database. This library supports on-premis Mongo instances, Azure CosmosDB (MongoAPI) and AWS DocumentDB.
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 manipulation
5. Removing collections / data
Added: SSH support
Added: TLS/SSL support (experimental feature)
Added: Custom specification collection name
Added: New overload for the method UseDatabase
Changed: Upgraded to the newest version of .NET Mongo Driver
Chnaged: Fixed list of bugs
Guys, unfortunately, I can't spend much time on this project, that is way since now you are able to create a pull request and develop some features, which can be useful. I will review that requests and merge them. Please don't forget about unit tests 😂 I hope this step will speed up the evolution of this project. And just to set a vector below I specified a list of preferred features for the community:
MongoDBMigrations tested with .NET Core 2.0+ https://www.nuget.org/packages/MongoDBMigrations/
PM> Install-Package MongoDBMigrations -Version 2.2.0
Create a migration by implementing the interface IMigration. The 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 a simple migration template. Method Up is used to migrate your database forward and Down to rollback thus these methods must do the opposite things. Please keep it in mind. You can use any version number greater than 0.0.0. In case you already have some migrations you should choose a version upper than the existing ones.
//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)
{
// ...
}
}
| Step #0 | Step #1 | Step #2 | Step #3 | Step #4 | Step #5 |
|---|---|---|---|---|---|
| Create an engine | Database, connection features | Migration classes | Validations | Hadling features | Excecution |
new MigrationEngine() |
UseSshTunnel(...) UseTls(...) UseDatabase(...) |
UseAssemblyOfType(...) UseAssemblyOfType<T>() UseAssembly(...) |
UseSchemeValidation(...) |
UseProgressHandler(...) UseCancelationToken(...) UseCustomSpecificationCollectionName(...) |
Run() |
Use the following code for initialize MigrationEngine and start migration.
new MigrationEngine()
.UseSshTunnel(sshServerAdress, user, privateKeyFileStream, mongoAdress, keyFilePassPhrase) //Use if you want to connect to your DB via SSH tunel. keyFilePassPhrase is optional.
.UseTls(cert) //Use if your database requires TLS. Please use X509Certificate2 instance as a cert value
.UseDatabase(connectionString, databaseName) //Required to use specific db
.UseAssembly(assemblyWithMigrations) //Required
.UseSchemeValidation(bool, string) //Optional if you want to ensure that all documents in collections, that will be affected in the current run, has a consistent structure. Set a true and absolute path to *.csproj file with migration classes or just false.
.UseCancelationToken(token) //Optional if you wanna have the possibility to cancel the migration process. Might be useful when you have many migrations and some interaction with the user.
.UseProgressHandler(Action<> action) // Optional some delegate that will be called each migration
.Run(targetVersion) // Execution call. Might be called without targetVersion, in that case, the engine will choose the latest available version.
In case if the handler does not found and validation has failed - migration process will be canceled automatically.
If you haven't tested your migration yet, mark it with IgnoreMigration attribute, and the runner will skip it.
You can't check if the database is outdated by dint of static class MongoDatabaseStateChecker
| Method | Description |
|---|---|
ThrowIfDatabaseOutdated(connectionString, databaseName, migrationAssambly, emulation) |
Check is DB outdated and throw DatabaseOutdatedExcetion if yes. MigrationAssambly is optional. If not set method will find migration in executing assembly. Emulation has a None value by default for Mongo databases, but you should use the AzureCosmos option in case of Azure Cosmos DB |
IsDatabaseOutdated(connectionString, databaseName, migrationAssambly, emulation) |
Returns true if DB outdated (you have unapplied migrations) otherwise false. MigrationAssambly is optional. If not set method will find migration in executing assembly. Emulation has a None value by default for Mongo databases, but you should use the AzureCosmos option in case of Azure Cosmos DB |
Begins from v2.1.0 this library supports databases in Azure CosmosDB service. There might be two cases:
applied in _migrations collection. If you don't have this index please create them prior you strart the migration run.Bagins from v2.2.0 this library supports databases in AWS DocumentDB service.
Now you have a chance to integrate the mongo database migration engine in your CI pipeline. In repository you can found MongoDBRunMigration.ps1 script. This approach allows you to have some backup rollback in case of any failure during migration.
Call the following commands prior to using this PS1 file:
Set-Alias mongodump <path_without_spaces>
Set-Alias mongorestore <path_without_spaces>
Paths should lead to executable files (*.exe). Please, modify the PS1 file if you have any authorization in your database.
| Parameter | Description |
|---|---|
| connectionString | Database connection string e.g. localhost:27017 |
| databaseName | Name of the database |
| backupLocation | Folder for the backup that will be created befor migration |
| migrationsAssemblyPath | Path to the assembly with migration classes |
Tips
LookInAssemblyOfType<T>() of MigratiotionLocator to finding them.IgnoreMigrationattribute while WIP.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 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 4 NuGet packages that depend on MongoDBMigrations:
| Package | Downloads |
|---|---|
|
Dosaic.Plugins.Persistence.MongoDb
A plugin-first dotnet framework for rapidly building anything hosted in the web. |
|
|
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 |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 2.2.0 | 1,012,062 | 2/1/2022 | |
| 2.1.0 | 350,716 | 11/20/2020 | |
| 2.0.0 | 132,200 | 3/22/2020 | |
| 1.1.2 | 107,327 | 10/4/2019 | 1.1.2 is deprecated because it is no longer maintained. |
| 1.1.1 | 3,278 | 9/20/2019 | 1.1.1 is deprecated because it is no longer maintained. |
| 1.1.0 | 8,438 | 11/21/2018 | 1.1.0 is deprecated because it is no longer maintained. |
| 1.0.1 | 3,328 | 8/16/2018 | 1.0.1 is deprecated because it is no longer maintained. |
| 1.0.0 | 3,307 | 8/9/2018 | 1.0.0 is deprecated because it is no longer maintained. |