Bluemage/schemas/migrations/20240804155710_create_subreddits_table.sql

37 lines
1.2 KiB
MySQL
Raw Normal View History

2024-08-04 23:16:05 +07:00
-- +goose Up
-- +goose StatementBegin
CREATE TABLE subreddits (
name VARCHAR(30) NOT NULL PRIMARY KEY COLLATE NOCASE,
2024-08-05 23:06:32 +07:00
disable_scheduler TINYINT NOT NULL DEFAULT 0,
2024-08-04 23:16:05 +07:00
"type" VARCHAR(5) NOT NULL DEFAULT 'r',
schedule VARCHAR(20) NOT NULL DEFAULT '@daily',
countback INT NOT NULL DEFAULT 300,
image_cover VARCHAR(255) NOT NULL DEFAULT '',
2024-08-05 23:06:32 +07:00
created_at BIGINT DEFAULT (strftime('%s', 'now')) NOT NULL,
updated_at BIGINT DEFAULT (strftime('%s', 'now')) NOT NULL
2024-08-04 23:16:05 +07:00
);
CREATE UNIQUE INDEX idx_subreddits_name ON subreddits(name);
CREATE TRIGGER subreddits_update_timestamp AFTER UPDATE ON subreddits FOR EACH ROW
BEGIN
2024-08-05 23:06:32 +07:00
UPDATE subreddits SET updated_at = (strftime('%s', 'now')) WHERE name = old.name;
2024-08-04 23:16:05 +07:00
END;
CREATE TRIGGER subreddits_insert_create_total_image_metrics AFTER INSERT on subreddits FOR EACH ROW
BEGIN
2024-08-05 23:06:32 +07:00
INSERT INTO metrics (name, value)
VALUES (CONCAT('subreddits.', NEW.name, '.total_images'), 0);
2024-08-04 23:16:05 +07:00
END;
CREATE TRIGGER subreddits_delete_total_image_metrics AFTER DELETE on subreddits FOR EACH ROW
BEGIN
DELETE FROM metrics WHERE name = CONCAT('subreddits.', OLD.name, '.total_images');
END;
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE subreddits;
-- +goose StatementEnd