Bluemage/schemas/migrations/20240808143100_create_images_table.sql

45 lines
1.7 KiB
MySQL
Raw Normal View History

2024-08-08 22:51:53 +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,
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,
post_author VARCHAR(50) NOT NULL,
post_author_url VARCHAR(255) NOT NULL,
image_relative_path VARCHAR(255) NOT NULL,
image_original_url VARCHAR(255) NOT NULL,
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-08-09 08:33:23 +07:00
nsfw TINYINT NOT NULL DEFAULT 0,
blacklisted TINYINT NOT NULL DEFAULT 0,
2024-08-08 22:51:53 +07:00
created_at BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_at BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
CONSTRAINT fk_image_subreddit
FOREIGN KEY (subreddit)
REFERENCES subreddits(name)
ON DELETE CASCADE,
CONSTRAINT fk_image_devices_slug
FOREIGN KEY (device)
REFERENCES devices(slug)
ON DELETE CASCADE
);
2024-08-09 08:33:23 +07:00
CREATE INDEX idx_subreddit_images_blacklisted ON images(subreddit, blacklisted);
CREATE INDEX idx_subreddit_device_images_blacklisted ON images(device, subreddit, blacklisted);
CREATE INDEX idx_images_nsfw_blacklisted ON images(nsfw, blacklisted);
CREATE INDEX idx_images_created_at_nsfw_blacklisted ON images(created_at DESC, nsfw, blacklisted);
CREATE INDEX idx_images_blacklisted ON images(blacklisted);
2024-08-08 22:51:53 +07:00
CREATE UNIQUE INDEX idx_unique_images_per_device ON images(device, post_name);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE images;
-- +goose StatementEnd