From dbe040f852140fd735146cbc8a589c3b0a5d15c8 Mon Sep 17 00:00:00 2001 From: spiral Date: Sun, 26 Sep 2021 23:18:17 -0400 Subject: [PATCH] feat(apiv2): database changes --- PluralKit.Core/Database/Migrations/18.sql | 18 ++++++++++++++++++ .../Repository/ModelRepository.Group.cs | 4 ++++ .../Repository/ModelRepository.Member.cs | 3 +++ .../Repository/ModelRepository.Switch.cs | 7 ++++++- .../Repository/ModelRepository.System.cs | 14 +++++++++++--- .../Database/Utils/DatabaseMigrator.cs | 2 +- PluralKit.Core/Models/PKGroup.cs | 3 +++ PluralKit.Core/Models/PKMember.cs | 2 ++ PluralKit.Core/Models/PKSwitch.cs | 5 +++-- PluralKit.Core/Models/PKSystem.cs | 3 +++ 10 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 PluralKit.Core/Database/Migrations/18.sql diff --git a/PluralKit.Core/Database/Migrations/18.sql b/PluralKit.Core/Database/Migrations/18.sql new file mode 100644 index 00000000..67224245 --- /dev/null +++ b/PluralKit.Core/Database/Migrations/18.sql @@ -0,0 +1,18 @@ +-- schema version 18: 2021-09-26 -- +-- Add UUIDs for APIs + +create extension if not exists pgcrypto; + +alter table systems add column uuid uuid default gen_random_uuid(); +create index systems_uuid_idx on systems(uuid); + +alter table members add column uuid uuid default gen_random_uuid(); +create index members_uuid_idx on members(uuid); + +alter table switches add column uuid uuid default gen_random_uuid(); +create index switches_uuid_idx on switches(uuid); + +alter table groups add column uuid uuid default gen_random_uuid(); +create index groups_uuid_idx on groups(uuid); + +update info set schema_version = 18; diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Group.cs b/PluralKit.Core/Database/Repository/ModelRepository.Group.cs index 67bc1147..74e96358 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Group.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Group.cs @@ -1,4 +1,5 @@ #nullable enable +using System; using System.Collections.Generic; using System.Data; using System.Linq; @@ -23,6 +24,9 @@ namespace PluralKit.Core new { hid = hid.ToLowerInvariant(), System = system } ); + public Task GetGroupByGuid(IPKConnection conn, Guid guid) => + conn.QueryFirstOrDefaultAsync("select * from groups where uuid = @Uuid", new { Uuid = guid }); + public Task GetGroupMemberCount(IPKConnection conn, GroupId id, PrivacyLevel? privacyFilter = null) { var query = new StringBuilder("select count(*) from group_members"); diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Member.cs b/PluralKit.Core/Database/Repository/ModelRepository.Member.cs index 654c08ff..74d680a0 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Member.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Member.cs @@ -18,6 +18,9 @@ namespace PluralKit.Core new { Hid = hid.ToLower(), System = system } ); + public Task GetMemberByGuid(IPKConnection conn, Guid guid) => + conn.QuerySingleOrDefaultAsync("select * from members where uuid = @Uuid", new { Uuid = guid }); + public Task GetMemberByName(IPKConnection conn, SystemId system, string name) => conn.QueryFirstOrDefaultAsync("select * from members where lower(name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system }); diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Switch.cs b/PluralKit.Core/Database/Repository/ModelRepository.Switch.cs index 11168bd1..fb4a1441 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Switch.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Switch.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -12,7 +13,7 @@ namespace PluralKit.Core { public partial class ModelRepository { - public async Task AddSwitch(IPKConnection conn, SystemId system, IReadOnlyCollection members) + public async Task AddSwitch(IPKConnection conn, SystemId system, IReadOnlyCollection members) { // Use a transaction here since we're doing multiple executed commands in one await using var tx = await conn.BeginTransactionAsync(); @@ -38,6 +39,7 @@ namespace PluralKit.Core await tx.CommitAsync(); _logger.Information("Created {SwitchId} in {SystemId}: {Members}", sw.Id, system, members); + return sw; } public async Task EditSwitch(IPKConnection conn, SwitchId switchId, IReadOnlyCollection members) { @@ -95,6 +97,9 @@ namespace PluralKit.Core new { System = system }); } + public Task GetSwitchByUuid(IPKConnection conn, Guid uuid) => + conn.QuerySingleOrDefaultAsync("select * from switches where uuid = @Uuid", new { Uuid = uuid }); + public async Task GetSwitchCount(IPKConnection conn, SystemId system) { return await conn.QuerySingleAsync("select count(*) from switches where system = @Id", new { Id = system }); diff --git a/PluralKit.Core/Database/Repository/ModelRepository.System.cs b/PluralKit.Core/Database/Repository/ModelRepository.System.cs index 3028793f..f03d03c4 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.System.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.System.cs @@ -1,4 +1,5 @@ #nullable enable +using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; @@ -12,6 +13,9 @@ namespace PluralKit.Core public Task GetSystem(IPKConnection conn, SystemId id) => conn.QueryFirstOrDefaultAsync("select * from systems where id = @id", new { id }); + public Task GetSystemByGuid(IPKConnection conn, Guid id) => + conn.QueryFirstOrDefaultAsync("select * from systems where uuid = @id", new { id }); + public Task GetSystemByAccount(IPKConnection conn, ulong accountId) => conn.QuerySingleOrDefaultAsync( "select systems.* from systems, accounts where accounts.system = systems.id and accounts.uid = @Id", @@ -38,9 +42,13 @@ namespace PluralKit.Core return conn.QuerySingleAsync(query.ToString(), new { Id = id }); } - public Task GetSystemGroupCount(IPKConnection conn, SystemId id) => - conn.QuerySingleAsync("select count(*) from groups where system = @System", new { System = id }); - + public Task GetSystemGroupCount(IPKConnection conn, SystemId id, PrivacyLevel? privacyFilter = null) + { + var query = new StringBuilder("select count(*) from groups where system = @Id"); + if (privacyFilter != null) + query.Append($" and visibility = {(int)privacyFilter.Value}"); + return conn.QuerySingleAsync(query.ToString(), new { Id = id }); + } public async Task CreateSystem(IPKConnection conn, string? systemName = null, IPKTransaction? tx = null) { var system = await conn.QuerySingleAsync( diff --git a/PluralKit.Core/Database/Utils/DatabaseMigrator.cs b/PluralKit.Core/Database/Utils/DatabaseMigrator.cs index a5055568..a7a7ca36 100644 --- a/PluralKit.Core/Database/Utils/DatabaseMigrator.cs +++ b/PluralKit.Core/Database/Utils/DatabaseMigrator.cs @@ -12,7 +12,7 @@ namespace PluralKit.Core internal class DatabaseMigrator { private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files - private const int TargetSchemaVersion = 17; + private const int TargetSchemaVersion = 18; private readonly ILogger _logger; public DatabaseMigrator(ILogger logger) diff --git a/PluralKit.Core/Models/PKGroup.cs b/PluralKit.Core/Models/PKGroup.cs index b288c69a..a08425fe 100644 --- a/PluralKit.Core/Models/PKGroup.cs +++ b/PluralKit.Core/Models/PKGroup.cs @@ -1,3 +1,5 @@ +using System; + using NodaTime; using Newtonsoft.Json.Linq; @@ -33,6 +35,7 @@ namespace PluralKit.Core { public GroupId Id { get; private set; } public string Hid { get; private set; } = null!; + public Guid Uuid { get; private set; } public SystemId System { get; private set; } public string Name { get; private set; } = null!; diff --git a/PluralKit.Core/Models/PKMember.cs b/PluralKit.Core/Models/PKMember.cs index 3e1e9777..b8e555f0 100644 --- a/PluralKit.Core/Models/PKMember.cs +++ b/PluralKit.Core/Models/PKMember.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; @@ -39,6 +40,7 @@ namespace PluralKit.Core // when trying to map to *subclasses* (eg. ListedMember). Adding private setters makes it work anyway. public MemberId Id { get; private set; } public string Hid { get; private set; } + public Guid Uuid { get; private set; } public SystemId System { get; private set; } public string Color { get; private set; } public string AvatarUrl { get; private set; } diff --git a/PluralKit.Core/Models/PKSwitch.cs b/PluralKit.Core/Models/PKSwitch.cs index e2209f6c..4ae1fd24 100644 --- a/PluralKit.Core/Models/PKSwitch.cs +++ b/PluralKit.Core/Models/PKSwitch.cs @@ -1,7 +1,7 @@ +using System; + using NodaTime; - - namespace PluralKit.Core { @@ -32,6 +32,7 @@ namespace PluralKit.Core public class PKSwitch { public SwitchId Id { get; } + public Guid Uuid { get; private set; } public SystemId System { get; set; } public Instant Timestamp { get; } } diff --git a/PluralKit.Core/Models/PKSystem.cs b/PluralKit.Core/Models/PKSystem.cs index b2c0d34f..fb0c7d07 100644 --- a/PluralKit.Core/Models/PKSystem.cs +++ b/PluralKit.Core/Models/PKSystem.cs @@ -1,3 +1,5 @@ +using System; + using Dapper.Contrib.Extensions; using Newtonsoft.Json; @@ -36,6 +38,7 @@ namespace PluralKit.Core { [Key] public SystemId Id { get; } public string Hid { get; } + public Guid Uuid { get; private set; } public string Name { get; } public string Description { get; } public string Tag { get; }