Files
reminder-bot/migrations/20261011120000_image_uploads.sql
T
2026-10-28 19:56:12 +00:00

64 lines
3.2 KiB
SQL

CREATE TABLE image_uploads (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-- Discord user ID from the authenticated session, including guild-only users.
uploader BIGINT UNSIGNED NOT NULL,
path VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
orphaned_at DATETIME DEFAULT NULL,
UNIQUE INDEX image_uploads_path (path),
INDEX image_uploads_uploader (uploader),
INDEX image_uploads_orphaned_at (orphaned_at)
);
-- Keep column additions separate so existing rows do not need to be rebuilt.
ALTER TABLE reminders
ADD COLUMN avatar_id BIGINT UNSIGNED DEFAULT NULL,
ADD COLUMN embed_image_id BIGINT UNSIGNED DEFAULT NULL,
ADD COLUMN embed_thumbnail_id BIGINT UNSIGNED DEFAULT NULL,
ADD COLUMN embed_author_id BIGINT UNSIGNED DEFAULT NULL,
ADD COLUMN embed_footer_id BIGINT UNSIGNED DEFAULT NULL,
ALGORITHM=INSTANT;
ALTER TABLE reminder_template
ADD COLUMN avatar_id BIGINT UNSIGNED DEFAULT NULL,
ADD COLUMN embed_image_id BIGINT UNSIGNED DEFAULT NULL,
ADD COLUMN embed_thumbnail_id BIGINT UNSIGNED DEFAULT NULL,
ADD COLUMN embed_author_id BIGINT UNSIGNED DEFAULT NULL,
ADD COLUMN embed_footer_id BIGINT UNSIGNED DEFAULT NULL,
ALGORITHM=INSTANT;
-- Run before the application populates the new columns: existing values are NULL.
-- Disabling checks on this connection permits online foreign key creation.
SET @image_uploads_foreign_key_checks = @@SESSION.foreign_key_checks;
SET SESSION foreign_key_checks = 0;
ALTER TABLE reminders
ADD INDEX reminders_avatar_id (avatar_id),
ADD FOREIGN KEY (avatar_id) REFERENCES image_uploads (id) ON DELETE RESTRICT,
ADD INDEX reminders_embed_image_id (embed_image_id),
ADD FOREIGN KEY (embed_image_id) REFERENCES image_uploads (id) ON DELETE RESTRICT,
ADD INDEX reminders_embed_thumbnail_id (embed_thumbnail_id),
ADD FOREIGN KEY (embed_thumbnail_id) REFERENCES image_uploads (id) ON DELETE RESTRICT,
ADD INDEX reminders_embed_author_id (embed_author_id),
ADD FOREIGN KEY (embed_author_id) REFERENCES image_uploads (id) ON DELETE RESTRICT,
ADD INDEX reminders_embed_footer_id (embed_footer_id),
ADD FOREIGN KEY (embed_footer_id) REFERENCES image_uploads (id) ON DELETE RESTRICT,
ALGORITHM=INPLACE,
LOCK=NONE;
ALTER TABLE reminder_template
ADD INDEX reminder_template_avatar_id (avatar_id),
ADD FOREIGN KEY (avatar_id) REFERENCES image_uploads (id) ON DELETE RESTRICT,
ADD INDEX reminder_template_embed_image_id (embed_image_id),
ADD FOREIGN KEY (embed_image_id) REFERENCES image_uploads (id) ON DELETE RESTRICT,
ADD INDEX reminder_template_embed_thumbnail_id (embed_thumbnail_id),
ADD FOREIGN KEY (embed_thumbnail_id) REFERENCES image_uploads (id) ON DELETE RESTRICT,
ADD INDEX reminder_template_embed_author_id (embed_author_id),
ADD FOREIGN KEY (embed_author_id) REFERENCES image_uploads (id) ON DELETE RESTRICT,
ADD INDEX reminder_template_embed_footer_id (embed_footer_id),
ADD FOREIGN KEY (embed_footer_id) REFERENCES image_uploads (id) ON DELETE RESTRICT,
ALGORITHM=INPLACE,
LOCK=NONE;
SET SESSION foreign_key_checks = @image_uploads_foreign_key_checks;