aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/DatabaseManagement
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-04-27 18:38:26 +0800
committercrupest <crupest@outlook.com>2021-04-27 18:38:26 +0800
commit2cbcd8b63bcd7e3d45cd92baa5bacd828527aea8 (patch)
tree36ef7044778eb0f660d02d08b57c4d0a143b9865 /BackEnd/Timeline/Services/DatabaseManagement
parentdeb02d10e6139bb74a63343e2a8b70fee11bec22 (diff)
downloadtimeline-2cbcd8b63bcd7e3d45cd92baa5bacd828527aea8.tar.gz
timeline-2cbcd8b63bcd7e3d45cd92baa5bacd828527aea8.tar.bz2
timeline-2cbcd8b63bcd7e3d45cd92baa5bacd828527aea8.zip
refactor: Refactor is still on...
Diffstat (limited to 'BackEnd/Timeline/Services/DatabaseManagement')
-rw-r--r--BackEnd/Timeline/Services/DatabaseManagement/DatabaseBackupService.cs11
-rw-r--r--BackEnd/Timeline/Services/DatabaseManagement/DatabaseCustomMigrator.cs17
-rw-r--r--BackEnd/Timeline/Services/DatabaseManagement/IDatabaseBackupService.cs10
-rw-r--r--BackEnd/Timeline/Services/DatabaseManagement/IDatabaseCustomMigration.cs9
-rw-r--r--BackEnd/Timeline/Services/DatabaseManagement/IDatabaseCustomMigrator.cs10
-rw-r--r--BackEnd/Timeline/Services/DatabaseManagement/MigationServiceCollectionExtensions.cs9
-rw-r--r--BackEnd/Timeline/Services/DatabaseManagement/Resource.Designer.cs108
-rw-r--r--BackEnd/Timeline/Services/DatabaseManagement/Resource.resx135
-rw-r--r--BackEnd/Timeline/Services/DatabaseManagement/TimelinePostContentToDataMigration.cs4
9 files changed, 291 insertions, 22 deletions
diff --git a/BackEnd/Timeline/Services/DatabaseManagement/DatabaseBackupService.cs b/BackEnd/Timeline/Services/DatabaseManagement/DatabaseBackupService.cs
index c00b5f95..718edcb1 100644
--- a/BackEnd/Timeline/Services/DatabaseManagement/DatabaseBackupService.cs
+++ b/BackEnd/Timeline/Services/DatabaseManagement/DatabaseBackupService.cs
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Logging;
using System.Globalization;
using System.IO;
using System.Threading;
@@ -7,19 +8,16 @@ using Timeline.Entities;
namespace Timeline.Services.DatabaseManagement
{
- public interface IDatabaseBackupService
- {
- Task BackupAsync(CancellationToken cancellationToken = default);
- }
-
public class DatabaseBackupService : IDatabaseBackupService
{
+ private readonly ILogger<DatabaseBackupService> _logger;
private readonly DatabaseContext _database;
private readonly IPathProvider _pathProvider;
private readonly IClock _clock;
- public DatabaseBackupService(DatabaseContext database, IPathProvider pathProvider, IClock clock)
+ public DatabaseBackupService(ILogger<DatabaseBackupService> logger, DatabaseContext database, IPathProvider pathProvider, IClock clock)
{
+ _logger = logger;
_database = database;
_pathProvider = pathProvider;
_clock = clock;
@@ -32,6 +30,7 @@ namespace Timeline.Services.DatabaseManagement
var fileName = _clock.GetCurrentTime().ToString("yyyy-MM-ddTHH-mm-ss", CultureInfo.InvariantCulture);
var path = Path.Combine(backupDirPath, fileName);
await _database.Database.ExecuteSqlInterpolatedAsync($"VACUUM INTO {path}", cancellationToken);
+ _logger.LogWarning(Resource.DatabaseBackupServiceFinishBackup, path);
}
}
}
diff --git a/BackEnd/Timeline/Services/DatabaseManagement/DatabaseCustomMigrator.cs b/BackEnd/Timeline/Services/DatabaseManagement/DatabaseCustomMigrator.cs
index 2180ad40..20e2c074 100644
--- a/BackEnd/Timeline/Services/DatabaseManagement/DatabaseCustomMigrator.cs
+++ b/BackEnd/Timeline/Services/DatabaseManagement/DatabaseCustomMigrator.cs
@@ -7,17 +7,12 @@ using Timeline.Entities;
namespace Timeline.Services.DatabaseManagement
{
- public interface IDatabaseCustomMigrator
- {
- Task MigrateAsync(CancellationToken cancellationToken = default);
- }
-
public class DatabaseCustomMigrator : IDatabaseCustomMigrator
{
- private IEnumerable<IDatabaseCustomMigration> _migrations;
- private DatabaseContext _database;
+ private readonly IEnumerable<IDatabaseCustomMigration> _migrations;
+ private readonly DatabaseContext _database;
- private ILogger<DatabaseCustomMigrator> _logger;
+ private readonly ILogger<DatabaseCustomMigrator> _logger;
public DatabaseCustomMigrator(IEnumerable<IDatabaseCustomMigration> migrations, DatabaseContext database, ILogger<DatabaseCustomMigrator> logger)
{
@@ -33,11 +28,11 @@ namespace Timeline.Services.DatabaseManagement
var name = migration.GetName();
var isApplied = await _database.Migrations.AnyAsync(m => m.Name == name, cancellationToken);
- _logger.LogInformation("Found custom migration '{0}'. Applied: {1}.", name, isApplied);
+ _logger.LogInformation(Resource.DatabaseCustomMigratorFoundMigration, name, isApplied);
if (!isApplied)
{
- _logger.LogWarning("Begin custom migration '{0}'.", name);
+ _logger.LogWarning(Resource.DatabaseCustomMigratorBeginMigration, name);
await using var transaction = await _database.Database.BeginTransactionAsync(cancellationToken);
@@ -48,7 +43,7 @@ namespace Timeline.Services.DatabaseManagement
await transaction.CommitAsync(cancellationToken);
- _logger.LogWarning("End custom migration '{0}'.", name);
+ _logger.LogWarning(Resource.DatabaseCustomMigratorFinishMigration, name);
}
}
}
diff --git a/BackEnd/Timeline/Services/DatabaseManagement/IDatabaseBackupService.cs b/BackEnd/Timeline/Services/DatabaseManagement/IDatabaseBackupService.cs
new file mode 100644
index 00000000..88f3d9b6
--- /dev/null
+++ b/BackEnd/Timeline/Services/DatabaseManagement/IDatabaseBackupService.cs
@@ -0,0 +1,10 @@
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Timeline.Services.DatabaseManagement
+{
+ public interface IDatabaseBackupService
+ {
+ Task BackupAsync(CancellationToken cancellationToken = default);
+ }
+}
diff --git a/BackEnd/Timeline/Services/DatabaseManagement/IDatabaseCustomMigration.cs b/BackEnd/Timeline/Services/DatabaseManagement/IDatabaseCustomMigration.cs
index 7300ccbd..a38dcb99 100644
--- a/BackEnd/Timeline/Services/DatabaseManagement/IDatabaseCustomMigration.cs
+++ b/BackEnd/Timeline/Services/DatabaseManagement/IDatabaseCustomMigration.cs
@@ -7,6 +7,15 @@ namespace Timeline.Services.DatabaseManagement
public interface IDatabaseCustomMigration
{
string GetName();
+
+ /// <summary>
+ /// Execute the migration on database.
+ /// </summary>
+ /// <param name="database">The database.</param>
+ /// <param name="cancellationToken">Cancellation token.</param>
+ /// <remarks>
+ /// Do not create transaction since the migrator will take care of transaction.
+ /// </remarks>
Task ExecuteAsync(DatabaseContext database, CancellationToken cancellationToken = default);
}
}
diff --git a/BackEnd/Timeline/Services/DatabaseManagement/IDatabaseCustomMigrator.cs b/BackEnd/Timeline/Services/DatabaseManagement/IDatabaseCustomMigrator.cs
new file mode 100644
index 00000000..5eb43b92
--- /dev/null
+++ b/BackEnd/Timeline/Services/DatabaseManagement/IDatabaseCustomMigrator.cs
@@ -0,0 +1,10 @@
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Timeline.Services.DatabaseManagement
+{
+ public interface IDatabaseCustomMigrator
+ {
+ Task MigrateAsync(CancellationToken cancellationToken = default);
+ }
+}
diff --git a/BackEnd/Timeline/Services/DatabaseManagement/MigationServiceCollectionExtensions.cs b/BackEnd/Timeline/Services/DatabaseManagement/MigationServiceCollectionExtensions.cs
index d1f9b51c..8269ce28 100644
--- a/BackEnd/Timeline/Services/DatabaseManagement/MigationServiceCollectionExtensions.cs
+++ b/BackEnd/Timeline/Services/DatabaseManagement/MigationServiceCollectionExtensions.cs
@@ -1,14 +1,17 @@
using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Timeline.Services.DatabaseManagement
{
- public static class DatabaseManagementServiceCollectionExtensions
+ public static class DatabaseManagementServicesServiceCollectionExtensions
{
public static IServiceCollection AddDatabaseManagementService(this IServiceCollection services)
{
- services.AddScoped<IDatabaseCustomMigrator, DatabaseCustomMigrator>();
+ services.TryAddScoped<IDatabaseCustomMigrator, DatabaseCustomMigrator>();
services.AddScoped<IDatabaseCustomMigration, TimelinePostContentToDataMigration>();
- services.AddScoped<IDatabaseBackupService, DatabaseBackupService>();
+
+ services.TryAddScoped<IDatabaseBackupService, DatabaseBackupService>();
+
services.AddHostedService<DatabaseManagementService>();
return services;
}
diff --git a/BackEnd/Timeline/Services/DatabaseManagement/Resource.Designer.cs b/BackEnd/Timeline/Services/DatabaseManagement/Resource.Designer.cs
new file mode 100644
index 00000000..c0e61a3f
--- /dev/null
+++ b/BackEnd/Timeline/Services/DatabaseManagement/Resource.Designer.cs
@@ -0,0 +1,108 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+// This code was generated by a tool.
+// Runtime Version:4.0.30319.42000
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace Timeline.Services.DatabaseManagement {
+ using System;
+
+
+ /// <summary>
+ /// A strongly-typed resource class, for looking up localized strings, etc.
+ /// </summary>
+ // This class was auto-generated by the StronglyTypedResourceBuilder
+ // class via a tool like ResGen or Visual Studio.
+ // To add or remove a member, edit your .ResX file then rerun ResGen
+ // with the /str option, or rebuild your VS project.
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+ internal class Resource {
+
+ private static global::System.Resources.ResourceManager resourceMan;
+
+ private static global::System.Globalization.CultureInfo resourceCulture;
+
+ [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
+ internal Resource() {
+ }
+
+ /// <summary>
+ /// Returns the cached ResourceManager instance used by this class.
+ /// </summary>
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Resources.ResourceManager ResourceManager {
+ get {
+ if (object.ReferenceEquals(resourceMan, null)) {
+ global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Services.DatabaseManagement.Resource", typeof(Resource).Assembly);
+ resourceMan = temp;
+ }
+ return resourceMan;
+ }
+ }
+
+ /// <summary>
+ /// Overrides the current thread's CurrentUICulture property for all
+ /// resource lookups using this strongly typed resource class.
+ /// </summary>
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Globalization.CultureInfo Culture {
+ get {
+ return resourceCulture;
+ }
+ set {
+ resourceCulture = value;
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Database backup finished with output file at {0}..
+ /// </summary>
+ internal static string DatabaseBackupServiceFinishBackup {
+ get {
+ return ResourceManager.GetString("DatabaseBackupServiceFinishBackup", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Begin custom migration &apos;{0}&apos;..
+ /// </summary>
+ internal static string DatabaseCustomMigratorBeginMigration {
+ get {
+ return ResourceManager.GetString("DatabaseCustomMigratorBeginMigration", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to End custom migration &apos;{0}&apos;..
+ /// </summary>
+ internal static string DatabaseCustomMigratorFinishMigration {
+ get {
+ return ResourceManager.GetString("DatabaseCustomMigratorFinishMigration", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Found custom migration &apos;{0}&apos;. Applied: {1}..
+ /// </summary>
+ internal static string DatabaseCustomMigratorFoundMigration {
+ get {
+ return ResourceManager.GetString("DatabaseCustomMigratorFoundMigration", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Old image content does not have corresponding data with the tag..
+ /// </summary>
+ internal static string TimelinePostContentToDataMigrationImageNoData {
+ get {
+ return ResourceManager.GetString("TimelinePostContentToDataMigrationImageNoData", resourceCulture);
+ }
+ }
+ }
+}
diff --git a/BackEnd/Timeline/Services/DatabaseManagement/Resource.resx b/BackEnd/Timeline/Services/DatabaseManagement/Resource.resx
new file mode 100644
index 00000000..9480ce64
--- /dev/null
+++ b/BackEnd/Timeline/Services/DatabaseManagement/Resource.resx
@@ -0,0 +1,135 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+ <!--
+ Microsoft ResX Schema
+
+ Version 2.0
+
+ The primary goals of this format is to allow a simple XML format
+ that is mostly human readable. The generation and parsing of the
+ various data types are done through the TypeConverter classes
+ associated with the data types.
+
+ Example:
+
+ ... ado.net/XML headers & schema ...
+ <resheader name="resmimetype">text/microsoft-resx</resheader>
+ <resheader name="version">2.0</resheader>
+ <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+ <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+ <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+ <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+ <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+ <value>[base64 mime encoded serialized .NET Framework object]</value>
+ </data>
+ <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+ <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+ <comment>This is a comment</comment>
+ </data>
+
+ There are any number of "resheader" rows that contain simple
+ name/value pairs.
+
+ Each data row contains a name, and value. The row also contains a
+ type or mimetype. Type corresponds to a .NET class that support
+ text/value conversion through the TypeConverter architecture.
+ Classes that don't support this are serialized and stored with the
+ mimetype set.
+
+ The mimetype is used for serialized objects, and tells the
+ ResXResourceReader how to depersist the object. This is currently not
+ extensible. For a given mimetype the value must be set accordingly:
+
+ Note - application/x-microsoft.net.object.binary.base64 is the format
+ that the ResXResourceWriter will generate, however the reader can
+ read any of the formats listed below.
+
+ mimetype: application/x-microsoft.net.object.binary.base64
+ value : The object must be serialized with
+ : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+ : and then encoded with base64 encoding.
+
+ mimetype: application/x-microsoft.net.object.soap.base64
+ value : The object must be serialized with
+ : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+ : and then encoded with base64 encoding.
+
+ mimetype: application/x-microsoft.net.object.bytearray.base64
+ value : The object must be serialized into a byte array
+ : using a System.ComponentModel.TypeConverter
+ : and then encoded with base64 encoding.
+ -->
+ <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+ <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+ <xsd:element name="root" msdata:IsDataSet="true">
+ <xsd:complexType>
+ <xsd:choice maxOccurs="unbounded">
+ <xsd:element name="metadata">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" />
+ </xsd:sequence>
+ <xsd:attribute name="name" use="required" type="xsd:string" />
+ <xsd:attribute name="type" type="xsd:string" />
+ <xsd:attribute name="mimetype" type="xsd:string" />
+ <xsd:attribute ref="xml:space" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="assembly">
+ <xsd:complexType>
+ <xsd:attribute name="alias" type="xsd:string" />
+ <xsd:attribute name="name" type="xsd:string" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="data">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+ <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+ <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+ <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+ <xsd:attribute ref="xml:space" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="resheader">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" use="required" />
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:choice>
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:schema>
+ <resheader name="resmimetype">
+ <value>text/microsoft-resx</value>
+ </resheader>
+ <resheader name="version">
+ <value>2.0</value>
+ </resheader>
+ <resheader name="reader">
+ <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </resheader>
+ <resheader name="writer">
+ <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </resheader>
+ <data name="DatabaseBackupServiceFinishBackup" xml:space="preserve">
+ <value>Database backup finished with output file at {0}.</value>
+ </data>
+ <data name="DatabaseCustomMigratorBeginMigration" xml:space="preserve">
+ <value>Begin custom migration '{0}'.</value>
+ </data>
+ <data name="DatabaseCustomMigratorFinishMigration" xml:space="preserve">
+ <value>End custom migration '{0}'.</value>
+ </data>
+ <data name="DatabaseCustomMigratorFoundMigration" xml:space="preserve">
+ <value>Found custom migration '{0}'. Applied: {1}.</value>
+ </data>
+ <data name="TimelinePostContentToDataMigrationImageNoData" xml:space="preserve">
+ <value>Old image content does not have corresponding data with the tag.</value>
+ </data>
+</root> \ No newline at end of file
diff --git a/BackEnd/Timeline/Services/DatabaseManagement/TimelinePostContentToDataMigration.cs b/BackEnd/Timeline/Services/DatabaseManagement/TimelinePostContentToDataMigration.cs
index f9a3418b..f6662a97 100644
--- a/BackEnd/Timeline/Services/DatabaseManagement/TimelinePostContentToDataMigration.cs
+++ b/BackEnd/Timeline/Services/DatabaseManagement/TimelinePostContentToDataMigration.cs
@@ -35,7 +35,7 @@ namespace Timeline.Services.DatabaseManagement
{
if (postEntity.ContentType == "text")
{
- var tag = await _dataManager.RetainEntry(Encoding.UTF8.GetBytes(postEntity.Content));
+ var tag = await _dataManager.RetainEntryAsync(Encoding.UTF8.GetBytes(postEntity.Content), cancellationToken);
database.TimelinePostData.Add(new TimelinePostDataEntity
{
DataTag = tag,
@@ -47,7 +47,7 @@ namespace Timeline.Services.DatabaseManagement
}
else
{
- var data = await _dataManager.GetEntryAndCheck(postEntity.Content, "Old image content does not have corresponding data with the tag.");
+ var data = await _dataManager.GetEntryAndCheck(postEntity.Content, Resource.TimelinePostContentToDataMigrationImageNoData, cancellationToken);
var format = Image.DetectFormat(data);
database.TimelinePostData.Add(new TimelinePostDataEntity
{