Improve migration performance (#16784)

* Improve migration performance

* Fix PR review comments

* Revert tags migration for sql lite as the optimized sql doesn't work properly and sqlLite datasets should not be large anyway
This commit is contained in:
Sven Geusens
2024-07-31 10:35:28 +02:00
committed by GitHub
parent dfe41d7f76
commit bb654360bf
2 changed files with 41 additions and 0 deletions

View File

@@ -25,6 +25,18 @@ public class MigrateTagsFromNVarcharToNText : MigrationBase
Database.Execute(updateDbTypeForTagsQuery);
// Then migrate the data from "varcharValue" column to "textValue"
if (Database.DatabaseType == DatabaseType.SQLite)
{
MigrateSqlLiteData();
}
else
{
MigrateSqlServerData();
}
}
private void MigrateSqlLiteData()
{
Sql<ISqlContext> tagsDataTypeIdQuery = Database.SqlContext.Sql()
.Select<DataTypeDto>(dt => dt.NodeId)
.From<DataTypeDto>()
@@ -44,4 +56,17 @@ public class MigrateTagsFromNVarcharToNText : MigrationBase
Database.Execute(updatePropertyDataColumnsQuery);
}
private void MigrateSqlServerData()
{
Sql<ISqlContext> updateTagsValues = Database.SqlContext.Sql()
.Update<PropertyDataDto>()
.Append("SET textValue = COALESCE([textValue], [varCharValue]), varcharValue = null")
.From<DataTypeDto>()
.InnerJoin<PropertyTypeDto>().On<DataTypeDto, PropertyTypeDto>((dt, pt) => dt.NodeId == pt.DataTypeId)
.InnerJoin<PropertyDataDto>().On<PropertyTypeDto, PropertyDataDto>((pt, pd) => pt.Id == pd.PropertyTypeId)
.Where<DataTypeDto>(dt => dt.EditorAlias == Constants.PropertyEditors.Aliases.Tags);
Database.Execute(updateTagsValues);
}
}

View File

@@ -32,6 +32,15 @@ public class UseNvarcharInsteadOfNText : MigrationBase
MigrateNtextColumn<PropertyDataDto>("textValue", Constants.DatabaseSchema.Tables.PropertyData, x => x.TextValue);
}
private void RawMigrateNtextColumn(string columnName, string tableName)
{
var updateTypeSql = @$"
ALTER TABLE {tableName}
ALTER COLUMN {columnName} nvarchar(max)";
Sql<ISqlContext> copyDataQuery = Database.SqlContext.Sql(updateTypeSql);
Database.Execute(copyDataQuery);
}
private void MigrateNtextColumn<TDto>(string columnName, string tableName, Expression<Func<TDto, object?>> fieldSelector, bool nullable = true)
{
var columnType = ColumnType(tableName, columnName);
@@ -40,6 +49,13 @@ public class UseNvarcharInsteadOfNText : MigrationBase
return;
}
// since it's ntext to nvarchar(max) we should be able to just raw query this without having issues with fallback values on sql server
if (Database.DatabaseType != DatabaseType.SQLite)
{
RawMigrateNtextColumn(columnName, tableName);
return;
}
var oldColumnName = $"Old{columnName}";
// Rename the column so we can create the new one and copy over the data.