Finish postgres migration

This commit is contained in:
Kieran 2022-06-13 15:20:12 +01:00
parent e49c2fe870
commit a0875ef229
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 36 additions and 16 deletions

View File

@ -2,9 +2,11 @@
using VoidCat.Services.Abstractions; using VoidCat.Services.Abstractions;
using VoidCat.Services.Files; using VoidCat.Services.Files;
using VoidCat.Services.Paywall; using VoidCat.Services.Paywall;
using VoidCat.Services.Users;
namespace VoidCat.Services.Migrations; namespace VoidCat.Services.Migrations;
/// <inheritdoc />
public class MigrateToPostgres : IMigration public class MigrateToPostgres : IMigration
{ {
private readonly ILogger<MigrateToPostgres> _logger; private readonly ILogger<MigrateToPostgres> _logger;
@ -12,17 +14,20 @@ public class MigrateToPostgres : IMigration
private readonly IFileMetadataStore _fileMetadata; private readonly IFileMetadataStore _fileMetadata;
private readonly ICache _cache; private readonly ICache _cache;
private readonly IPaywallStore _paywallStore; private readonly IPaywallStore _paywallStore;
private readonly IUserStore _userStore;
public MigrateToPostgres(VoidSettings settings, ILogger<MigrateToPostgres> logger, IFileMetadataStore fileMetadata, public MigrateToPostgres(VoidSettings settings, ILogger<MigrateToPostgres> logger, IFileMetadataStore fileMetadata,
ICache cache, IPaywallStore paywallStore) ICache cache, IPaywallStore paywallStore, IUserStore userStore)
{ {
_logger = logger; _logger = logger;
_settings = settings; _settings = settings;
_fileMetadata = fileMetadata; _fileMetadata = fileMetadata;
_cache = cache; _cache = cache;
_paywallStore = paywallStore; _paywallStore = paywallStore;
_userStore = userStore;
} }
/// <inheritdoc />
public async ValueTask<IMigration.MigrationResult> Migrate(string[] args) public async ValueTask<IMigration.MigrationResult> Migrate(string[] args)
{ {
if (args.Contains("--migrate-local-metadata-to-postgres")) if (args.Contains("--migrate-local-metadata-to-postgres"))
@ -37,6 +42,12 @@ public class MigrateToPostgres : IMigration
return IMigration.MigrationResult.ExitCompleted; return IMigration.MigrationResult.ExitCompleted;
} }
if (args.Contains("--migrate-cache-users-to-postgres"))
{
await MigrateUsers();
return IMigration.MigrationResult.ExitCompleted;
}
return IMigration.MigrationResult.Skipped; return IMigration.MigrationResult.Skipped;
} }
@ -64,7 +75,7 @@ public class MigrateToPostgres : IMigration
private async Task MigratePaywall() private async Task MigratePaywall()
{ {
var cachePaywallStore = new CachePaywallStore(_cache); var cachePaywallStore = new CachePaywallStore(_cache);
var files = await _fileMetadata.ListFiles<VoidFileMeta>(new(0, int.MaxValue)); var files = await _fileMetadata.ListFiles<VoidFileMeta>(new(0, int.MaxValue));
await foreach (var file in files.Results) 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);
}
}
}
} }

View File

@ -7,13 +7,11 @@ namespace VoidCat.Services.Users;
public class CacheUserStore : IUserStore public class CacheUserStore : IUserStore
{ {
private const string UserList = "users"; private const string UserList = "users";
private readonly ILogger<CacheUserStore> _logger;
private readonly ICache _cache; private readonly ICache _cache;
public CacheUserStore(ICache cache, ILogger<CacheUserStore> logger) public CacheUserStore(ICache cache)
{ {
_cache = cache; _cache = cache;
_logger = logger;
} }
/// <inheritdoc /> /// <inheritdoc />
@ -23,18 +21,9 @@ public class CacheUserStore : IUserStore
} }
/// <inheritdoc /> /// <inheritdoc />
public async ValueTask<T?> Get<T>(Guid id) where T : VoidUser public ValueTask<T?> Get<T>(Guid id) where T : VoidUser
{ {
try return _cache.Get<T>(MapKey(id));
{
return await _cache.Get<T>(MapKey(id));
}
catch (FormatException)
{
_logger.LogWarning("Corrupt user data at: {Key}", MapKey(id));
}
return default;
} }
/// <inheritdoc /> /// <inheritdoc />