From 1aa2476779e42e034f28748978f2155794e34ad3 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Mon, 23 Feb 2026 21:08:46 +0100 Subject: [PATCH] Fixed migrations breaking because of IIS, added MySQL transaction method --- src/Server/Helper/SQLHelper.cs | 47 +++++++++++++++++++++ src/Server/Migrations/DatabaseMigrations.cs | 30 +++++++++---- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/src/Server/Helper/SQLHelper.cs b/src/Server/Helper/SQLHelper.cs index 0972b06..92c0f6a 100644 --- a/src/Server/Helper/SQLHelper.cs +++ b/src/Server/Helper/SQLHelper.cs @@ -226,6 +226,53 @@ public class SQLHelper:IDisposable Thread.Sleep(sleepTime); } } + public async Task ExecuteInTransactionAsync(Func operation) + { + var poolElement = await GetMySqlConnectionPoolElement(); + var connection = poolElement.Connection; + try + { + using var transaction = connection.BeginTransaction(); + try + { + await operation(connection, transaction); + await transaction.CommitAsync(); + } + catch + { + await transaction.RollbackAsync(); + throw; + } + } + finally + { + poolElement.Semaphore.Release(); + } + } + + public void ExecuteInTransaction(Action operation) + { + var poolElement = GetMySqlConnectionPoolElement().Result; + var connection = poolElement.Connection; + try + { + using var transaction = connection.BeginTransaction(); + try + { + operation(connection, transaction); + transaction.Commit(); + } + catch + { + transaction.Rollback(); + throw; + } + } + finally + { + poolElement.Semaphore.Release(); + } + } } public struct MySqlConnectionPoolElement diff --git a/src/Server/Migrations/DatabaseMigrations.cs b/src/Server/Migrations/DatabaseMigrations.cs index d757a75..fbe4a77 100644 --- a/src/Server/Migrations/DatabaseMigrations.cs +++ b/src/Server/Migrations/DatabaseMigrations.cs @@ -29,13 +29,9 @@ public static class DatabaseMigrations if (version >= databaseVersion) { databaseVersion = (int)method.Invoke(null, new object[] { helper }); + var _ = helper.ExecuteSQLNonQuery("UPDATE settings SET value = @databaseVersion", new() { ["databaseVersion"] = databaseVersion.ToString() }).Result; } } - - if (databaseVersion != initialDatabaseVersion) - { - var _ = helper.ExecuteSQLNonQuery("UPDATE settings SET value = @databaseVersion", new() { ["databaseVersion"] = databaseVersion.ToString() }).Result; - } } public static int DatabaseGetVersion(SQLHelper helper) @@ -122,25 +118,41 @@ public static class DatabaseMigrations { // Add id_entity to embedding var _ = helper.ExecuteSQLNonQuery("ALTER TABLE embedding ADD COLUMN id_entity INT NULL", []).Result; + return 6; + } + public static int UpdateFrom6(SQLHelper helper) + { int count; do { count = helper.ExecuteSQLNonQuery("UPDATE embedding e JOIN datapoint d ON d.id = e.id_datapoint JOIN (SELECT id FROM embedding WHERE id_entity IS NULL LIMIT 10000) x on x.id = e.id SET e.id_entity = d.id_entity;", []).Result; } while (count == 10000); - + return 7; + } + public static int UpdateFrom7(SQLHelper helper) + { _ = helper.ExecuteSQLNonQuery("ALTER TABLE embedding MODIFY id_entity INT NOT NULL;", []).Result; _ = helper.ExecuteSQLNonQuery("CREATE INDEX idx_embedding_entity_model ON embedding (id_entity, model)", []).Result; // Add id_searchdomain to embedding _ = helper.ExecuteSQLNonQuery("ALTER TABLE embedding ADD COLUMN id_searchdomain INT NULL", []).Result; + return 8; + } + + public static int UpdateFrom8(SQLHelper helper) + { + int count = 0; do { count = helper.ExecuteSQLNonQuery("UPDATE embedding e JOIN entity en ON en.id = e.id_entity JOIN (SELECT id FROM embedding WHERE id_searchdomain IS NULL LIMIT 10000) x on x.id = e.id SET e.id_searchdomain = en.id_searchdomain;", []).Result; } while (count == 10000); - + return 9; + } + + public static int UpdateFrom9(SQLHelper helper) + { _ = helper.ExecuteSQLNonQuery("ALTER TABLE embedding MODIFY id_searchdomain INT NOT NULL;", []).Result; _ = helper.ExecuteSQLNonQuery("CREATE INDEX idx_embedding_searchdomain_model ON embedding (id_searchdomain)", []).Result; - - return 6; + return 10; } } \ No newline at end of file