Skip to content

Commit

Permalink
fix: db upgrade issue on User_Variables column
Browse files Browse the repository at this point in the history
  • Loading branch information
lodicolo committed Oct 15, 2023
1 parent 047c6be commit ba7cf70
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions Intersect.Server/Database/SqliteNetCoreGuidPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Remotion.Linq.Clauses;
using SqlKata.Execution;

namespace Intersect.Server.Database;
Expand Down Expand Up @@ -103,6 +104,12 @@ public static void ApplyTo<TContext>(TContext context)
column => column.NotNull
);

var searchIdColumns = new[] { "Id" };
if (!guidColumnNames.Contains("Id"))
{
searchIdColumns = guidColumnNames;
}

var numberOfRows = queryFactory.Query(entityTable).Select("*").AsCount().First<int>();
var convertedRowCount = 0;
const int take = 100;
Expand Down Expand Up @@ -151,28 +158,30 @@ public static void ApplyTo<TContext>(TContext context)
}
}
);
return (convertedRow, row["Id"]);
var searchValues = searchIdColumns
.Select(searchIdColumn => (searchIdColumn, row[searchIdColumn]))
.ToArray();
return (convertedRow, searchValues);
}
)
.ToList();

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

var searchIdString = searchId is byte[] searchIdBytes
? new Guid(searchIdBytes).ToString()
: searchId.ToString();
var result = query.Update(convertedRow, transaction: transaction);

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.");
Log.Debug($"Processed row {convertedRowCount++}/{numberOfRows} in '{entityTable}' (segment {segmentConvertedRowCount++}/{convertedRows.Count}), {result} rows changed.");
}
}
transaction.Commit();
Expand Down

0 comments on commit ba7cf70

Please sign in to comment.