refactor(db): migrate user rule settings

This commit is contained in:
cesnimda
2026-08-30 16:44:48 +02:00
parent e47ad1d243
commit c094800c69
9 changed files with 107 additions and 42 deletions
@@ -0,0 +1,54 @@
using System;
using JobTrackerApi.Data;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace JobTrackerApi.Migrations;
/// <summary>
/// Transfers creation ownership of per-user rule settings from startup reconciliation to the
/// migration chain without replacing tables created by older deployments.
/// </summary>
[DbContext(typeof(JobTrackerContext))]
[Migration("20260830121000_AdoptUserRuleSettingsSchema")]
public sealed class AdoptUserRuleSettingsSchema : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
{
migrationBuilder.Sql("""
CREATE TABLE IF NOT EXISTS `UserRuleSettings` (
`OwnerUserId` varchar(255) NOT NULL,
`AppliedFollowUpDays` int NOT NULL,
`AppliedGhostDays` int NOT NULL,
`OfferFollowUpDays` int NOT NULL,
`OfferGhostDays` int NOT NULL,
`FeedbackFollowUpDays` int NOT NULL,
`FeedbackGhostDays` int NOT NULL,
PRIMARY KEY (`OwnerUserId`)
) CHARACTER SET=utf8mb4;
""");
return;
}
migrationBuilder.Sql("""
CREATE TABLE IF NOT EXISTS "UserRuleSettings" (
"OwnerUserId" TEXT NOT NULL CONSTRAINT "PK_UserRuleSettings" PRIMARY KEY,
"AppliedFollowUpDays" INTEGER NOT NULL,
"AppliedGhostDays" INTEGER NOT NULL,
"OfferFollowUpDays" INTEGER NOT NULL,
"OfferGhostDays" INTEGER NOT NULL,
"FeedbackFollowUpDays" INTEGER NOT NULL,
"FeedbackGhostDays" INTEGER NOT NULL
);
""");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
// Preserve settings that may have been created before migration ownership was introduced.
}
}
@@ -536,25 +536,6 @@ public static class StartupInitializationExtensions
// UiLanguage is migration-owned (AddUiLanguagePreference). Adding it here before
// the per-migration loop makes a fresh database fail when that migration runs.
static void EnsureUserRuleSettingsTable(DbConnection c)
{
if (HasTable(c, "UserRuleSettings")) return;
Exec(c, """
CREATE TABLE IF NOT EXISTS "UserRuleSettings" (
"OwnerUserId" TEXT NOT NULL CONSTRAINT "PK_UserRuleSettings" PRIMARY KEY,
"AppliedFollowUpDays" INTEGER NOT NULL,
"AppliedGhostDays" INTEGER NOT NULL,
"OfferFollowUpDays" INTEGER NOT NULL,
"OfferGhostDays" INTEGER NOT NULL,
"FeedbackFollowUpDays" INTEGER NOT NULL,
"FeedbackGhostDays" INTEGER NOT NULL
);
""");
}
EnsureUserRuleSettingsTable(conn);
static void EnsureGmailConnectionsTable(DbConnection c)
{
Exec(c, """
@@ -1453,22 +1434,6 @@ public static class StartupInitializationExtensions
seedRuleSettings.ExecuteNonQuery();
}
if (!HasMySqlTable(conn, "UserRuleSettings"))
{
using var cmd = conn.CreateCommand();
cmd.CommandText = @"CREATE TABLE IF NOT EXISTS `UserRuleSettings` (
`OwnerUserId` varchar(255) NOT NULL,
`AppliedFollowUpDays` int NOT NULL,
`AppliedGhostDays` int NOT NULL,
`OfferFollowUpDays` int NOT NULL,
`OfferGhostDays` int NOT NULL,
`FeedbackFollowUpDays` int NOT NULL,
`FeedbackGhostDays` int NOT NULL,
PRIMARY KEY (`OwnerUserId`)
);";
cmd.ExecuteNonQuery();
}
if (!HasMySqlTable(conn, "CvUploadArtifacts"))
{
using var cmd = conn.CreateCommand();
@@ -24,6 +24,7 @@ internal static class StartupSchemaOwnership
"SystemEmailSettings",
"UserNotifications",
"UserOperations",
"UserRuleSettings",
};
internal static readonly IReadOnlySet<string> ReconcilerOwnedTables = new HashSet<string>(StringComparer.Ordinal)
@@ -60,7 +61,6 @@ internal static class StartupSchemaOwnership
"TailoredCvDrafts",
"TrustedDevices",
"TwoFactorRecoveryCodes",
"UserRuleSettings",
"UserSessions",
};