recommender/internal/migrations/1_setup.sql
2024-11-10 00:11:00 +08:00

144 lines
4.6 KiB
SQL

-- +goose Up
CREATE TABLE `applications`
(
id serial NOT NULL,
name varchar(255) NOT NULL,
user_id varchar(255) NOT NULL,
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp DEFAULT CURRENT_TIMESTAMP,
primary key (id),
index (user_id)
);
CREATE TABLE `application_tokens`
(
id serial NOT NULL,
token varchar(255) NOT NULL,
application_id bigint unsigned NOT NULL,
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp DEFAULT CURRENT_TIMESTAMP,
primary key (id),
index (token),
foreign key (application_id) references applications (id) on delete cascade
);
CREATE TABLE `external_users`
(
id serial NOT NULL,
name varchar(255) DEFAULT NULL,
email varchar(255) DEFAULT NULL,
external_id varchar(255) NOT NULL,
summary varchar(255) DEFAULT NULL,
application_id bigint unsigned NOT NULL,
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp DEFAULT CURRENT_TIMESTAMP,
primary key (id),
index (external_id),
foreign key (application_id) references applications (id) on delete cascade
);
CREATE TABLE `categories`
(
id serial NOT NULL,
name varchar(255) NOT NULL,
application_id bigint unsigned NOT NULL,
primary key (id),
foreign key (application_id) references applications (id) on delete cascade
);
CREATE TABLE `tags`
(
id serial NOT NULL,
name varchar(255) NOT NULL,
application_id bigint unsigned NOT NULL,
primary key (id),
foreign key (application_id) references applications (id) on delete cascade
);
CREATE TABLE `tag_mappings`
(
id serial NOT NULL,
tag_id bigint unsigned NOT NULL,
name varchar(255) NOT NULL,
application_id bigint unsigned NOT NULL,
primary key (id),
foreign key (tag_id) references tags (id)
on delete cascade,
foreign key (application_id) references applications (id) on delete cascade,
index (tag_id, name, application_id)
);
CREATE TABLE `posts`
(
id serial NOT NULL,
target_id VARCHAR(255) NOT NULL,
title varchar(255) NOT NULL,
content LONGTEXT NOT NULL,
application_id bigint unsigned NOT NULL,
processed BOOLEAN DEFAULT FALSE,
category_id bigint unsigned,
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp DEFAULT CURRENT_TIMESTAMP,
index (target_id, processed, application_id),
primary key (id),
foreign key (application_id) references applications (id) on delete cascade,
foreign key (category_id) references categories (id)
);
CREATE TABLE `post_tags`
(
id serial NOT NULL,
post_id bigint unsigned NOT NULL,
tag_id bigint unsigned NOT NULL,
primary key (id),
foreign key (post_id) references posts (id)
on delete cascade,
foreign key (tag_id) references tags (id)
on delete cascade,
index (post_id, tag_id)
);
-- user tag scores
CREATE TABLE `user_tag_scores`
(
external_user_id bigint unsigned NOT NULL,
tag_id bigint unsigned NOT NULL,
score int NOT NULL,
application_id bigint unsigned NOT NULL,
primary key (external_user_id, tag_id),
index (score, application_id),
foreign key (tag_id) references tags (id) on delete cascade,
foreign key (application_id) references applications (id) on delete cascade,
foreign key (external_user_id) references external_users (id) on delete cascade
);
-- user likes
CREATE TABLE `user_likes`
(
external_user_id bigint unsigned NOT NULL,
post_id bigint unsigned NOT NULL,
type enum ('like', 'dislike') NOT NULL,
application_id bigint unsigned NOT NULL,
primary key (external_user_id, post_id),
index (type, application_id),
foreign key (post_id) references posts (id) on delete cascade,
foreign key (application_id) references applications (id) on delete cascade,
foreign key (external_user_id) references external_users (id) on delete cascade
);
-- +goose Down
DROP TABLE IF EXISTS `tag_mappings`;
DROP TABLE IF EXISTS `user_tag_scores`;
DROP TABLE IF EXISTS `post_tags`;
DROP TABLE IF EXISTS `user_likes`;
DROP TABLE IF EXISTS `posts`;
DROP TABLE IF EXISTS `tags`;
DROP TABLE IF EXISTS `categories`;
DROP TABLE IF EXISTS `application_tokens`;
DROP TABLE IF EXISTS `applications`;
DROP TABLE IF EXISTS `external_users`;