Skip to content

Commit

Permalink
Add support for Migrations #62 (#106)
Browse files Browse the repository at this point in the history
* Add support for Migrations #62
  • Loading branch information
KrzysztofPajak committed Sep 7, 2021
1 parent 8a98a09 commit c15988d
Show file tree
Hide file tree
Showing 24 changed files with 594 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public class WkPdfService : IPdfService
private readonly IViewRenderService _viewRenderService;
private readonly ILanguageService _languageService;
private readonly IRepository<Download> _downloadRepository;
private readonly IDatabaseContext _dbContext;
private readonly IStoreFilesContext _storeFilesContext;

public WkPdfService(IGeneratePdf generatePdf, IViewRenderService viewRenderService, IRepository<Download> downloadRepository,
ILanguageService languageService, IDatabaseContext dbContext)
ILanguageService languageService, IStoreFilesContext storeFilesContext)
{
_generatePdf = generatePdf;
_viewRenderService = viewRenderService;
_languageService = languageService;
_downloadRepository = downloadRepository;
_dbContext = dbContext;
_storeFilesContext = storeFilesContext;
}

public async Task PrintOrdersToPdf(Stream stream, IList<Order> orders, string languageId = "", string vendorId = "")
Expand Down Expand Up @@ -122,7 +122,7 @@ public async Task<string> SaveOrderToBinary(Order order, string languageId, stri
ContentType = "application/pdf",
};

download.DownloadObjectId = await _dbContext.GridFSBucketUploadFromBytesAsync(download.Filename, ms.ToArray());
download.DownloadObjectId = await _storeFilesContext.BucketUploadFromBytesAsync(download.Filename, ms.ToArray());
await _downloadRepository.InsertAsync(download);

//TODO
Expand Down
12 changes: 6 additions & 6 deletions src/Business/Grand.Business.Storage/Services/DownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class DownloadService : IDownloadService
#region Fields

private readonly IRepository<Download> _downloadRepository;
private readonly IDatabaseContext _dbContext;
private readonly IStoreFilesContext _storeFilesContext;
private readonly IMediator _mediator;

#endregion
Expand All @@ -28,14 +28,14 @@ public partial class DownloadService : IDownloadService
/// Ctor
/// </summary>
/// <param name="downloadRepository">Download repository</param>
/// <param name="dbContext">DbContext</param>
/// <param name="storeFilesContext">Store Files Context</param>
/// <param name="mediator">Mediator</param>
public DownloadService(IRepository<Download> downloadRepository,
IDatabaseContext dbContext,
IStoreFilesContext storeFilesContext,
IMediator mediator)
{
_downloadRepository = downloadRepository;
_dbContext = dbContext;
_storeFilesContext = storeFilesContext;
_mediator = mediator;
}

Expand All @@ -62,7 +62,7 @@ public virtual async Task<Download> GetDownloadById(string downloadId)

protected virtual async Task<byte[]> DownloadAsBytes(string objectId)
{
var binary = await _dbContext.GridFSBucketDownload(objectId);
var binary = await _storeFilesContext.BucketDownload(objectId);
return binary;
}
/// <summary>
Expand Down Expand Up @@ -96,7 +96,7 @@ public virtual async Task InsertDownload(Download download)
throw new ArgumentNullException(nameof(download));
if (!download.UseDownloadUrl)
{
download.DownloadObjectId = await _dbContext.GridFSBucketUploadFromBytesAsync(download.Filename, download.DownloadBinary);
download.DownloadObjectId = await _storeFilesContext.BucketUploadFromBytesAsync(download.Filename, download.DownloadBinary);
}

download.DownloadBinary = null;
Expand Down
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;
}
}
}
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);
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
using Grand.Business.System.Services.ExportImport;
using Grand.Business.System.Services.Installation;
using Grand.Business.System.Services.MachineNameProvider;
using Grand.Business.System.Services.Migrations;
using Grand.Business.System.Services.Reports;
using Grand.Domain.Data;
using Grand.Infrastructure;
using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Migrations;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -91,6 +93,8 @@ private void RegisterInstallService(IServiceCollection serviceCollection)
serviceCollection.AddScoped<IInstallationLocalizedService, InstallationLocalizedService>();
serviceCollection.AddScoped<IInstallationService, InstallationService>();
}

serviceCollection.AddScoped<IMigrationProcess, MigrationProcess>();
}

private void RegisterExportImportService(IServiceCollection serviceCollection)
Expand Down
5 changes: 3 additions & 2 deletions src/Core/Grand.Domain/Data/IDatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ namespace Grand.Domain.Data
{
public interface IDatabaseContext
{
string ConnectionString { get; }
IQueryable<T> Table<T>(string collectionName);
Task<byte[]> GridFSBucketDownload(string id);
Task<string> GridFSBucketUploadFromBytesAsync(string filename, byte[] source);
Task<bool> DatabaseExist(string connectionString);
Task CreateTable(string name, string collation);
Task DeleteTable(string name);
Task CreateIndex<T>(IRepository<T> repository, OrderBuilder<T> orderBuilder, string indexName, bool unique = false) where T : BaseEntity;
Task DeleteIndex<T>(IRepository<T> repository, string indexName) where T : BaseEntity;
}
}
10 changes: 10 additions & 0 deletions src/Core/Grand.Domain/Data/IStoreFilesContext.cs
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);
}
}
27 changes: 27 additions & 0 deletions src/Core/Grand.Domain/Data/MigrationDb.cs
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; }
}
}
Loading

0 comments on commit c15988d

Please sign in to comment.