synthwave/migrations/0000_init.sql

64 lines
1.8 KiB
MySQL
Raw Permalink Normal View History

2024-10-04 17:39:51 +00:00
CREATE TYPE user_role AS ENUM ('regular', 'admin');
2024-10-04 10:00:33 +00:00
2024-10-04 17:39:51 +00:00
CREATE TABLE IF NOT EXISTS users (
username VARCHAR(255) NOT NULL PRIMARY KEY,
"password" text NOT NULL,
user_role user_role NOT NULL DEFAULT 'regular'
2024-10-04 10:00:33 +00:00
);
CREATE TABLE IF NOT EXISTS user_session (
id UUID NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
token text NOT NULL,
2024-10-04 17:39:51 +00:00
"user" varchar(255) NOT NULL,
FOREIGN KEY("user") REFERENCES users(username)
2024-10-04 10:00:33 +00:00
);
CREATE TABLE IF NOT EXISTS artist (
id UUID NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL
);
CREATE TABLE IF NOT EXISTS album (
id UUID NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
title text NOT NULL,
2024-10-04 17:39:51 +00:00
artist UUID,
2024-10-04 10:00:33 +00:00
FOREIGN KEY(artist) REFERENCES artist(id)
);
CREATE TABLE IF NOT EXISTS track (
id UUID NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
path text NOT NULL,
title text,
2024-10-04 17:39:51 +00:00
date_added timestamptz NOT NULL DEFAULT current_timestamp,
2024-10-04 10:00:33 +00:00
album UUID,
artist UUID,
meta jsonb,
FOREIGN KEY(album) REFERENCES album(id),
FOREIGN KEY(artist) REFERENCES artist(id)
);
2024-10-04 17:39:51 +00:00
CREATE TYPE event_kind AS ENUM ('play', 'played', 'stop');
2024-10-04 10:00:33 +00:00
CREATE TABLE IF NOT EXISTS events (
2024-10-04 17:39:51 +00:00
id UUID NOT NULL DEFAULT gen_random_uuid(),
time timestamptz NOT NULL DEFAULT current_timestamp,
kind event_kind NOT NULL,
"user" VARCHAR(255) NOT NULL,
2024-10-04 10:00:33 +00:00
track UUID NOT NULL,
2024-10-04 17:39:51 +00:00
FOREIGN KEY("user") REFERENCES users(username),
2024-10-04 10:00:33 +00:00
FOREIGN KEY(track) REFERENCES track(id)
);
SELECT create_hypertable('events', by_range('time'));
2024-10-04 12:38:35 +00:00
2024-10-04 17:39:51 +00:00
CREATE TYPE visibility AS ENUM ('public', 'private');
2024-10-04 12:38:35 +00:00
CREATE TABLE IF NOT EXISTS playlist (
id UUID NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
owner VARCHAR(255) NOT NULL,
title text NOT NULL,
2024-10-04 17:39:51 +00:00
visibility visibility NOT NULL DEFAULT 'private',
tracks UUID[] NOT NULL DEFAULT '{}',
FOREIGN KEY(owner) REFERENCES users(username)
2024-10-04 12:38:35 +00:00
);