Switches the database from SQL Server to MariaDB

The target Pi only has MariaDB, and SQL Server has no ARM64 build at
all - not a config problem, a real gap discovered during deployment
setup. Swapped the EF Core provider, regenerated every migration,
updated connection strings and the backup script everywhere they
appear.

Took two tries to land on a provider that actually works: Pomelo
builds fine against this project's EF Core 10 packages but fails at
runtime (it's compiled against 9's internal API surface, which moved
in 10 wherever Identity/DataProtection force the newer packages).
Oracle's official provider builds and migrates fine but has a real
MariaDB bug in its own migration-lock code, reproduced against a live
database. Kept Oracle's provider and worked around just that one
broken method - everything else it does is correct - rather than
give up more of the stack to chase a workaround.

Verified against a real local MariaDB end to end: all three
migrations applied, both hosts start clean, full suite still green.
This commit is contained in:
2026-07-29 11:59:09 +02:00
parent 579e0ceaac
commit 33d18ebbf8
33 changed files with 575 additions and 1367 deletions
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using MySql.Data.MySqlClient;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Sentry;
@@ -108,7 +108,7 @@ public static class DatabaseMigrationExtensions
{
for (var current = exception; current is not null; current = current.InnerException)
{
if (current is SqlException or TimeoutException)
if (current is MySqlException or TimeoutException)
{
return true;
}
@@ -0,0 +1,99 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
namespace SlpModularCms.Core.Hosting;
/// <summary>
/// Works around a confirmed bug in Oracle's <c>MySql.EntityFrameworkCore</c> provider (10.0.7)
/// against MariaDB: its <c>AcquireDatabaseLock</c> issues MariaDB's <c>GET_LOCK()</c> and casts
/// the result straight to <see cref="long"/>, but MariaDB returns <c>NULL</c> in a case real MySQL
/// Server apparently doesn't — producing an unconditional <see cref="InvalidCastException"/> that
/// blocks every migration attempt, reproduced against a real MariaDB instance during Operations.
/// </summary>
/// <remarks>
/// Oracle's own <c>MySQLHistoryRepository</c> is an internal type, so it cannot be subclassed
/// directly to override just the lock methods. This instead constructs a real instance of it via
/// reflection (its constructor is public even though the class itself is not) and forwards every
/// <see cref="IHistoryRepository"/> member to that instance, except the two lock methods — which
/// never reach the broken call at all.
///
/// Skipping the lock is acceptable here because migrations only ever run from one place at a
/// time: <c>MigrateCoreDatabase()</c> at startup, driven by the atomic-release deploy sequence,
/// which never runs two deploys concurrently against the same environment. A genuinely concurrent
/// multi-instance migration race is not a scenario this deployment model produces.
/// </remarks>
public sealed class NonLockingMySQLHistoryRepository : IHistoryRepository
{
private const string InnerTypeName = "MySql.EntityFrameworkCore.Migrations.Internal.MySQLHistoryRepository";
private readonly IHistoryRepository _inner;
public NonLockingMySQLHistoryRepository(HistoryRepositoryDependencies dependencies)
{
var innerType = typeof(MySQLDbContextOptionsExtensions).Assembly.GetType(InnerTypeName)
?? throw new InvalidOperationException(
$"{InnerTypeName} was not found. MySql.EntityFrameworkCore may have changed its internal " +
"layout — this workaround needs re-verifying against the new version.");
_inner = (IHistoryRepository)Activator.CreateInstance(innerType, dependencies)!;
}
public bool Exists() => _inner.Exists();
public Task<bool> ExistsAsync(CancellationToken cancellationToken = default) =>
_inner.ExistsAsync(cancellationToken);
public void Create() => _inner.Create();
public Task CreateAsync(CancellationToken cancellationToken = default) =>
_inner.CreateAsync(cancellationToken);
public bool CreateIfNotExists() => _inner.CreateIfNotExists();
public Task<bool> CreateIfNotExistsAsync(CancellationToken cancellationToken = default) =>
_inner.CreateIfNotExistsAsync(cancellationToken);
public IReadOnlyList<HistoryRow> GetAppliedMigrations() => _inner.GetAppliedMigrations();
public Task<IReadOnlyList<HistoryRow>> GetAppliedMigrationsAsync(CancellationToken cancellationToken = default) =>
_inner.GetAppliedMigrationsAsync(cancellationToken);
public LockReleaseBehavior LockReleaseBehavior => _inner.LockReleaseBehavior;
public string GetCreateScript() => _inner.GetCreateScript();
public string GetCreateIfNotExistsScript() => _inner.GetCreateIfNotExistsScript();
public string GetInsertScript(HistoryRow row) => _inner.GetInsertScript(row);
public string GetDeleteScript(string migrationId) => _inner.GetDeleteScript(migrationId);
public string GetBeginIfNotExistsScript(string migrationId) => _inner.GetBeginIfNotExistsScript(migrationId);
public string GetBeginIfExistsScript(string migrationId) => _inner.GetBeginIfExistsScript(migrationId);
public string GetEndIfScript() => _inner.GetEndIfScript();
// The actual workaround: never call into the inner repository's broken GET_LOCK path.
public IMigrationsDatabaseLock AcquireDatabaseLock() => new NoOpMigrationsDatabaseLock(this);
public Task<IMigrationsDatabaseLock> AcquireDatabaseLockAsync(CancellationToken cancellationToken = default) =>
Task.FromResult<IMigrationsDatabaseLock>(new NoOpMigrationsDatabaseLock(this));
private sealed class NoOpMigrationsDatabaseLock(IHistoryRepository historyRepository) : IMigrationsDatabaseLock
{
public IHistoryRepository HistoryRepository { get; } = historyRepository;
public void Dispose()
{
}
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
public IMigrationsDatabaseLock ReacquireIfNeeded(bool connectionOpened, bool? recreateIfInvalid) => this;
public Task<IMigrationsDatabaseLock> ReacquireIfNeededAsync(
bool connectionOpened, bool? recreateIfInvalid, CancellationToken cancellationToken = default) =>
Task.FromResult<IMigrationsDatabaseLock>(this);
}
}
@@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@@ -33,7 +34,10 @@ public static class ServiceCollectionExtensions
{
// 1. Database
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
{
options.UseMySQL(configuration.GetConnectionString("DefaultConnection")!);
options.ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>();
});
// 2. Identity
services.AddIdentityCore<ApplicationUser>(options =>