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)
This commit is contained in:
sam 2024-11-27 15:22:29 +01:00
parent 27e77eeaed
commit 4047df8610
Signed by: sam
GPG key ID: 5F3C3C1B3166639D

View file

@ -141,7 +141,7 @@ public class DatabaseMigrator(ILogger logger, IClock clock, DatabaseConnection c
if (hasMigrationTable)
{
return await conn.QuerySingleOrDefaultAsync<MigrationEntry>(
"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<string> GetMigrationNames() =>
private static IEnumerable<string> GetMigrationNames() =>
typeof(DatabasePool)
.Assembly.GetManifestResourceNames()
.Where(s => s.StartsWith($"{RootPath}.Migrations"))