Skip to content

Commit

Permalink
fix: update dbguildid, bulk update, debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
lodicolo committed Oct 10, 2023
1 parent 25bec0c commit 0459dcf
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions Intersect.Server/Database/SqliteNetCoreGuidPatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Dapper;
using Intersect.Config;
using Intersect.Logging;
using Intersect.Reflection;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down Expand Up @@ -64,8 +66,11 @@ public static void ApplyTo<TContext>(TContext context)
.Where("type", "table")
.Get<string>()
.ToList();

var startTimeDatabase = DateTime.UtcNow;
foreach (var entityType in context.Model.GetEntityTypes())
{
var startTimeTable = DateTime.UtcNow;
var entityTable = entityType.GetTableName();
if (!tablesInDatabase.Contains(entityTable))
{
Expand All @@ -87,15 +92,23 @@ public static void ApplyTo<TContext>(TContext context)
)
.Select(column => column.Name)
.ToArray();

if (entityTable == "Players")
{
guidColumnNames = guidColumnNames.Append("DbGuildId").Distinct().ToArray();
}

var notNullLookup = columnsInDatabase.ToDictionary(
column => column.Name,
column => column.NotNull
);

var numberOfRows = queryFactory.Query(entityTable).Select("*").AsCount().First<int>();
var convertedRowCount = 0;
const int take = 100;
for (var skip = 0; skip < numberOfRows; skip += take)
{
var startTimeSegment = DateTime.UtcNow;
var rows = queryFactory
.Query(entityTable)
.Select(guidColumnNames)
Expand Down Expand Up @@ -142,20 +155,40 @@ public static void ApplyTo<TContext>(TContext context)
}
)
.ToList();

dbConnection.Open();
var transaction = dbConnection.BeginTransaction();
var segmentConvertedRowCount = 0;
foreach (var (convertedRow, searchId) in convertedRows)
{
var result = queryFactory
.Query(entityTable)
.Where("Id", searchId)
.AsUpdate(convertedRow)
.Get();
result.ToString();
.Update(convertedRow, transaction: transaction);

var searchIdString = searchId is byte[] searchIdBytes
? new Guid(searchIdBytes).ToString()
: searchId.ToString();

if (Log.Default.Configuration.LogLevel >= LogLevel.Debug)
{
Log.Debug($"Processed row {convertedRowCount++}/{numberOfRows} in '{entityTable}' (segment {segmentConvertedRowCount++}/{convertedRows.Count}) ({searchIdString} was {searchId.GetFullishName()}), {result} rows changed.");
}
}
transaction.Commit();
dbConnection.Close();

string.Empty.ToString();
if (Log.Default.Configuration.LogLevel >= LogLevel.Debug)
{
Log.Debug(
$"Completed updating segment in {(DateTime.UtcNow - startTimeSegment).TotalMilliseconds}ms ('{entityTable}', {convertedRows.Count} rows updated)"
);
}
}

string.Empty.ToString();
Log.Verbose($"Completed updating table in {(DateTime.UtcNow - startTimeTable).TotalMilliseconds}ms ('{entityTable}', {numberOfRows} rows updated)");
}

Log.Verbose($"Completed updating database in {(DateTime.UtcNow - startTimeDatabase).TotalMilliseconds}ms");
}
}

0 comments on commit 0459dcf

Please sign in to comment.