refactor(db): migrate auth support tables
Move recovery codes, trusted devices, and revocable sessions into an additive provider-aware migration. Preserve existing security records and retain guarded MariaDB repairs for historical schemas.
This commit is contained in:
@@ -687,56 +687,6 @@ public static class StartupInitializationExtensions
|
||||
Exec(c, """CREATE INDEX IF NOT EXISTS "IX_TailoredCvDrafts_JobApplicationId" ON "TailoredCvDrafts" ("JobApplicationId");""");
|
||||
}
|
||||
|
||||
static void EnsureTwoFactorRecoveryCodesTable(DbConnection c)
|
||||
{
|
||||
Exec(c, """
|
||||
CREATE TABLE IF NOT EXISTS "TwoFactorRecoveryCodes" (
|
||||
"Id" INTEGER NOT NULL CONSTRAINT "PK_TwoFactorRecoveryCodes" PRIMARY KEY AUTOINCREMENT,
|
||||
"UserId" TEXT NOT NULL,
|
||||
"CodeHash" TEXT NOT NULL,
|
||||
"CreatedAtUtc" TEXT NOT NULL,
|
||||
"UsedAtUtc" TEXT NULL
|
||||
);
|
||||
""");
|
||||
|
||||
Exec(c, """CREATE INDEX IF NOT EXISTS "IX_TwoFactorRecoveryCodes_UserId_UsedAtUtc" ON "TwoFactorRecoveryCodes" ("UserId", "UsedAtUtc");""");
|
||||
}
|
||||
|
||||
static void EnsureTrustedDevicesTable(DbConnection c)
|
||||
{
|
||||
Exec(c, """
|
||||
CREATE TABLE IF NOT EXISTS "TrustedDevices" (
|
||||
"Id" INTEGER NOT NULL CONSTRAINT "PK_TrustedDevices" PRIMARY KEY AUTOINCREMENT,
|
||||
"UserId" TEXT NOT NULL,
|
||||
"TokenHash" TEXT NOT NULL,
|
||||
"DeviceLabel" TEXT NULL,
|
||||
"CreatedAtUtc" TEXT NOT NULL,
|
||||
"LastSeenAtUtc" TEXT NOT NULL,
|
||||
"ExpiresAtUtc" TEXT NOT NULL
|
||||
);
|
||||
""");
|
||||
|
||||
Exec(c, """CREATE INDEX IF NOT EXISTS "IX_TrustedDevices_UserId" ON "TrustedDevices" ("UserId");""");
|
||||
Exec(c, """CREATE INDEX IF NOT EXISTS "IX_TrustedDevices_TokenHash" ON "TrustedDevices" ("TokenHash");""");
|
||||
}
|
||||
|
||||
static void EnsureUserSessionsTable(DbConnection c)
|
||||
{
|
||||
Exec(c, """
|
||||
CREATE TABLE IF NOT EXISTS "UserSessions" (
|
||||
"Id" TEXT NOT NULL CONSTRAINT "PK_UserSessions" PRIMARY KEY,
|
||||
"UserId" TEXT NOT NULL,
|
||||
"DeviceLabel" TEXT NULL,
|
||||
"CreatedAtUtc" TEXT NOT NULL,
|
||||
"LastSeenAtUtc" TEXT NOT NULL,
|
||||
"ExpiresAtUtc" TEXT NOT NULL,
|
||||
"RevokedAtUtc" TEXT NULL
|
||||
);
|
||||
""");
|
||||
|
||||
Exec(c, """CREATE INDEX IF NOT EXISTS "IX_UserSessions_UserId" ON "UserSessions" ("UserId");""");
|
||||
}
|
||||
|
||||
// Career Workspace foundation (docs/career-workspace-implementation-roadmap.md
|
||||
// Phase F1). Additive tables: ApplicationUser.ProfileCvStructureJson remains the
|
||||
// authoritative column every existing read path uses; these mirror it so future
|
||||
@@ -1078,9 +1028,6 @@ public static class StartupInitializationExtensions
|
||||
EnsureMicrosoftGraphConnectionsTable(conn);
|
||||
EnsureImapConnectionsTable(conn);
|
||||
EnsureCvTables(conn);
|
||||
EnsureTwoFactorRecoveryCodesTable(conn);
|
||||
EnsureTrustedDevicesTable(conn);
|
||||
EnsureUserSessionsTable(conn);
|
||||
EnsureCareerProfileTables(conn);
|
||||
EnsureInterviewPrepNotesTable(conn);
|
||||
EnsureAiWorkspaceNotesTable(conn);
|
||||
@@ -1820,62 +1767,16 @@ public static class StartupInitializationExtensions
|
||||
|
||||
EnsureMySqlIndex(conn, "CareerProfileVersions", "IX_CareerProfileVersions_OwnerUserId_CareerProfileId_Version", "`OwnerUserId`, `CareerProfileId`, `Version`");
|
||||
|
||||
if (!HasMySqlTable(conn, "TwoFactorRecoveryCodes"))
|
||||
{
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = @"CREATE TABLE IF NOT EXISTS `TwoFactorRecoveryCodes` (
|
||||
`Id` int NOT NULL AUTO_INCREMENT,
|
||||
`UserId` varchar(255) NOT NULL,
|
||||
`CodeHash` varchar(255) NOT NULL,
|
||||
`CreatedAtUtc` datetime(6) NOT NULL,
|
||||
`UsedAtUtc` datetime(6) NULL,
|
||||
PRIMARY KEY (`Id`)
|
||||
);";
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
EnsureMySqlAutoIncrementPrimaryKey(conn, "TwoFactorRecoveryCodes", "Id");
|
||||
|
||||
EnsureMySqlIndex(conn, "TwoFactorRecoveryCodes", "IX_TwoFactorRecoveryCodes_UserId_UsedAtUtc", "`UserId`, `UsedAtUtc`");
|
||||
|
||||
if (!HasMySqlTable(conn, "TrustedDevices"))
|
||||
{
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = @"CREATE TABLE IF NOT EXISTS `TrustedDevices` (
|
||||
`Id` int NOT NULL AUTO_INCREMENT,
|
||||
`UserId` varchar(255) NOT NULL,
|
||||
`TokenHash` varchar(255) NOT NULL,
|
||||
`DeviceLabel` varchar(255) NULL,
|
||||
`CreatedAtUtc` datetime(6) NOT NULL,
|
||||
`LastSeenAtUtc` datetime(6) NOT NULL,
|
||||
`ExpiresAtUtc` datetime(6) NOT NULL,
|
||||
PRIMARY KEY (`Id`)
|
||||
);";
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
EnsureMySqlAutoIncrementPrimaryKey(conn, "TrustedDevices", "Id");
|
||||
|
||||
EnsureMySqlIndex(conn, "TrustedDevices", "IX_TrustedDevices_UserId", "`UserId`");
|
||||
|
||||
EnsureMySqlIndex(conn, "TrustedDevices", "IX_TrustedDevices_TokenHash", "`TokenHash`");
|
||||
|
||||
if (!HasMySqlTable(conn, "UserSessions"))
|
||||
{
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = @"CREATE TABLE IF NOT EXISTS `UserSessions` (
|
||||
`Id` varchar(64) NOT NULL,
|
||||
`UserId` varchar(255) NOT NULL,
|
||||
`DeviceLabel` varchar(255) NULL,
|
||||
`CreatedAtUtc` datetime(6) NOT NULL,
|
||||
`LastSeenAtUtc` datetime(6) NOT NULL,
|
||||
`ExpiresAtUtc` datetime(6) NOT NULL,
|
||||
`RevokedAtUtc` datetime(6) NULL,
|
||||
PRIMARY KEY (`Id`)
|
||||
);";
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
EnsureMySqlIndex(conn, "UserSessions", "IX_UserSessions_UserId", "`UserId`");
|
||||
|
||||
// Schema reconciliation must never crash app startup: an index that fails
|
||||
|
||||
@@ -23,9 +23,12 @@ internal static class StartupSchemaOwnership
|
||||
"Jobs",
|
||||
"RuleSettings",
|
||||
"SystemEmailSettings",
|
||||
"TrustedDevices",
|
||||
"TwoFactorRecoveryCodes",
|
||||
"UserNotifications",
|
||||
"UserOperations",
|
||||
"UserRuleSettings",
|
||||
"UserSessions",
|
||||
};
|
||||
|
||||
internal static readonly IReadOnlySet<string> ReconcilerOwnedTables = new HashSet<string>(StringComparer.Ordinal)
|
||||
@@ -59,9 +62,6 @@ internal static class StartupSchemaOwnership
|
||||
"InterviewPrepNotes",
|
||||
"MicrosoftGraphConnections",
|
||||
"TailoredCvDrafts",
|
||||
"TrustedDevices",
|
||||
"TwoFactorRecoveryCodes",
|
||||
"UserSessions",
|
||||
};
|
||||
|
||||
// Historical migrations contain guarded compatibility bootstraps for these tables so direct
|
||||
|
||||
Reference in New Issue
Block a user