forked from SRoddis/Mongo.Migration
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from esleybonomo/feature/EB-Advanced-Migration
Feature/eb advanced migration
- Loading branch information
Showing
21 changed files
with
652 additions
and
16 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
public class ApplicationLogging | ||
{ | ||
private static ILoggerFactory _factory = null; | ||
|
||
public static void ConfigureLogger(ILoggerFactory factory) | ||
{ | ||
factory.AddFile("logs/migration/log-migration.log"); | ||
} | ||
|
||
/// <summary> | ||
/// Cria uma instancia do logFactory | ||
/// </summary> | ||
/// <param name="equipment">Objeto com os dados do equipamento</param> | ||
/// <returns>Instancia da logger Factory</returns> | ||
public static ILoggerFactory LoggerFactory | ||
{ | ||
get | ||
{ | ||
if (_factory == null) | ||
{ | ||
_factory = new LoggerFactory(); | ||
ConfigureLogger(_factory); | ||
} | ||
return _factory; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Retorna uma instancia do ILogger configurada para ser usada nas migrações | ||
/// </summary> | ||
/// <returns>Instancia do ILogger</returns> | ||
public static ILogger CreateLogger() => LoggerFactory.CreateLogger("logger"); | ||
} |
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,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
|
||
namespace Mongo.Migration.Migrations | ||
{ | ||
public abstract class AdvancedMigration : IAdvancedMigration | ||
{ | ||
protected AdvancedMigration(string version, string collection) | ||
{ | ||
Version = version; | ||
CollectionName = collection; | ||
} | ||
|
||
public string Version { get; } | ||
|
||
public string CollectionName { get; } | ||
|
||
public Type Type => typeof(AdvancedMigration); | ||
|
||
public virtual void Up(IMongoDatabase db) { | ||
var bulk = new List<WriteModel<BsonDocument>>(); | ||
var collection = db.GetCollection<BsonDocument>(CollectionName); | ||
|
||
collection.FindSync(_ => true).ToList().ForEach(document => | ||
{ | ||
Up(document); | ||
var update = new ReplaceOneModel<BsonDocument>( | ||
new BsonDocument {{"_id", document["_id"]}}, | ||
document | ||
); | ||
bulk.Add(update); | ||
}); | ||
|
||
if (bulk.Count > 0) | ||
{ | ||
collection.BulkWrite(bulk); | ||
} | ||
} | ||
|
||
public virtual void Down(IMongoDatabase db) { | ||
var bulk = new List<WriteModel<BsonDocument>>(); | ||
var collection = db.GetCollection<BsonDocument>(CollectionName); | ||
|
||
collection.FindSync(_ => true).ToList().ForEach(document => | ||
{ | ||
Down(document); | ||
var update = new ReplaceOneModel<BsonDocument>( | ||
new BsonDocument {{"_id", document["_id"]}}, | ||
document | ||
); | ||
bulk.Add(update); | ||
}); | ||
|
||
if (bulk.Count > 0) | ||
{ | ||
collection.BulkWrite(bulk); | ||
} | ||
} | ||
|
||
public abstract void Up(BsonDocument document); | ||
|
||
public abstract void Down(BsonDocument document); | ||
} | ||
} |
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,105 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.Logging; | ||
using Mongo.Migration.Migrations.Locators; | ||
using Mongo.Migration.Services; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
|
||
namespace Mongo.Migration.Migrations | ||
{ | ||
internal class AdvancedMigrationRunner : IAdvancedMigrationRunner | ||
{ | ||
private ILogger _logger; | ||
public IAdvancedMigrationLocator _migrationLocator { get; } | ||
|
||
private readonly IVersionService _versionService; | ||
|
||
private IMongoDatabase _db; | ||
|
||
private IMongoCollection<BsonDocument> _migrationshistory; | ||
|
||
public AdvancedMigrationRunner(IAdvancedMigrationLocator migrationLocator, IVersionService versionService) | ||
{ | ||
_migrationLocator = migrationLocator; | ||
_versionService = versionService; | ||
_logger = ApplicationLogging.CreateLogger(); | ||
} | ||
|
||
public void Run(IMongoDatabase db, string runningVersion) | ||
{ | ||
_logger.LogInformation($"Iniciando a execução das Migrações ..."); | ||
|
||
_db = db; | ||
_migrationshistory = db.GetCollection<BsonDocument>("_migrationshistory"); | ||
|
||
var migrations = _migrationLocator.GetMigrations(typeof(AdvancedMigration)) ?? Enumerable.Empty<IAdvancedMigration>(); | ||
var migrationsToDowngrade = new List<IAdvancedMigration>(); | ||
|
||
foreach (var migration in migrations) | ||
{ | ||
|
||
var migrationsInDb = _migrationshistory.FindSync(CreateQueryForMigration(migration.GetType().ToString())).ToList(); | ||
|
||
if (migrationsInDb.Count() > 0) | ||
{ | ||
foreach (var document in migrationsInDb) | ||
{ | ||
if (document["productVersion"].ToString().CompareTo(runningVersion) <= 0) | ||
continue; | ||
else if (document["productVersion"].ToString().CompareTo(runningVersion) > 0) | ||
{ | ||
migrationsToDowngrade.Add(migration); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
if (runningVersion.CompareTo(migration.Version) >= 0) | ||
{ | ||
try | ||
{ | ||
_logger.LogInformation("Iniciando a execução da Migração Up: {0}:{1} ", migration.GetType().ToString(), migration.Version); | ||
|
||
migration.Up(_db); | ||
_migrationshistory.InsertOne(new BsonDocument { { "migrationId", migration.GetType().ToString() }, { "productVersion", migration.Version } }); | ||
|
||
_logger.LogInformation("Migração Up executada com sucesso: {0}:{1} ", migration.GetType().ToString(), migration.Version); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Erro ao executar upgrade da migração {0}:{1} ", migration.GetType().ToString(), migration.Version); | ||
} | ||
} | ||
} | ||
} | ||
|
||
migrationsToDowngrade.Reverse(); | ||
foreach (var migration in migrationsToDowngrade) | ||
{ | ||
try | ||
{ | ||
_logger.LogInformation("Iniciando a execução da Migração Down: {0}:{1} ", migration.GetType().ToString(), migration.Version); | ||
|
||
migration.Down(_db); | ||
_migrationshistory.DeleteOne(Builders<BsonDocument>.Filter.Eq("migrationId", migration.GetType().ToString())); | ||
|
||
_logger.LogInformation("Migração Down executada com sucesso: {0}:{1} ", migration.GetType().ToString(), migration.Version); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Erro ao executar downgrade da migração {0}:{1} ", migration.GetType().ToString(), migration.Version); | ||
} | ||
} | ||
|
||
_logger.LogInformation($"Migrações finalizadas."); | ||
} | ||
|
||
private FilterDefinition<BsonDocument> CreateQueryForMigration( | ||
string type) | ||
{ | ||
return Builders<BsonDocument>.Filter.Eq("migrationId", type); | ||
} | ||
} | ||
} |
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,47 @@ | ||
using Microsoft.Extensions.Options; | ||
using Mongo.Migration.Startup.DotNetCore; | ||
using MongoDB.Driver; | ||
|
||
namespace Mongo.Migration.Migrations | ||
{ | ||
internal class DatabaseMigrationRunner : IDatabaseMigrationRunner | ||
{ | ||
private readonly IMongoClient _client; | ||
|
||
private readonly IAdvancedMigrationRunner _migrationRunner; | ||
|
||
private readonly string _databaseName; | ||
|
||
private readonly string _runningVersion; | ||
|
||
private readonly IOptions<MongoMigrationSettings> _options; | ||
|
||
private const int CONNECTION_CHECK_TIMEOUT = 1000; | ||
public DatabaseMigrationRunner( | ||
IOptions<MongoMigrationSettings> options, | ||
IAdvancedMigrationRunner migrationRunner) | ||
: this( | ||
new MongoClient(options.Value.ConnectionString), | ||
migrationRunner, | ||
options.Value.RunningVersion) | ||
{ | ||
_options = options; | ||
_databaseName = options.Value.Database; | ||
} | ||
|
||
public DatabaseMigrationRunner( | ||
IMongoClient client, | ||
IAdvancedMigrationRunner migrationRunner, | ||
string runningVersion) | ||
{ | ||
_runningVersion = runningVersion; | ||
_client = client; | ||
_migrationRunner = migrationRunner; | ||
} | ||
|
||
public void RunAll() | ||
{ | ||
_migrationRunner.Run(_client.GetDatabase(_databaseName), _runningVersion); | ||
} | ||
} | ||
} |
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,23 @@ | ||
using System; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
|
||
namespace Mongo.Migration.Migrations | ||
{ | ||
public interface IAdvancedMigration | ||
{ | ||
string Version { get; } | ||
|
||
string CollectionName { get; } | ||
|
||
Type Type { get; } | ||
|
||
void Up(IMongoDatabase db); | ||
|
||
void Down(IMongoDatabase db); | ||
|
||
void Up(BsonDocument document); | ||
|
||
void Down(BsonDocument document); | ||
} | ||
} |
Oops, something went wrong.