From a75d5c7a34c044f83daffa3298e402cd4b92cbca Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Wed, 25 Dec 2024 22:41:56 +0100 Subject: [PATCH] Update migration add data truncation if source data exceeds length (#190) --- .../AliasVault.InstallCli/Program.cs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Utilities/AliasVault.InstallCli/Program.cs b/src/Utilities/AliasVault.InstallCli/Program.cs index 1b333ee95..576e46af5 100644 --- a/src/Utilities/AliasVault.InstallCli/Program.cs +++ b/src/Utilities/AliasVault.InstallCli/Program.cs @@ -226,7 +226,34 @@ public static partial class Program if (items.Count > 0) { - const int batchSize = 30; + // Get entity type from the model to check annotations + var entityType = destinationContext.Model.FindEntityType(typeof(T)); + + foreach (var item in items) + { + // Check each property for MaxLength annotation + foreach (var property in entityType!.GetProperties()) + { + // Only process string properties + if (property.ClrType == typeof(string)) + { + var maxLength = property.GetMaxLength(); + if (maxLength.HasValue) + { + var propertyInfo = typeof(T).GetProperty(property.Name); + var value = propertyInfo?.GetValue(item) as string; + + if (value?.Length > maxLength.Value) + { + propertyInfo?.SetValue(item, value.Substring(0, maxLength.Value)); + Console.WriteLine($"Truncated {property.Name} in {tableName} from {value.Length} to {maxLength.Value} characters"); + } + } + } + } + } + + const int batchSize = 50; foreach (var batch in items.Chunk(batchSize)) { try