Skip to content

Commit

Permalink
Improve migration performance (#16784)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Migaroez authored Jul 31, 2024
1 parent dfe41d7 commit bb65436
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ protected override void Migrate()
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>()
Expand All @@ -44,4 +56,17 @@ protected override void Migrate()

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ protected override void Migrate()
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);
Expand All @@ -40,6 +49,13 @@ private void MigrateNtextColumn<TDto>(string columnName, string tableName, Expre
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.
Expand Down

0 comments on commit bb65436

Please sign in to comment.