feat: rewrite database schema for localized autoproxy
This commit is contained in:
@@ -15,10 +15,6 @@ public class MessageContext
|
||||
public bool InLogBlacklist { get; }
|
||||
public bool LogCleanupEnabled { get; }
|
||||
public bool ProxyEnabled { get; }
|
||||
public AutoproxyMode AutoproxyMode { get; }
|
||||
public MemberId? AutoproxyMember { get; }
|
||||
public ulong? LastMessage { get; }
|
||||
public MemberId? LastMessageMember { get; }
|
||||
public SwitchId? LastSwitch { get; }
|
||||
public MemberId[] LastSwitchMembers { get; } = new MemberId[0];
|
||||
public Instant? LastSwitchTimestamp { get; }
|
||||
|
@@ -6,10 +6,6 @@
|
||||
in_log_blacklist bool,
|
||||
log_cleanup_enabled bool,
|
||||
proxy_enabled bool,
|
||||
autoproxy_mode int,
|
||||
autoproxy_member int,
|
||||
last_message bigint,
|
||||
last_message_member int,
|
||||
last_switch int,
|
||||
last_switch_members int[],
|
||||
last_switch_timestamp timestamp,
|
||||
@@ -28,8 +24,7 @@ as $$
|
||||
left join system_config on system_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),
|
||||
last_message as (select * from messages where messages.guild = guild_id and messages.sender = account_id order by mid desc limit 1)
|
||||
guild as (select * from servers where id = guild_id)
|
||||
select
|
||||
system.id as system_id,
|
||||
guild.log_channel,
|
||||
@@ -37,10 +32,6 @@ as $$
|
||||
(channel_id = any(guild.log_blacklist)) as in_log_blacklist,
|
||||
coalesce(guild.log_cleanup_enabled, false),
|
||||
coalesce(system_guild.proxy_enabled, true) as proxy_enabled,
|
||||
coalesce(system_guild.autoproxy_mode, 1) as autoproxy_mode,
|
||||
system_guild.autoproxy_member,
|
||||
last_message.mid as last_message,
|
||||
last_message.member as last_message_member,
|
||||
system_last_switch.switch as last_switch,
|
||||
system_last_switch.members as last_switch_members,
|
||||
system_last_switch.timestamp as last_switch_timestamp,
|
||||
@@ -55,7 +46,6 @@ as $$
|
||||
from (select 1) as _placeholder
|
||||
left join system 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
|
||||
left join system_guild on system_guild.system = system.id and system_guild.guild = guild_id
|
||||
$$ language sql stable rows 1;
|
||||
|
36
PluralKit.Core/Database/Migrations/27.sql
Normal file
36
PluralKit.Core/Database/Migrations/27.sql
Normal file
@@ -0,0 +1,36 @@
|
||||
-- schema version 27
|
||||
-- autoproxy locations
|
||||
|
||||
-- mode pseudo-enum: (copied from 3.sql)
|
||||
-- 1 = autoproxy off
|
||||
-- 2 = front mode (first fronter)
|
||||
-- 3 = latch mode (last proxyer)
|
||||
-- 4 = member mode (specific member)
|
||||
|
||||
create table autoproxy (
|
||||
system int references systems(id) on delete cascade,
|
||||
channel_id bigint,
|
||||
guild_id bigint,
|
||||
autoproxy_mode int check (mode in (1, 2, 3, 4)) not null default 1,
|
||||
autoproxy_member int references members(id) on delete set null,
|
||||
last_latch_timestamp timestamp,
|
||||
check (
|
||||
(channel_id = 0 and guild_id = 0)
|
||||
or (channel_id != 0 and guild_id = 0)
|
||||
or (channel_id = 0 and guild_id != 0)
|
||||
),
|
||||
primary key (system, channel_id, guild_id)
|
||||
);
|
||||
|
||||
insert into autoproxy select
|
||||
system,
|
||||
0 as channel_id,
|
||||
guild as guild_id,
|
||||
autoproxy_mode,
|
||||
autoproxy_member
|
||||
from system_guild;
|
||||
|
||||
alter table system_guild drop column autoproxy_mode;
|
||||
alter table system_guild drop column autoproxy_member;
|
||||
|
||||
update info set schema_version = 27;
|
@@ -0,0 +1,35 @@
|
||||
using Dapper;
|
||||
|
||||
using SqlKata;
|
||||
|
||||
namespace PluralKit.Core;
|
||||
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public async Task UpdateAutoproxy(SystemId system, ulong? guildId, ulong? channelId, AutoproxyPatch patch)
|
||||
{
|
||||
var locationStr = guildId != null ? "guild" : (channelId != null ? "channel" : "global");
|
||||
_logger.Information("Updated autoproxy for {SystemId} in location {location}: {@AutoproxyPatch}", system, locationStr, patch);
|
||||
|
||||
var query = patch.Apply(new Query("autoproxy")
|
||||
.Where("system", system)
|
||||
.Where("guild_id", guildId ?? 0)
|
||||
.Where("channel_id", channelId ?? 0)
|
||||
);
|
||||
_ = _dispatch.Dispatch(system, guildId, channelId, patch);
|
||||
await _db.ExecuteQuery(query);
|
||||
}
|
||||
|
||||
// todo: this might break with differently scoped autoproxy
|
||||
public async Task<AutoproxySettings> GetAutoproxySettings(SystemId system, ulong? guildId, ulong? channelId)
|
||||
=> await _db.QueryFirst<AutoproxySettings>(new Query("autoproxy").AsInsert(new {
|
||||
system = system,
|
||||
guild_id = guildId ?? 0,
|
||||
channel_id = channelId ?? 0,
|
||||
})
|
||||
.Where("system", system)
|
||||
.Where("guild_id", guildId ?? 0)
|
||||
.Where("channel_id", channelId ?? 0),
|
||||
"on conflict (system, guild_id, channel_id) do update set system = $1 returning *"
|
||||
);
|
||||
}
|
@@ -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 = 26;
|
||||
private const int TargetSchemaVersion = 27;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public DatabaseMigrator(ILogger logger)
|
||||
|
Reference in New Issue
Block a user