feat: pk;config

This commit is contained in:
spiral
2021-11-29 21:35:21 -05:00
parent d195c80d92
commit 56d07e0f2d
41 changed files with 648 additions and 313 deletions

View File

@@ -23,8 +23,9 @@
as $$
-- CTEs to query "static" (accessible only through args) data
with
system as (select systems.*, system_guild.tag as guild_tag, system_guild.tag_enabled as tag_enabled, allow_autoproxy as account_autoproxy from accounts
system as (select systems.*, config.latch_timeout, system_guild.tag as guild_tag, system_guild.tag_enabled as tag_enabled, allow_autoproxy as account_autoproxy from accounts
left join systems on systems.id = accounts.system
left join config on config.system = accounts.system
left join system_guild on system_guild.system = accounts.system and system_guild.guild = guild_id
where accounts.uid = account_id),
guild as (select * from servers where id = guild_id),
@@ -48,11 +49,12 @@ as $$
coalesce(system.tag_enabled, true) as tag_enabled,
system.avatar_url as system_avatar,
system.account_autoproxy as allow_autoproxy,
system.latch_timeout as latch_timeout
config.latch_timeout as latch_timeout
-- We need a "from" clause, so we just use some bogus data that's always present
-- This ensure we always have exactly one row going forward, so we can left join afterwards and still get data
from (select 1) as _placeholder
left join system on true
left join config on true
left join guild on true
left join last_message on true
left join system_last_switch on system_last_switch.system = system.id

View File

@@ -0,0 +1,29 @@
-- schema version 21
-- create `config` table
create table config (
system int primary key references systems(id) on delete cascade,
ui_tz text not null default 'UTC',
pings_enabled bool not null default true,
latch_timeout int,
member_limit_override int,
group_limit_override int
);
insert into config select
id as system,
ui_tz,
pings_enabled,
latch_timeout,
member_limit_override,
group_limit_override
from systems;
alter table systems
drop column ui_tz,
drop column pings_enabled,
drop column latch_timeout,
drop column member_limit_override,
drop column group_limit_override;
update info set schema_version = 21;

View File

@@ -0,0 +1,23 @@
using SqlKata;
namespace PluralKit.Core;
public partial class ModelRepository
{
public Task<SystemConfig> GetSystemConfig(SystemId system)
=> _db.QueryFirst<SystemConfig>(new Query("config").Where("system", system));
public async Task<SystemConfig> UpdateSystemConfig(SystemId system, SystemConfigPatch patch)
{
var query = patch.Apply(new Query("config").Where("system", system));
var config = await _db.QueryFirst<SystemConfig>(query, "returning *");
_ = _dispatch.Dispatch(system, new UpdateDispatchData
{
Event = DispatchEvent.UPDATE_SETTINGS,
EventData = patch.ToJson()
});
return config;
}
}

View File

@@ -1,4 +1,6 @@
#nullable enable
using Dapper;
using SqlKata;
namespace PluralKit.Core;
@@ -78,6 +80,8 @@ public partial class ModelRepository
var system = await _db.QueryFirst<PKSystem>(conn, query, "returning *");
_logger.Information("Created {SystemId}", system.Id);
await _db.Execute(conn => conn.QueryAsync("insert into config (system) value (@system)", new { system = system.Id }));
// no dispatch call here - system was just created, we don't have a webhook URL
return system;
}

View File

@@ -9,7 +9,7 @@ namespace PluralKit.Core;
internal class DatabaseMigrator
{
private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files
private const int TargetSchemaVersion = 20;
private const int TargetSchemaVersion = 21;
private readonly ILogger _logger;
public DatabaseMigrator(ILogger logger)