mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 02:11:10 +00:00
Implements tests for commit eaca69bf2a
This commit is contained in:
1 parent
94a67aded4
commit
d185c77999
15 files changed
+527
-128
No files matched your search
-55
@@ -1,55 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using System.Data.Common;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.Common.Interceptors;
|
||||
|
||||
/// <summary>
|
||||
/// Registers the SQL function used by the shared reference normalization migration, so that names are folded with the same culture-invariant rule as the domain value objects.
|
||||
/// </summary>
|
||||
public sealed class SqliteSharedReferenceNormalizationInterceptor : DbConnectionInterceptor
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the SQL function that trims a name and lowercases it with the invariant culture.
|
||||
/// </summary>
|
||||
public const string NORMALIZE_FUNCTION_NAME = "lumina_normalize_shared_reference_name";
|
||||
|
||||
/// <summary>
|
||||
/// Intercepts a synchronously opened connection to register the normalization function.
|
||||
/// </summary>
|
||||
/// <param name="connection">The connection that was opened.</param>
|
||||
/// <param name="eventData">The event data of the opened connection.</param>
|
||||
public override void ConnectionOpened(DbConnection connection, ConnectionEndEventData eventData)
|
||||
{
|
||||
RegisterNormalizationFunction(connection);
|
||||
base.ConnectionOpened(connection, eventData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Intercepts an asynchronously opened connection to register the normalization function.
|
||||
/// </summary>
|
||||
/// <param name="connection">The connection that was opened.</param>
|
||||
/// <param name="eventData">The event data of the opened connection.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
public override Task ConnectionOpenedAsync(DbConnection connection, ConnectionEndEventData eventData, CancellationToken cancellationToken = default)
|
||||
{
|
||||
RegisterNormalizationFunction(connection);
|
||||
return base.ConnectionOpenedAsync(connection, eventData, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers the normalization function on <paramref name="connection"/> when it is a SQLite connection.
|
||||
/// </summary>
|
||||
/// <param name="connection">The connection that was opened.</param>
|
||||
private static void RegisterNormalizationFunction(DbConnection connection)
|
||||
{
|
||||
if (connection is not SqliteConnection sqliteConnection)
|
||||
return;
|
||||
sqliteConnection.CreateFunction(NORMALIZE_FUNCTION_NAME, (string? name) => name?.Trim().ToLowerInvariant(), isDeterministic: true);
|
||||
}
|
||||
}
|
||||
+24
-24
@@ -12,46 +12,46 @@ namespace Lumina.DataAccess.Common.Migrations
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
// The value objects now store names trimmed and lowercased with the invariant culture, so the existing rows must be normalized before new
|
||||
// scans start reusing them by their normalized name. The SQL function is registered on every opened connection by
|
||||
// SqliteSharedReferenceNormalizationInterceptor, so non-ASCII uppercase letters are folded exactly like the value objects fold them.
|
||||
// The lowercase rows are inserted first, so that the join rows can always be repointed.
|
||||
// The value objects now store names trimmed and lowercased, so the existing rows must be normalized before new scans start reusing them
|
||||
// by their normalized name. SQLite's built-in lower() folds ASCII letters only, so a legacy name whose uppercase letters are outside
|
||||
// ASCII is not fully folded and is not merged with its lowercase counterpart. The lowercase rows are inserted first, so that the join
|
||||
// rows can always be repointed.
|
||||
migrationBuilder.Sql("""
|
||||
INSERT OR IGNORE INTO Tags (Name)
|
||||
SELECT DISTINCT lumina_normalize_shared_reference_name(Name) FROM Tags;
|
||||
SELECT DISTINCT trim(lower(Name)) FROM Tags;
|
||||
|
||||
INSERT OR IGNORE INTO Genres (Name)
|
||||
SELECT DISTINCT lumina_normalize_shared_reference_name(Name) FROM Genres;
|
||||
SELECT DISTINCT trim(lower(Name)) FROM Genres;
|
||||
""");
|
||||
|
||||
// A repointed join row that would collide with an existing lowercase join row is left in its old form by OR IGNORE, and is removed below.
|
||||
migrationBuilder.Sql("""
|
||||
UPDATE OR IGNORE TrackTags SET TagId = lumina_normalize_shared_reference_name(TagId) WHERE TagId <> lumina_normalize_shared_reference_name(TagId);
|
||||
UPDATE OR IGNORE AlbumTags SET TagId = lumina_normalize_shared_reference_name(TagId) WHERE TagId <> lumina_normalize_shared_reference_name(TagId);
|
||||
UPDATE OR IGNORE ArtistTags SET TagId = lumina_normalize_shared_reference_name(TagId) WHERE TagId <> lumina_normalize_shared_reference_name(TagId);
|
||||
UPDATE OR IGNORE BookTags SET TagId = lumina_normalize_shared_reference_name(TagId) WHERE TagId <> lumina_normalize_shared_reference_name(TagId);
|
||||
UPDATE OR IGNORE TrackTags SET TagId = trim(lower(TagId)) WHERE TagId <> trim(lower(TagId));
|
||||
UPDATE OR IGNORE AlbumTags SET TagId = trim(lower(TagId)) WHERE TagId <> trim(lower(TagId));
|
||||
UPDATE OR IGNORE ArtistTags SET TagId = trim(lower(TagId)) WHERE TagId <> trim(lower(TagId));
|
||||
UPDATE OR IGNORE BookTags SET TagId = trim(lower(TagId)) WHERE TagId <> trim(lower(TagId));
|
||||
|
||||
UPDATE OR IGNORE TrackGenres SET GenreId = lumina_normalize_shared_reference_name(GenreId) WHERE GenreId <> lumina_normalize_shared_reference_name(GenreId);
|
||||
UPDATE OR IGNORE AlbumGenres SET GenreId = lumina_normalize_shared_reference_name(GenreId) WHERE GenreId <> lumina_normalize_shared_reference_name(GenreId);
|
||||
UPDATE OR IGNORE ArtistGenres SET GenreId = lumina_normalize_shared_reference_name(GenreId) WHERE GenreId <> lumina_normalize_shared_reference_name(GenreId);
|
||||
UPDATE OR IGNORE BookGenres SET GenreId = lumina_normalize_shared_reference_name(GenreId) WHERE GenreId <> lumina_normalize_shared_reference_name(GenreId);
|
||||
UPDATE OR IGNORE TrackGenres SET GenreId = trim(lower(GenreId)) WHERE GenreId <> trim(lower(GenreId));
|
||||
UPDATE OR IGNORE AlbumGenres SET GenreId = trim(lower(GenreId)) WHERE GenreId <> trim(lower(GenreId));
|
||||
UPDATE OR IGNORE ArtistGenres SET GenreId = trim(lower(GenreId)) WHERE GenreId <> trim(lower(GenreId));
|
||||
UPDATE OR IGNORE BookGenres SET GenreId = trim(lower(GenreId)) WHERE GenreId <> trim(lower(GenreId));
|
||||
""");
|
||||
|
||||
// The rows still carrying a mixed-case or untrimmed name are the duplicates that could not be repointed, because the lowercase join row
|
||||
// was already present. They are removed before the old tag and genre rows, so that no foreign key is left pointing at a deleted row.
|
||||
migrationBuilder.Sql("""
|
||||
DELETE FROM TrackTags WHERE TagId <> lumina_normalize_shared_reference_name(TagId);
|
||||
DELETE FROM AlbumTags WHERE TagId <> lumina_normalize_shared_reference_name(TagId);
|
||||
DELETE FROM ArtistTags WHERE TagId <> lumina_normalize_shared_reference_name(TagId);
|
||||
DELETE FROM BookTags WHERE TagId <> lumina_normalize_shared_reference_name(TagId);
|
||||
DELETE FROM TrackTags WHERE TagId <> trim(lower(TagId));
|
||||
DELETE FROM AlbumTags WHERE TagId <> trim(lower(TagId));
|
||||
DELETE FROM ArtistTags WHERE TagId <> trim(lower(TagId));
|
||||
DELETE FROM BookTags WHERE TagId <> trim(lower(TagId));
|
||||
|
||||
DELETE FROM TrackGenres WHERE GenreId <> lumina_normalize_shared_reference_name(GenreId);
|
||||
DELETE FROM AlbumGenres WHERE GenreId <> lumina_normalize_shared_reference_name(GenreId);
|
||||
DELETE FROM ArtistGenres WHERE GenreId <> lumina_normalize_shared_reference_name(GenreId);
|
||||
DELETE FROM BookGenres WHERE GenreId <> lumina_normalize_shared_reference_name(GenreId);
|
||||
DELETE FROM TrackGenres WHERE GenreId <> trim(lower(GenreId));
|
||||
DELETE FROM AlbumGenres WHERE GenreId <> trim(lower(GenreId));
|
||||
DELETE FROM ArtistGenres WHERE GenreId <> trim(lower(GenreId));
|
||||
DELETE FROM BookGenres WHERE GenreId <> trim(lower(GenreId));
|
||||
|
||||
DELETE FROM Tags WHERE Name <> lumina_normalize_shared_reference_name(Name);
|
||||
DELETE FROM Genres WHERE Name <> lumina_normalize_shared_reference_name(Name);
|
||||
DELETE FROM Tags WHERE Name <> trim(lower(Name));
|
||||
DELETE FROM Genres WHERE Name <> trim(lower(Name));
|
||||
""");
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@ using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.DataAccess.Common.Interceptors;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
#endregion
|
||||
|
||||
@@ -67,16 +66,6 @@ public class LuminaDbContext : DbContext
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the database context options, including the interceptors that apply to every connection.
|
||||
/// </summary>
|
||||
/// <param name="optionsBuilder">The builder used to configure the database context.</param>
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
optionsBuilder.AddInterceptors(new SqliteSharedReferenceNormalizationInterceptor());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the entity mappings for the database context.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user