-- migrate:up

create table "users" (
    "id" serial primary key,
    "email" varchar(255) unique,
    "pendingEmail" varchar(255) unique,
    "emailVerificationSecret" varchar(255) unique,
    "registered" timestamp not null,
    "passwordCrypt" bytea not null,
    "permissions" text not null,
    "accepted" timestamp,
    "rejected" timestamp,
    "seceded" timestamp,
    "toBeDeleted" timestamp,
    "memberData" jsonb not null
);

create index "users_memberData_index" on "users" using gin ("memberData");

create table "keys" (
    "id" serial primary key,
    "uid" integer not null references "users" on delete cascade,
    "data" bytea not null,
    "expires" timestamp,
    "uploaded" timestamp not null,
    "comment" text not null,
    "isPrimaryEncryptionKey" bool not null
);

create unique index "keys_isPrimaryEncryptionKey_constraint" on "keys" ("uid")
where "isPrimaryEncryptionKey";

create index "keys_uid_index" on "keys" ("uid");

create table "tokens" (
    "id" serial primary key,
    "uid" integer not null references "users" on delete cascade,
    "name" text,
    "data" text unique not null,
    "comment" text not null,
    "issued" timestamp not null,
    "expires" timestamp,
    "permissions" text,
    unique ("name", "uid")
);

create index "tokens_data_index" on "tokens" ("data");

-- migrate:down

drop table "tokens";
drop table "keys";
drop table "users";
