From ac829e375fa89f9643d2c3e0e9c6dc273efd9da3 Mon Sep 17 00:00:00 2001 From: Toaster Date: Mon, 8 Sep 2025 11:57:41 +0200 Subject: [PATCH] Fix the --import-assets command failing when trying to update assets --- Refresh.Core/Importing/AssetImporter.cs | 14 +++++++++----- Refresh.Database/GameDatabaseContext.Assets.cs | 18 ++++++++++++------ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Refresh.Core/Importing/AssetImporter.cs b/Refresh.Core/Importing/AssetImporter.cs index 165464e98..61bfdaa8d 100644 --- a/Refresh.Core/Importing/AssetImporter.cs +++ b/Refresh.Core/Importing/AssetImporter.cs @@ -42,7 +42,8 @@ public void ImportFromDataStore(GameDatabaseProvider databaseProvider, IDataStor { using GameDatabaseContext database = databaseProvider.GetContext(); - List assets = new(assetHashes.Length / taskCount); + List assetsToAdd = new(assetHashes.Length / taskCount); + List assetsToUpdate = new(assetHashes.Length / taskCount); while (hashesToProcess.TryTake(out string? path)) { @@ -63,19 +64,22 @@ public void ImportFromDataStore(GameDatabaseProvider databaseProvider, IDataStor { newAsset.OriginalUploader = oldAsset.OriginalUploader; newAsset.UploadDate = oldAsset.UploadDate; + assetsToUpdate.Add(newAsset); Interlocked.Increment(ref updatedAssets); } else { + assetsToAdd.Add(newAsset); Interlocked.Increment(ref newAssets); } - assets.Add(newAsset); this.Info($"[{Interlocked.Increment(ref processed)}/{assetHashes.Length}] Processed {newAsset.AssetType} asset {hash} ({AssetSafetyLevelExtensions.FromAssetType(newAsset.AssetType, newAsset.AssetFormat)})"); } - - database.AddOrUpdateAssetsInDatabase(assets); - + + database.ChangeTracker.Clear(); // Updating assets will throw otherwise + database.AddAssetsToDatabase(assetsToAdd); + database.UpdateAssetsInDatabase(assetsToUpdate); + return Task.CompletedTask; }, TaskCreationOptions.LongRunning); } diff --git a/Refresh.Database/GameDatabaseContext.Assets.cs b/Refresh.Database/GameDatabaseContext.Assets.cs index ca2faa1d7..2e548c3cc 100644 --- a/Refresh.Database/GameDatabaseContext.Assets.cs +++ b/Refresh.Database/GameDatabaseContext.Assets.cs @@ -77,12 +77,18 @@ public void AddAssetToDatabase(GameAsset asset) => { this.GameAssets.Add(asset); }); - - public void AddOrUpdateAssetsInDatabase(IEnumerable assets) => - this.Write(() => - { - this.GameAssets.AddRange(assets, true); - }); + + public void AddAssetsToDatabase(IEnumerable assets) + { + this.GameAssets.AddRange(assets); + this.SaveChanges(); + } + + public void UpdateAssetsInDatabase(IEnumerable assets) + { + this.GameAssets.UpdateRange(assets); + this.SaveChanges(); + } public void SetMainlineIconHash(GameAsset asset, string hash) => this.Write(() =>