This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
otter-space-sdk/pkg/database/migrations/001_inital_database.sql

117 lines
2.7 KiB
SQL

-- +migrate Up
CREATE TYPE Rating AS ENUM (
'safe',
'questionable',
'explicit'
);
CREATE TYPE TagType AS ENUM (
'general',
'species',
'character',
'artist',
'lore',
'meta',
'invalid'
);
CREATE TABLE "Post"
(
id CHAR(25) UNIQUE PRIMARY KEY,
rating Rating,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL NULL
);
CREATE TABLE "Source"
(
id CHAR(25) UNIQUE PRIMARY KEY,
display_name TEXT NULL,
icon TEXT NULL,
domain TEXT NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL
);
CREATE TABLE "Tag"
(
name TEXT PRIMARY KEY,
tag_type TagType,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL
);
CREATE TABLE "User"
(
id TEXT UNIQUE PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL
);
CREATE TABLE "PostReference"
(
post_id TEXT REFERENCES "Post" (id),
source_id TEXT REFERENCES "Source" (id),
url TEXT NOT NULL UNIQUE,
full_file_url TEXT,
preview_file_url TEXT,
sample_file_url TEXT,
source_post_id TEXT,
PRIMARY KEY (post_id, source_id)
);
CREATE TABLE "TagAlias"
(
name TEXT PRIMARY KEY,
tag_id TEXT REFERENCES "Tag" (name),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE "TagGroup"
(
name TEXT PRIMARY KEY,
tag_id TEXT REFERENCES "Tag" (name),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE "UserFavorites"
(
user_id TEXT UNIQUE REFERENCES "User" (id),
post_id TEXT UNIQUE REFERENCES "Post" (id),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, post_id)
);
CREATE TABLE "UserSource"
(
user_id TEXT REFERENCES "User" (id),
source_id TEXT REFERENCES "Source" (id),
account_username TEXT,
account_id TEXT,
PRIMARY KEY (user_id, source_id),
UNIQUE (account_username, account_id)
);
CREATE TABLE "post_tags"
(
post_id TEXT REFERENCES "Post" (id),
tag_name TEXT REFERENCES "Tag" (name),
PRIMARY KEY (post_id, tag_name)
);
-- +migrate Down
DROP TYPE Rating;
DROP TYPE TagType;
DROP TABLE Post;
DROP TABLE Source;
DROP TABLE Tag;
DROP TABLE User;
DROP TABLE PostReference;
DROP TABLE TagAlias;
DROP TABLE TagGroup;
DROP TABLE UserFavorites;
DROP TABLE UserSource;