4 Commits

Author SHA1 Message Date
LD50
1b88bd1960 Merge pull request #133 from LD-Reborn/132-migrations-break-database-on-failure-due-to-lack-of-transactions
Fixed migrations breaking because of IIS, added MySQL transaction method
2026-02-23 21:13:20 +01:00
1aa2476779 Fixed migrations breaking because of IIS, added MySQL transaction method 2026-02-23 21:08:46 +01:00
LD50
7ed144bc39 Merge pull request #131 from LD-Reborn/20260223_mysqlfux
Fixed MySQL migration error
2026-02-23 07:41:33 +01:00
3b42a73b73 Fixed MySQL migration error 2026-02-23 07:41:03 +01:00
2 changed files with 69 additions and 10 deletions

View File

@@ -226,6 +226,53 @@ public class SQLHelper:IDisposable
Thread.Sleep(sleepTime); Thread.Sleep(sleepTime);
} }
} }
public async Task ExecuteInTransactionAsync(Func<MySqlConnection, DbTransaction, Task> 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<MySqlConnection, MySqlTransaction> 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 public struct MySqlConnectionPoolElement

View File

@@ -29,13 +29,9 @@ public static class DatabaseMigrations
if (version >= databaseVersion) if (version >= databaseVersion)
{ {
databaseVersion = (int)method.Invoke(null, new object[] { helper }); 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) public static int DatabaseGetVersion(SQLHelper helper)
@@ -122,25 +118,41 @@ public static class DatabaseMigrations
{ {
// Add id_entity to embedding // Add id_entity to embedding
var _ = helper.ExecuteSQLNonQuery("ALTER TABLE embedding ADD COLUMN id_entity INT NULL", []).Result; var _ = helper.ExecuteSQLNonQuery("ALTER TABLE embedding ADD COLUMN id_entity INT NULL", []).Result;
return 6;
}
public static int UpdateFrom6(SQLHelper helper)
{
int count; int count;
do 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; 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); } 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("ALTER TABLE embedding MODIFY id_entity INT NOT NULL;", []).Result;
_ = helper.ExecuteSQLNonQuery("CREATE INDEX idx_embedding_entity_model ON embedding (id_entity, model)", []).Result; _ = helper.ExecuteSQLNonQuery("CREATE INDEX idx_embedding_entity_model ON embedding (id_entity, model)", []).Result;
// Add id_searchdomain to embedding // Add id_searchdomain to embedding
_ = helper.ExecuteSQLNonQuery("ALTER TABLE embedding ADD COLUMN id_searchdomain INT NULL", []).Result; _ = helper.ExecuteSQLNonQuery("ALTER TABLE embedding ADD COLUMN id_searchdomain INT NULL", []).Result;
return 8;
}
public static int UpdateFrom8(SQLHelper helper)
{
int count = 0;
do do
{ {
count = helper.ExecuteSQLNonQuery("UPDATE embedding e JOIN entity en ON en.id = e.id_datapoint 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; 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); } 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("ALTER TABLE embedding MODIFY id_searchdomain INT NOT NULL;", []).Result;
_ = helper.ExecuteSQLNonQuery("CREATE INDEX idx_embedding_searchdomain_model ON embedding (id_searchdomain)", []).Result; _ = helper.ExecuteSQLNonQuery("CREATE INDEX idx_embedding_searchdomain_model ON embedding (id_searchdomain)", []).Result;
return 10;
return 6;
} }
} }