-- 2023-04-04: Create initial tables

-- +migrate Up

-- User accounts
-- Many fields in this table are only relevant for local users, but all blogs are associated with an account
-- to make the database schema simpler.
create table accounts (
    id       text primary key,
    username text not null,
    domain   text, -- if null, is a local account
    email    text,
    password bytea,

    unique (username, domain)
);

-- Blogs
-- These are the AP actors.
create table blogs (
    id     text primary key,
    name   text not null,
    domain text, -- if null, is a local blog
    bio    text not null,

    account_id text not null references accounts (id) on delete cascade,

    unique (name, domain)
);

create type post_visibility as enum ('public', 'unlisted', 'followers', 'direct');

create table posts (
    id         text primary key,
    blog_id    text not null references blogs (id) on delete cascade,
    content    text,
    source     text,
    visibility post_visibility not null
);

-- +migrate Down
drop table accounts;
drop table blogs;
drop table posts;
