feat: import/export system config

This commit is contained in:
spiral 2021-11-30 17:04:42 -05:00
parent 9097142718
commit cc4e659cec
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
6 changed files with 30 additions and 9 deletions

View File

@ -100,7 +100,7 @@ public class ImportExport
var json = await ctx.BusyIndicator(async () => var json = await ctx.BusyIndicator(async () =>
{ {
// Make the actual data file // Make the actual data file
var data = await _dataFiles.ExportSystem(ctx.System, ctx.Config.UiTz); var data = await _dataFiles.ExportSystem(ctx.System);
return JsonConvert.SerializeObject(data, Formatting.None); return JsonConvert.SerializeObject(data, Formatting.None);
}); });

View File

@ -4,13 +4,13 @@ namespace PluralKit.Core;
public partial class ModelRepository public partial class ModelRepository
{ {
public Task<SystemConfig> GetSystemConfig(SystemId system) public Task<SystemConfig> GetSystemConfig(SystemId system, IPKConnection conn = null)
=> _db.QueryFirst<SystemConfig>(new Query("config").Where("system", system)); => _db.QueryFirst<SystemConfig>(conn, new Query("config").Where("system", system));
public async Task<SystemConfig> UpdateSystemConfig(SystemId system, SystemConfigPatch patch) public async Task<SystemConfig> UpdateSystemConfig(SystemId system, SystemConfigPatch patch, IPKConnection conn = null)
{ {
var query = patch.Apply(new Query("config").Where("system", system)); var query = patch.Apply(new Query("config").Where("system", system));
var config = await _db.QueryFirst<SystemConfig>(query, "returning *"); var config = await _db.QueryFirst<SystemConfig>(conn, query, "returning *");
_ = _dispatch.Dispatch(system, new UpdateDispatchData _ = _dispatch.Dispatch(system, new UpdateDispatchData
{ {

View File

@ -80,7 +80,12 @@ public partial class ModelRepository
var system = await _db.QueryFirst<PKSystem>(conn, query, "returning *"); var system = await _db.QueryFirst<PKSystem>(conn, query, "returning *");
_logger.Information("Created {SystemId}", system.Id); _logger.Information("Created {SystemId}", system.Id);
await _db.Execute(conn => conn.QueryAsync("insert into config (system) values (@system)", new { system = system.Id })); var (q, pms) = ("insert into config (system) values (@system)", new { system = system.Id });
if (conn == null)
await _db.Execute(conn => conn.QueryAsync(q, pms));
else
await conn.QueryAsync(q, pms);
// no dispatch call here - system was just created, we don't have a webhook URL // no dispatch call here - system was just created, we don't have a webhook URL
return system; return system;

View File

@ -21,7 +21,7 @@ public class DataFileService
_dispatch = dispatch; _dispatch = dispatch;
} }
public async Task<JObject> ExportSystem(PKSystem system, string timezone) public async Task<JObject> ExportSystem(PKSystem system)
{ {
await using var conn = await _db.Obtain(); await using var conn = await _db.Obtain();
@ -30,7 +30,9 @@ public class DataFileService
o.Merge(system.ToJson(LookupContext.ByOwner)); o.Merge(system.ToJson(LookupContext.ByOwner));
o.Add("timezone", timezone); var config = await _repo.GetSystemConfig(system.Id);
o.Add("config", config.ToJson());
o.Add("accounts", new JArray((await _repo.GetSystemAccounts(system.Id)).ToList())); o.Add("accounts", new JArray((await _repo.GetSystemAccounts(system.Id)).ToList()));
o.Add("members", o.Add("members",
new JArray((await _repo.GetSystemMembers(system.Id).ToListAsync()).Select(m => new JArray((await _repo.GetSystemMembers(system.Id).ToListAsync()).Select(m =>

View File

@ -61,7 +61,7 @@ public partial class BulkImporter: IAsyncDisposable
importer._system = system; importer._system = system;
} }
importer._cfg = await repo.GetSystemConfig(system.Id); importer._cfg = await repo.GetSystemConfig(system.Id, conn);
// Fetch all members in the system and log their names and hids // Fetch all members in the system and log their names and hids
var members = await conn.QueryAsync<PKMember>("select id, hid, name from members where system = @System", var members = await conn.QueryAsync<PKMember>("select id, hid, name from members where system = @System",

View File

@ -30,6 +30,20 @@ public partial class BulkImporter
await _repo.UpdateSystem(_system.Id, patch, _conn); await _repo.UpdateSystem(_system.Id, patch, _conn);
var configPatch = new SystemConfigPatch();
if (importFile.ContainsKey("config"))
configPatch = SystemConfigPatch.FromJson(importFile.Value<JObject>("config"));
if (importFile.ContainsKey("timezone"))
configPatch.UiTz = importFile.Value<string>("timezone");
configPatch.AssertIsValid();
if (configPatch.Errors.Count > 0)
throw new ImportException($"Field config.{patch.Errors[0].Key} in export file is invalid.");
await _repo.UpdateSystemConfig(_system.Id, configPatch, _conn);
var members = importFile.Value<JArray>("members"); var members = importFile.Value<JArray>("members");
var groups = importFile.Value<JArray>("groups"); var groups = importFile.Value<JArray>("groups");
var switches = importFile.Value<JArray>("switches"); var switches = importFile.Value<JArray>("switches");