From 4047df8610bd79c8056b9a92717bc2e3f677f155 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 27 Nov 2024 15:22:29 +0100 Subject: [PATCH] fix: always get the latest migration even when two are applied at the same time without also ordering by migration_name, two migrations being applied at the same time *can* (but won't always) result in the *earlier* migration being returned as the latest one. as our migrations are not idempotent, this causes the bot to crash on startup (and even if they *were* idempotent, the code that inserts the new migration name into the migrations table isn't) --- Catalogger.Backend/Database/DatabaseMigrator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Catalogger.Backend/Database/DatabaseMigrator.cs b/Catalogger.Backend/Database/DatabaseMigrator.cs index 80e68c6..183723d 100644 --- a/Catalogger.Backend/Database/DatabaseMigrator.cs +++ b/Catalogger.Backend/Database/DatabaseMigrator.cs @@ -141,7 +141,7 @@ public class DatabaseMigrator(ILogger logger, IClock clock, DatabaseConnection c if (hasMigrationTable) { return await conn.QuerySingleOrDefaultAsync( - "SELECT * FROM migrations ORDER BY applied_at DESC LIMIT 1" + "SELECT * FROM migrations ORDER BY applied_at DESC, migration_name DESC LIMIT 1" ); } @@ -163,7 +163,7 @@ public class DatabaseMigrator(ILogger logger, IClock clock, DatabaseConnection c return await reader.ReadToEndAsync(); } - public static IEnumerable GetMigrationNames() => + private static IEnumerable GetMigrationNames() => typeof(DatabasePool) .Assembly.GetManifestResourceNames() .Where(s => s.StartsWith($"{RootPath}.Migrations"))