Redmage/db/migrations/20240409222145_create_images_table.sql

43 lines
1.5 KiB
MySQL
Raw Normal View History

2024-04-09 21:49:23 +07:00
-- +goose Up
-- +goose StatementBegin
CREATE TABLE images(
id INTEGER PRIMARY KEY,
subreddit VARCHAR(255) NOT NULL COLLATE NOCASE,
device VARCHAR(250) NOT NULL COLLATE NOCASE,
2024-04-30 14:12:33 +07:00
post_title VARCHAR(255) NOT NULL,
post_name VARCHAR(255) NOT NULL,
post_url VARCHAR(255) NOT NULL,
post_created BIGINT NOT NULL DEFAULT CURRENT_TIMESTAMP,
2024-04-30 14:12:33 +07:00
post_author VARCHAR(50) NOT NULL,
post_author_url VARCHAR(255) NOT NULL,
2024-04-09 21:49:23 +07:00
image_relative_path VARCHAR(255) NOT NULL,
image_original_url VARCHAR(255) NOT NULL,
2024-04-30 14:12:33 +07:00
image_height INTEGER NOT NULL DEFAULT 0,
image_width INTEGER NOT NULL DEFAULT 0,
image_size BIGINT NOT NULL DEFAULT 0,
thumbnail_relative_path VARCHAR(255) NOT NULL DEFAULT '',
2024-04-09 22:37:26 +07:00
nsfw INTEGER NOT NULL DEFAULT 0,
created_at BIGINT DEFAULT 0 NOT NULL,
updated_at BIGINT DEFAULT 0 NOT NULL,
2024-04-30 14:12:33 +07:00
CONSTRAINT fk_image_subreddit
FOREIGN KEY (subreddit)
REFERENCES subreddits(name)
2024-04-09 22:37:26 +07:00
ON DELETE CASCADE,
2024-04-30 14:12:33 +07:00
CONSTRAINT fk_image_devices_slug
FOREIGN KEY (device)
REFERENCES devices(slug)
2024-04-09 21:49:23 +07:00
ON DELETE CASCADE
);
2024-04-30 14:12:33 +07:00
CREATE INDEX idx_subreddit_images ON images(subreddit);
CREATE INDEX idx_subreddit_device_images ON images(device, subreddit);
CREATE INDEX idx_nsfw_images ON images(nsfw);
2024-04-30 14:12:33 +07:00
CREATE INDEX idx_images_created_at_nsfw ON images(created_at DESC, nsfw);
CREATE UNIQUE INDEX idx_unique_images_per_device ON images(device, post_name);
2024-04-09 21:49:23 +07:00
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS images;
-- +goose StatementEnd