From a0875ef229b83b4dc110ae8a7a5883acfa4bd7b0 Mon Sep 17 00:00:00 2001 From: Kieran Date: Mon, 13 Jun 2022 15:20:12 +0100 Subject: [PATCH] Finish postgres migration --- .../Services/Migrations/MigrateToPostgres.cs | 35 +++++++++++++++++-- VoidCat/Services/Users/CacheUserStore.cs | 17 ++------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/VoidCat/Services/Migrations/MigrateToPostgres.cs b/VoidCat/Services/Migrations/MigrateToPostgres.cs index e220484..645f7c8 100644 --- a/VoidCat/Services/Migrations/MigrateToPostgres.cs +++ b/VoidCat/Services/Migrations/MigrateToPostgres.cs @@ -2,9 +2,11 @@ using VoidCat.Services.Abstractions; using VoidCat.Services.Files; using VoidCat.Services.Paywall; +using VoidCat.Services.Users; namespace VoidCat.Services.Migrations; +/// public class MigrateToPostgres : IMigration { private readonly ILogger _logger; @@ -12,17 +14,20 @@ public class MigrateToPostgres : IMigration private readonly IFileMetadataStore _fileMetadata; private readonly ICache _cache; private readonly IPaywallStore _paywallStore; + private readonly IUserStore _userStore; public MigrateToPostgres(VoidSettings settings, ILogger logger, IFileMetadataStore fileMetadata, - ICache cache, IPaywallStore paywallStore) + ICache cache, IPaywallStore paywallStore, IUserStore userStore) { _logger = logger; _settings = settings; _fileMetadata = fileMetadata; _cache = cache; _paywallStore = paywallStore; + _userStore = userStore; } + /// public async ValueTask Migrate(string[] args) { if (args.Contains("--migrate-local-metadata-to-postgres")) @@ -37,6 +42,12 @@ public class MigrateToPostgres : IMigration return IMigration.MigrationResult.ExitCompleted; } + if (args.Contains("--migrate-cache-users-to-postgres")) + { + await MigrateUsers(); + return IMigration.MigrationResult.ExitCompleted; + } + return IMigration.MigrationResult.Skipped; } @@ -64,7 +75,7 @@ public class MigrateToPostgres : IMigration private async Task MigratePaywall() { var cachePaywallStore = new CachePaywallStore(_cache); - + var files = await _fileMetadata.ListFiles(new(0, int.MaxValue)); await foreach (var file in files.Results) { @@ -83,4 +94,24 @@ public class MigrateToPostgres : IMigration } } } + + private async Task MigrateUsers() + { + var cacheUsers = new CacheUserStore(_cache); + + var users = await cacheUsers.ListUsers(new(0, int.MaxValue)); + await foreach (var user in users.Results) + { + try + { + var privateUser = await cacheUsers.GetPrivate(user.Id); + await _userStore.Set(privateUser!.Id, privateUser); + _logger.LogInformation("Migrated user {USer}", user.Id); + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to migrate user {User}", user.Id); + } + } + } } \ No newline at end of file diff --git a/VoidCat/Services/Users/CacheUserStore.cs b/VoidCat/Services/Users/CacheUserStore.cs index d684d62..08a898e 100644 --- a/VoidCat/Services/Users/CacheUserStore.cs +++ b/VoidCat/Services/Users/CacheUserStore.cs @@ -7,13 +7,11 @@ namespace VoidCat.Services.Users; public class CacheUserStore : IUserStore { private const string UserList = "users"; - private readonly ILogger _logger; private readonly ICache _cache; - public CacheUserStore(ICache cache, ILogger logger) + public CacheUserStore(ICache cache) { _cache = cache; - _logger = logger; } /// @@ -23,18 +21,9 @@ public class CacheUserStore : IUserStore } /// - public async ValueTask Get(Guid id) where T : VoidUser + public ValueTask Get(Guid id) where T : VoidUser { - try - { - return await _cache.Get(MapKey(id)); - } - catch (FormatException) - { - _logger.LogWarning("Corrupt user data at: {Key}", MapKey(id)); - } - - return default; + return _cache.Get(MapKey(id)); } ///