feat: initial 6-character HID rework

This commit is contained in:
Iris System 2024-04-28 15:46:06 +12:00
parent 73f43b8cb3
commit 9f56697241
30 changed files with 208 additions and 91 deletions

View file

@ -122,13 +122,13 @@ begin
end
$$ language plpgsql;
create function generate_hid() returns char(5) as $$
select string_agg(substr('abcdefghijklmnopqrstuvwxyz', ceil(random() * 26)::integer, 1), '') from generate_series(1, 5)
create function generate_hid() returns char(6) as $$
select string_agg(substr('abcefghjknoprstuvwxyz', ceil(random() * 21)::integer, 1), '') from generate_series(1, 6)
$$ language sql volatile;
create function find_free_system_hid() returns char(5) as $$
declare new_hid char(5);
create function find_free_system_hid() returns char(6) as $$
declare new_hid char(6);
begin
loop
new_hid := generate_hid();
@ -138,8 +138,8 @@ end
$$ language plpgsql volatile;
create function find_free_member_hid() returns char(5) as $$
declare new_hid char(5);
create function find_free_member_hid() returns char(6) as $$
declare new_hid char(6);
begin
loop
new_hid := generate_hid();
@ -148,8 +148,9 @@ begin
end
$$ language plpgsql volatile;
create function find_free_group_hid() returns char(5) as $$
declare new_hid char(5);
create function find_free_group_hid() returns char(6) as $$
declare new_hid char(6);
begin
loop
new_hid := generate_hid();

View file

@ -0,0 +1,10 @@
-- database version 42
-- move to 6 character HIDs, add HID display config setting
alter table systems alter column hid type char(6) using rpad(hid, 6, ' ');
alter table members alter column hid type char(6) using rpad(hid, 6, ' ');
alter table groups alter column hid type char(6) using rpad(hid, 6, ' ');
alter table system_config add column hid_display_split bool default false;
update info set schema_version = 42;

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 = 41;
private const int TargetSchemaVersion = 42;
private readonly ILogger _logger;
public DatabaseMigrator(ILogger logger)

View file

@ -0,0 +1,35 @@
using System.Linq;
using System.Text.RegularExpressions;
namespace PluralKit.Core;
public static class HidUtils
{
private static readonly Regex _hidRegex = new(@"^[a-zA-Z]{5,6}$");
public static string? ParseHid(string input)
{
input = input.ToLower().Replace("-", null);
if (!_hidRegex.IsMatch(input))
return null;
return input;
}
public static bool TryParseHid(this string input, out string hid)
{
hid = ParseHid(input);
return hid != null;
}
public static string HidTransform(string input, bool split = false)
{
if (split && input.Length > 5)
{
var len = (int)Math.Floor(input.Length / 2.0);
input = string.Concat(input.AsSpan(0, len), "-", input.AsSpan(len));
}
return input;
}
}