recommender/internal/migrations/1_setup.sql

147 lines
4.7 KiB
MySQL
Raw Normal View History

2024-11-06 10:47:56 +00:00
-- +goose Up
2024-11-07 10:09:13 +00:00
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)
);
2024-11-06 10:47:56 +00:00
2024-11-07 10:09:13 +00:00
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
);
2024-11-09 16:11:00 +00:00
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
);
2024-11-07 10:09:13 +00:00
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`
(
2024-11-09 19:49:53 +00:00
id serial NOT NULL,
name varchar(255) NOT NULL,
vectorized bool DEFAULT FALSE,
# application_id bigint unsigned NOT NULL,
2024-11-07 10:09:13 +00:00
primary key (id),
2024-11-09 19:49:53 +00:00
index (vectorized)
# foreign key (application_id) references applications (id) on delete cascade
2024-11-07 10:09:13 +00:00
);
2024-11-07 18:25:15 +00:00
CREATE TABLE `tag_mappings`
2024-11-07 10:09:13 +00:00
(
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,
2024-11-09 19:49:53 +00:00
vectorized bool DEFAULT FALSE,
2024-11-07 18:25:15 +00:00
category_id bigint unsigned,
2024-11-07 10:09:13 +00:00
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp DEFAULT CURRENT_TIMESTAMP,
2024-11-09 19:49:53 +00:00
index (target_id, processed, vectorized, application_id),
2024-11-07 10:09:13 +00:00
primary key (id),
2024-11-07 18:25:15 +00:00
foreign key (application_id) references applications (id) on delete cascade,
foreign key (category_id) references categories (id)
2024-11-07 10:09:13 +00:00
);
2024-11-06 10:47:56 +00:00
2024-11-07 10:09:13 +00:00
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`
(
2024-11-09 16:11:00 +00:00
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),
2024-11-07 10:09:13 +00:00
index (score, application_id),
foreign key (tag_id) references tags (id) on delete cascade,
2024-11-09 16:11:00 +00:00
foreign key (application_id) references applications (id) on delete cascade,
foreign key (external_user_id) references external_users (id) on delete cascade
2024-11-07 10:09:13 +00:00
);
-- user likes
CREATE TABLE `user_likes`
(
2024-11-09 16:11:00 +00:00
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),
2024-11-07 10:09:13 +00:00
index (type, application_id),
foreign key (post_id) references posts (id) on delete cascade,
2024-11-09 16:11:00 +00:00
foreign key (application_id) references applications (id) on delete cascade,
foreign key (external_user_id) references external_users (id) on delete cascade
2024-11-07 10:09:13 +00:00
);
-- +goose Down
2024-11-07 18:25:15 +00:00
DROP TABLE IF EXISTS `tag_mappings`;
2024-11-07 10:09:13 +00:00
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`;
2024-11-09 19:49:53 +00:00
DROP TABLE IF EXISTS `external_users`;
2024-11-07 10:09:13 +00:00
DROP TABLE IF EXISTS `application_tokens`;
DROP TABLE IF EXISTS `applications`;