-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
F/226 simple data migrations system #234
Open
DavidPL-coder
wants to merge
18
commits into
Devscord-Team:master
Choose a base branch
from
DavidPL-coder:F/226-simple-data-migrations-system
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
56f6c20
Add packages to implement migrations system.
DavidPL-coder 8f28102
Remove unnecessary database configuration.
DavidPL-coder 45e74db
Add data migrations system for MongoDB database.
DavidPL-coder 14563f7
Do the MongoDB configuration after the logger configuration.
DavidPL-coder 952b9f3
Add the field with copy of Name field in ConfigurationItems db collec…
DavidPL-coder 9a38bb9
Upgrade database to 1.2.0 version as example of using the migrations …
DavidPL-coder 4211c3f
Upgrade databse to 1.3.0 version as example of using the migrations s…
DavidPL-coder 3302911
Downgrade database to 1.2.0 version as an example of using migrations.
DavidPL-coder b4aed64
Unapply all migrations as an example of using migrations.
DavidPL-coder ab82fba
Upgrade database to the latest version as an example of using migrati…
DavidPL-coder e46eaca
Add validation in M120_CreateBackupOfInitEventsMigration.
DavidPL-coder 5a8b3b7
Add checking existing of database and migrations before using of the …
DavidPL-coder 1fd7898
Remove unnecessary nuget packages.
DavidPL-coder b32852c
Remove unnecessary fields (which were used as example of using migrat…
DavidPL-coder e031e24
Remove migrations with 1.2.0 version (which were used as an example c…
DavidPL-coder 6f82b4a
Remove example migration with 1.1.0 version and related things.
DavidPL-coder db679e0
Add migrations folder for the next migrations.
DavidPL-coder 6e53ff5
Move MongoConventions class to other file.
DavidPL-coder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
using MongoDB.Bson; | ||
using Microsoft.Extensions.Configuration; | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization.Conventions; | ||
using MongoDB.Driver; | ||
using MongoDBMigrations; | ||
using Serilog; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Watchman.Integrations.Database.MongoDB | ||
{ | ||
|
@@ -10,13 +16,14 @@ public static class MongoConfiguration | |
{ | ||
private static bool _initialized; | ||
|
||
public static void Initialize() | ||
public static void Initialize(IConfigurationRoot configuration) | ||
{ | ||
if (_initialized) | ||
{ | ||
return; | ||
} | ||
RegisterConventions(); | ||
TryToMigrateDatabase(configuration); | ||
_initialized = true; | ||
} | ||
|
||
|
@@ -25,14 +32,53 @@ private static void RegisterConventions() | |
ConventionRegistry.Register("DevscordConventions", new MongoConventions(), x => true); | ||
} | ||
|
||
private class MongoConventions : IConventionPack | ||
private static void TryToMigrateDatabase(IConfigurationRoot configuration) | ||
{ | ||
public IEnumerable<IConvention> Conventions => new List<IConvention> | ||
var connectionString = configuration.GetConnectionString("Mongo"); | ||
var databaseName = connectionString.Split('/').Last(); | ||
var assemblyForMigrations = Assembly.GetExecutingAssembly(); | ||
|
||
if (ShouldNotDoMigrations(connectionString, databaseName, assemblyForMigrations)) | ||
{ | ||
return; | ||
} | ||
|
||
var result = new MigrationEngine() | ||
.UseDatabase(connectionString, databaseName) | ||
.UseAssembly(assemblyForMigrations) | ||
.UseSchemeValidation(enabled: false) | ||
.UseProgressHandler(context => | ||
Log.Information($@"The migration with name ""{context.MigrationName}"" from {context.TargetVersion} version was processed as {context.CurrentNumber} from all {context.TotalCount} migrations.")) | ||
.Run(); | ||
|
||
if (!result.Success) | ||
{ | ||
new IgnoreExtraElementsConvention(true), | ||
new EnumRepresentationConvention(BsonType.String), | ||
new CamelCaseElementNameConvention() | ||
}; | ||
// TODO: Do something when migrations throw exceptions. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Właśnie co zrobić gdy wystąpi wyjątek podczas migracji? Zakończyć aplikacje z odpowiednią informacją? Poza tym swoją drogą jak teraz patrze w dokumentacje tej paczki to chyba będę musiał użyć try catch aby to obsłużyć zamiast tego if'a. |
||
} | ||
|
||
Log.Information($"The current version of the database after data migrations is {result.CurrentVersion}."); | ||
} | ||
|
||
private static bool ShouldNotDoMigrations(string connectionString, string databaseName, Assembly assemblyForMigrations) | ||
{ | ||
var migrationInterfaceType = typeof(IMigration); | ||
var areNotThereMigrations = assemblyForMigrations.GetTypes() | ||
.All(t => t.GetInterfaces().Contains(migrationInterfaceType) == false); | ||
|
||
if (areNotThereMigrations) | ||
{ | ||
Log.Warning("The app doesn't see any migration to use."); | ||
} | ||
|
||
var mongoClient = new MongoClient(new MongoUrl(connectionString)); | ||
var isNotDatabaseExisting = mongoClient.ListDatabaseNames().ToList().All(x => x != databaseName); | ||
|
||
if (isNotDatabaseExisting) | ||
{ | ||
Log.Warning("The database doesn't exist yet (so data migrations won't be used)."); | ||
} | ||
|
||
return areNotThereMigrations || isNotDatabaseExisting; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Watchman.Integrations/Database/MongoDB/MongoConventions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization.Conventions; | ||
using System.Collections.Generic; | ||
|
||
namespace Watchman.Integrations.Database.MongoDB | ||
{ | ||
internal class MongoConventions : IConventionPack | ||
{ | ||
public IEnumerable<IConvention> Conventions => new List<IConvention> | ||
{ | ||
new IgnoreExtraElementsConvention(true), | ||
new EnumRepresentationConvention(BsonType.String), | ||
new CamelCaseElementNameConvention() | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Czy takie logi są wystarczające? Podałem taki log jako przykład, ale nie wiem czy trzeba coś zmienić. Inne logi tutaj też są wystarczające?