diff options
author | crupest <crupest@outlook.com> | 2021-02-12 22:10:56 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-02-12 22:13:51 +0800 |
commit | 1aec1eb3f0822e17793d43e040efdf127ea8e561 (patch) | |
tree | 3ebd62f2b631538105f64a5fb0dcb28c672a6c82 /BackEnd/Timeline/Services | |
parent | 85ac5c4513bd464d29696bc99fcda45ac228d1a2 (diff) | |
download | timeline-1aec1eb3f0822e17793d43e040efdf127ea8e561.tar.gz timeline-1aec1eb3f0822e17793d43e040efdf127ea8e561.tar.bz2 timeline-1aec1eb3f0822e17793d43e040efdf127ea8e561.zip |
feat: Add databse custom migration service.
Diffstat (limited to 'BackEnd/Timeline/Services')
4 files changed, 149 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Services/Migration/CustomMigrationManager.cs b/BackEnd/Timeline/Services/Migration/CustomMigrationManager.cs new file mode 100644 index 00000000..ba86e10b --- /dev/null +++ b/BackEnd/Timeline/Services/Migration/CustomMigrationManager.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic;
+using System.Threading.Tasks;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Logging;
+using Timeline.Entities;
+
+namespace Timeline.Services.Migration
+{
+ public interface ICustomMigrationManager
+ {
+ Task Migrate();
+ }
+
+ public class CustomMigrationManager : ICustomMigrationManager
+ {
+ private IEnumerable<ICustomMigration> _migrations;
+ private DatabaseContext _database;
+
+ private ILogger<CustomMigrationManager> _logger;
+
+ public CustomMigrationManager(IEnumerable<ICustomMigration> migrations, DatabaseContext database, ILogger<CustomMigrationManager> logger)
+ {
+ _migrations = migrations;
+ _database = database;
+ _logger = logger;
+ }
+
+ public async Task Migrate()
+ {
+ foreach (var migration in _migrations)
+ {
+ var name = migration.GetName();
+ var did = await _database.Migrations.AnyAsync(m => m.Name == name);
+
+ _logger.LogInformation("Found custom migration '{0}'. Did: {1}.", name, did);
+
+ if (!did)
+ {
+ _logger.LogInformation("Begin custom migration '{0}'.", name);
+
+ await using var transaction = await _database.Database.BeginTransactionAsync();
+
+ await migration.Execute(_database);
+
+ _database.Migrations.Add(new MigrationEntity { Name = name });
+ await _database.SaveChangesAsync();
+
+ await transaction.CommitAsync();
+
+ _logger.LogInformation("End custom migration '{0}'.", name);
+ }
+ }
+ }
+ }
+}
diff --git a/BackEnd/Timeline/Services/Migration/ICustomMigration.cs b/BackEnd/Timeline/Services/Migration/ICustomMigration.cs new file mode 100644 index 00000000..1f47df1e --- /dev/null +++ b/BackEnd/Timeline/Services/Migration/ICustomMigration.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks;
+using Timeline.Entities;
+
+namespace Timeline.Services.Migration
+{
+ public interface ICustomMigration
+ {
+ string GetName();
+ Task Execute(DatabaseContext database);
+ }
+}
diff --git a/BackEnd/Timeline/Services/Migration/MigationServiceCollectionExtensions.cs b/BackEnd/Timeline/Services/Migration/MigationServiceCollectionExtensions.cs new file mode 100644 index 00000000..0e6f6c0a --- /dev/null +++ b/BackEnd/Timeline/Services/Migration/MigationServiceCollectionExtensions.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.DependencyInjection;
+
+namespace Timeline.Services.Migration
+{
+ public static class MigrationServiceCollectionExtensions
+ {
+ public static IServiceCollection AddCustomMigration(this IServiceCollection services)
+ {
+ services.AddScoped<ICustomMigrationManager, CustomMigrationManager>();
+ services.AddScoped<ICustomMigration, TimelinePostContentToDataMigration>();
+ return services;
+ }
+ }
+}
\ No newline at end of file diff --git a/BackEnd/Timeline/Services/Migration/TimelinePostContentToDataMigration.cs b/BackEnd/Timeline/Services/Migration/TimelinePostContentToDataMigration.cs new file mode 100644 index 00000000..de2e2183 --- /dev/null +++ b/BackEnd/Timeline/Services/Migration/TimelinePostContentToDataMigration.cs @@ -0,0 +1,69 @@ +using System.Text;
+using System.Threading.Tasks;
+using Microsoft.EntityFrameworkCore;
+using SixLabors.ImageSharp;
+using Timeline.Entities;
+using Timeline.Models;
+
+namespace Timeline.Services.Migration
+{
+ public class TimelinePostContentToDataMigration : ICustomMigration
+ {
+ private readonly IDataManager _dataManager;
+
+ public TimelinePostContentToDataMigration(IDataManager dataManager)
+ {
+ _dataManager = dataManager;
+ }
+
+ public string GetName() => "TimelinePostContentToData";
+
+ public async Task Execute(DatabaseContext database)
+ {
+#pragma warning disable CS0618
+ var postEntities = await database.TimelinePosts.ToListAsync();
+
+ foreach (var postEntity in postEntities)
+ {
+ if (postEntity.Content is null)
+ {
+ postEntity.Deleted = true;
+ }
+ else
+ {
+ if (postEntity.ContentType == "text")
+ {
+ var tag = await _dataManager.RetainEntry(Encoding.UTF8.GetBytes(postEntity.Content), false);
+ database.TimelinePostData.Add(new TimelinePostDataEntity
+ {
+ DataTag = tag,
+ Kind = MimeTypes.TextPlain,
+ Index = 0,
+ PostId = postEntity.Id,
+ LastUpdated = postEntity.LastUpdated
+ });
+ }
+ else
+ {
+ var data = await _dataManager.GetEntryAndCheck(postEntity.Content, "Old image content does not have corresponding data with the tag.");
+ var format = Image.DetectFormat(data);
+ database.TimelinePostData.Add(new TimelinePostDataEntity
+ {
+ DataTag = postEntity.Content,
+ Kind = format.DefaultMimeType,
+ Index = 0,
+ PostId = postEntity.Id,
+ LastUpdated = postEntity.LastUpdated
+ });
+ }
+ }
+ postEntity.Content = null;
+ postEntity.ContentType = null;
+ postEntity.ExtraContent = null;
+ }
+
+ await database.SaveChangesAsync();
+#pragma warning restore CS0618
+ }
+ }
+}
|