fix(migrations): add ALTER TABLE fallback for media_sort_date column

When the storage.files table already exists from a manual migration,
CREATE TABLE IF NOT EXISTS is skipped entirely, so the new
media_sort_date column is never added. Add an ALTER TABLE ADD COLUMN
IF NOT EXISTS immediately after the CREATE TABLE to handle both fresh
and pre-existing databases.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jared Wolff
2026-03-10 21:17:34 -04:00
parent 7e67db8c98
commit 9ec0f0e0d6
@@ -560,6 +560,10 @@ CREATE TABLE IF NOT EXISTS storage.files (
media_sort_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Ensure media_sort_date exists on pre-existing tables (CREATE TABLE IF NOT EXISTS skips it)
ALTER TABLE storage.files ADD COLUMN IF NOT EXISTS
media_sort_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP;
-- A user cannot have two non-trashed files with the same name in the same folder
CREATE UNIQUE INDEX IF NOT EXISTS idx_files_unique_name_in_folder
ON storage.files(folder_id, name, user_id) WHERE NOT is_trashed AND folder_id IS NOT NULL;