2024-08-08 22:51:53 +07:00
|
|
|
-- +goose Up
|
|
|
|
-- +goose StatementBegin
|
|
|
|
CREATE TABLE images(
|
2024-08-16 14:38:57 +07:00
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
2024-08-08 22:51:53 +07:00
|
|
|
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,
|
2024-08-25 12:43:57 +07:00
|
|
|
image_height BIGINT NOT NULL DEFAULT 0,
|
|
|
|
image_width BIGINT NOT NULL DEFAULT 0,
|
2024-08-08 22:51:53 +07:00
|
|
|
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)
|
2024-08-09 15:22:54 +07:00
|
|
|
ON DELETE CASCADE,
|
|
|
|
CONSTRAINT fk_image_subreddit_cover
|
|
|
|
FOREIGN KEY (id)
|
|
|
|
REFERENCES subreddits(cover_image_id)
|
2024-08-08 22:51:53 +07:00
|
|
|
);
|
|
|
|
|
2024-08-16 14:38:57 +07:00
|
|
|
CREATE VIRTUAL TABLE images_fts5 USING fts5(
|
2024-08-24 19:21:01 +07:00
|
|
|
post_author,
|
2024-08-16 14:38:57 +07:00
|
|
|
post_title,
|
|
|
|
post_name,
|
|
|
|
image_relative_path,
|
2024-08-24 19:21:01 +07:00
|
|
|
post_url,
|
|
|
|
post_author_url,
|
2024-08-16 14:38:57 +07:00
|
|
|
content='images',
|
|
|
|
content_rowid='id'
|
|
|
|
);
|
|
|
|
|
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);
|
2024-08-09 15:22:54 +07:00
|
|
|
|
|
|
|
CREATE TRIGGER images_update_timestamp AFTER UPDATE ON images FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
UPDATE images SET updated_at = strftime('%s', 'now') WHERE id = old.id;
|
|
|
|
END;
|
|
|
|
|
|
|
|
CREATE TRIGGER images_update_subreddit_cover_on_insert AFTER INSERT ON images FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
UPDATE subreddits SET cover_image_id = new.id WHERE name = new.subreddit AND cover_image_id IS NULL;
|
|
|
|
END;
|
|
|
|
|
|
|
|
CREATE TRIGGER images_update_subreddit_cover_on_delete AFTER DELETE ON images FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
UPDATE subreddits SET cover_image_id = NULL WHERE cover_image_id = old.id;
|
|
|
|
END;
|
2024-08-16 14:38:57 +07:00
|
|
|
|
|
|
|
CREATE TRIGGER images_update_fts_insert AFTER INSERT ON images FOR EACH ROW
|
|
|
|
BEGIN
|
2024-08-24 19:21:01 +07:00
|
|
|
INSERT INTO images_fts5(
|
|
|
|
rowid, post_author, post_title, post_name, image_relative_path, post_url, post_author_url
|
|
|
|
) VALUES(
|
|
|
|
new.id, new.post_author, new.post_title, new.post_name, new.image_relative_path, new.post_url, new.post_author_url
|
|
|
|
);
|
2024-08-16 14:38:57 +07:00
|
|
|
END;
|
|
|
|
|
|
|
|
CREATE TRIGGER images_update_fts_delete AFTER DELETE ON images FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
DELETE FROM images_fts5 WHERE rowid = old.id;
|
|
|
|
END;
|
|
|
|
|
2024-08-08 22:51:53 +07:00
|
|
|
-- +goose StatementEnd
|
|
|
|
|
|
|
|
-- +goose Down
|
|
|
|
-- +goose StatementBegin
|
2024-08-16 14:38:57 +07:00
|
|
|
DROP TABLE images_fts5;
|
2024-08-08 22:51:53 +07:00
|
|
|
DROP TABLE images;
|
|
|
|
-- +goose StatementEnd
|