feat(apiv2): database changes
This commit is contained in:
parent
fb4aaad19f
commit
dbe040f852
PluralKit.Core
18
PluralKit.Core/Database/Migrations/18.sql
Normal file
18
PluralKit.Core/Database/Migrations/18.sql
Normal file
@ -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;
|
@ -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<PKGroup?> GetGroupByGuid(IPKConnection conn, Guid guid) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where uuid = @Uuid", new { Uuid = guid });
|
||||
|
||||
public Task<int> GetGroupMemberCount(IPKConnection conn, GroupId id, PrivacyLevel? privacyFilter = null)
|
||||
{
|
||||
var query = new StringBuilder("select count(*) from group_members");
|
||||
|
@ -18,6 +18,9 @@ namespace PluralKit.Core
|
||||
new { Hid = hid.ToLower(), System = system }
|
||||
);
|
||||
|
||||
public Task<PKMember?> GetMemberByGuid(IPKConnection conn, Guid guid) =>
|
||||
conn.QuerySingleOrDefaultAsync<PKMember?>("select * from members where uuid = @Uuid", new { Uuid = guid });
|
||||
|
||||
public Task<PKMember?> GetMemberByName(IPKConnection conn, SystemId system, string name) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where lower(name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system });
|
||||
|
||||
|
@ -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<MemberId> members)
|
||||
public async Task<PKSwitch> AddSwitch(IPKConnection conn, SystemId system, IReadOnlyCollection<MemberId> 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<MemberId> members)
|
||||
{
|
||||
@ -95,6 +97,9 @@ namespace PluralKit.Core
|
||||
new { System = system });
|
||||
}
|
||||
|
||||
public Task<PKSwitch> GetSwitchByUuid(IPKConnection conn, Guid uuid) =>
|
||||
conn.QuerySingleOrDefaultAsync<PKSwitch>("select * from switches where uuid = @Uuid", new { Uuid = uuid });
|
||||
|
||||
public async Task<int> GetSwitchCount(IPKConnection conn, SystemId system)
|
||||
{
|
||||
return await conn.QuerySingleAsync<int>("select count(*) from switches where system = @Id", new { Id = system });
|
||||
|
@ -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<PKSystem?> GetSystem(IPKConnection conn, SystemId id) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKSystem?>("select * from systems where id = @id", new { id });
|
||||
|
||||
public Task<PKSystem?> GetSystemByGuid(IPKConnection conn, Guid id) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKSystem?>("select * from systems where uuid = @id", new { id });
|
||||
|
||||
public Task<PKSystem?> GetSystemByAccount(IPKConnection conn, ulong accountId) =>
|
||||
conn.QuerySingleOrDefaultAsync<PKSystem?>(
|
||||
"select systems.* from systems, accounts where accounts.system = systems.id and accounts.uid = @Id",
|
||||
@ -38,9 +42,13 @@ namespace PluralKit.Core
|
||||
return conn.QuerySingleAsync<int>(query.ToString(), new { Id = id });
|
||||
}
|
||||
|
||||
public Task<int> GetSystemGroupCount(IPKConnection conn, SystemId id) =>
|
||||
conn.QuerySingleAsync<int>("select count(*) from groups where system = @System", new { System = id });
|
||||
|
||||
public Task<int> 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<int>(query.ToString(), new { Id = id });
|
||||
}
|
||||
public async Task<PKSystem> CreateSystem(IPKConnection conn, string? systemName = null, IPKTransaction? tx = null)
|
||||
{
|
||||
var system = await conn.QuerySingleAsync<PKSystem>(
|
||||
|
@ -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)
|
||||
|
@ -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!;
|
||||
|
@ -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; }
|
||||
|
@ -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; }
|
||||
}
|
||||
|
@ -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; }
|
||||
|
Loading…
Reference in New Issue
Block a user