-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add support for Migrations #62
- Loading branch information
1 parent
8a98a09
commit c15988d
Showing
24 changed files
with
594 additions
and
35 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
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
39 changes: 39 additions & 0 deletions
39
src/Business/Grand.Business.System/Services/Migrations/1.1/MigrationUpgradeDbVersion_11.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,39 @@ | ||
using Grand.Domain.Common; | ||
using Grand.Domain.Data; | ||
using Grand.Infrastructure; | ||
using Grand.Infrastructure.Migrations; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Grand.Business.System.Services.Migrations._1._1 | ||
{ | ||
public class MigrationUpgradeDbVersion_11 : IMigration | ||
{ | ||
|
||
public int Priority => 0; | ||
|
||
public DbVersion Version => new(1, 1); | ||
|
||
public Guid Identity => new("6BDB7093-4C31-4D78-9604-58188DF728D3"); | ||
|
||
public string Name => "Upgrade version of the database to 1.1"; | ||
|
||
/// <summary> | ||
/// Upgrade process | ||
/// </summary> | ||
/// <param name="database"></param> | ||
/// <param name="serviceProvider"></param> | ||
/// <returns></returns> | ||
public bool UpgradeProcess(IDatabaseContext database, IServiceProvider serviceProvider) | ||
{ | ||
var repository = serviceProvider.GetRequiredService<IRepository<GrandNodeVersion>>(); | ||
|
||
var dbversion = repository.Table.ToList().FirstOrDefault(); | ||
dbversion.DataBaseVersion = $"{GrandVersion.SupportedDBVersion}"; | ||
repository.Update(dbversion); | ||
|
||
return true; | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/Business/Grand.Business.System/Services/Migrations/MigrationProcess.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,85 @@ | ||
using Grand.Business.Common.Interfaces.Logging; | ||
using Grand.Domain.Data; | ||
using Grand.Infrastructure.Migrations; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Grand.Business.System.Services.Migrations | ||
{ | ||
public class MigrationProcess : IMigrationProcess | ||
{ | ||
private readonly IDatabaseContext _databaseContext; | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly ILogger _logger; | ||
|
||
private readonly IRepository<MigrationDb> _repositoryMigration; | ||
|
||
public MigrationProcess( | ||
IDatabaseContext databaseContext, | ||
IServiceProvider serviceProvider, | ||
ILogger logger, | ||
IRepository<MigrationDb> repositoryMigration) | ||
{ | ||
_databaseContext = databaseContext; | ||
_serviceProvider = serviceProvider; | ||
_logger = logger; | ||
_repositoryMigration = repositoryMigration; | ||
} | ||
|
||
public virtual MigrationResult RunProcess(IMigration migration) | ||
{ | ||
var result = RunProcessInternal(migration); | ||
try | ||
{ | ||
if (result.Success) | ||
SaveMigration(result); | ||
else | ||
_logger.InsertLog(Domain.Logging.LogLevel.Error, $"Something went wrong during migration process {migration.Name}"); | ||
return result; | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new InvalidOperationException($"Exception run migration {migration.Name}", ex); | ||
} | ||
} | ||
|
||
private MigrationResult RunProcessInternal(IMigration migration) | ||
{ | ||
var model = new MigrationResult { | ||
Success = migration.UpgradeProcess(_databaseContext, _serviceProvider), | ||
Migration = migration, | ||
}; | ||
return model; | ||
} | ||
|
||
private void SaveMigration(MigrationResult migrationResult) | ||
{ | ||
_repositoryMigration.Insert(new MigrationDb() { | ||
Identity = migrationResult.Migration.Identity, | ||
Name = migrationResult.Migration.Name, | ||
Version = migrationResult.Migration.Version.ToString(), | ||
CreatedOnUtc = DateTime.UtcNow, | ||
}); | ||
} | ||
|
||
private IList<MigrationDb> GetMigrationDb() | ||
{ | ||
return _repositoryMigration.Table.ToList(); | ||
} | ||
|
||
public virtual void RunMigrationProcess() | ||
{ | ||
var migrationsDb = GetMigrationDb(); | ||
var migrationManager = new MigrationManager(); | ||
foreach (var item in migrationManager.GetCurrentMigrations()) | ||
{ | ||
if (migrationsDb.FirstOrDefault(x => x.Identity == item.Identity) == null) | ||
{ | ||
RunProcess(item); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
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,10 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Grand.Domain.Data | ||
{ | ||
public interface IStoreFilesContext | ||
{ | ||
Task<byte[]> BucketDownload(string id); | ||
Task<string> BucketUploadFromBytesAsync(string filename, byte[] source); | ||
} | ||
} |
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,27 @@ | ||
using System; | ||
|
||
namespace Grand.Domain.Data | ||
{ | ||
public class MigrationDb : BaseEntity | ||
{ | ||
/// <summary> | ||
/// The unique identity of migration. | ||
/// </summary> | ||
public Guid Identity { get; set; } | ||
|
||
/// <summary> | ||
/// Name of migration. | ||
/// </summary> | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// Db Version | ||
/// </summary> | ||
public string Version { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the date and time of migration creation | ||
/// </summary> | ||
public DateTime CreatedOnUtc { get; set; } | ||
} | ||
} |
Oops, something went wrong.