forked from Kieran/void.cat
Finish postgres migration
This commit is contained in:
parent
e49c2fe870
commit
a0875ef229
@ -2,9 +2,11 @@
|
||||
using VoidCat.Services.Abstractions;
|
||||
using VoidCat.Services.Files;
|
||||
using VoidCat.Services.Paywall;
|
||||
using VoidCat.Services.Users;
|
||||
|
||||
namespace VoidCat.Services.Migrations;
|
||||
|
||||
/// <inheritdoc />
|
||||
public class MigrateToPostgres : IMigration
|
||||
{
|
||||
private readonly ILogger<MigrateToPostgres> _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<MigrateToPostgres> 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;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask<IMigration.MigrationResult> 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<VoidFileMeta>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -7,13 +7,11 @@ namespace VoidCat.Services.Users;
|
||||
public class CacheUserStore : IUserStore
|
||||
{
|
||||
private const string UserList = "users";
|
||||
private readonly ILogger<CacheUserStore> _logger;
|
||||
private readonly ICache _cache;
|
||||
|
||||
public CacheUserStore(ICache cache, ILogger<CacheUserStore> logger)
|
||||
public CacheUserStore(ICache cache)
|
||||
{
|
||||
_cache = cache;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -23,18 +21,9 @@ public class CacheUserStore : IUserStore
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask<T?> Get<T>(Guid id) where T : VoidUser
|
||||
public ValueTask<T?> Get<T>(Guid id) where T : VoidUser
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _cache.Get<T>(MapKey(id));
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
_logger.LogWarning("Corrupt user data at: {Key}", MapKey(id));
|
||||
}
|
||||
|
||||
return default;
|
||||
return _cache.Get<T>(MapKey(id));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
Loading…
Reference in New Issue
Block a user