From 0d5af690cc1668d13d83efe42b004474fc93c28f Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Fri, 1 Apr 2022 16:49:26 +0200 Subject: [PATCH] Migrations: Improve SQLite schema migration statements #2216 --- internal/migrate/dialect_sqlite3.go | 10 +++++----- internal/migrate/sqlite3/20220329-050000.sql | 2 +- internal/migrate/sqlite3/20220329-061000.sql | 1 - internal/migrate/sqlite3/20220329-071000.sql | 2 +- internal/migrate/sqlite3/20220329-082000.sql | 2 +- internal/migrate/sqlite3/20220329-092000.sql | 2 +- 6 files changed, 9 insertions(+), 10 deletions(-) diff --git a/internal/migrate/dialect_sqlite3.go b/internal/migrate/dialect_sqlite3.go index 594f0c0fa..f1376b19c 100644 --- a/internal/migrate/dialect_sqlite3.go +++ b/internal/migrate/dialect_sqlite3.go @@ -20,17 +20,17 @@ var DialectSQLite3 = Migrations{ { ID: "20220329-050000", Dialect: "sqlite3", - Statements: []string{"CREATE INDEX idx_albums_album_filter ON albums (album_filter);"}, + Statements: []string{"CREATE INDEX IF NOT EXISTS idx_albums_album_filter ON albums (album_filter);"}, }, { ID: "20220329-061000", Dialect: "sqlite3", - Statements: []string{"DROP INDEX IF EXISTS idx_files_photo_id;", "CREATE INDEX IF NOT EXISTS idx_files_photo_id ON files (photo_id, file_primary);"}, + Statements: []string{"CREATE INDEX IF NOT EXISTS idx_files_photo_id ON files (photo_id, file_primary);"}, }, { ID: "20220329-071000", Dialect: "sqlite3", - Statements: []string{"UPDATE files SET photo_taken_at = (SELECT photos.taken_at_local FROM photos WHERE photos.id = files.photo_id) WHERE 1;"}, + Statements: []string{"UPDATE files SET photo_taken_at = (SELECT p.taken_at_local FROM photos p WHERE p.id = photo_id) WHERE photo_id IS NOT NULL;"}, }, { ID: "20220329-081000", @@ -40,7 +40,7 @@ var DialectSQLite3 = Migrations{ { ID: "20220329-082000", Dialect: "sqlite3", - Statements: []string{"UPDATE files SET media_id = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN (HEX(100000000000 - photo_id) || '-' || 1 + file_sidecar - file_primary || '-' || file_uid) END;"}, + Statements: []string{"UPDATE files SET media_id = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN (HEX(100000000000 - photo_id) || '-' || (1 + file_sidecar - file_primary) || '-' || file_uid) ELSE NULL END WHERE photo_id IS NOT NULL;"}, }, { ID: "20220329-091000", @@ -50,6 +50,6 @@ var DialectSQLite3 = Migrations{ { ID: "20220329-092000", Dialect: "sqlite3", - Statements: []string{"UPDATE files SET time_index = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN (100000000000000 - CAST(photo_taken_at AS UNSIGNED) || '-' || media_id) END;"}, + Statements: []string{"UPDATE files SET time_index = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN ((100000000000000 - CAST(photo_taken_at AS UNSIGNED)) || '-' || media_id) ELSE NULL END WHERE media_id IS NOT NULL;"}, }, } diff --git a/internal/migrate/sqlite3/20220329-050000.sql b/internal/migrate/sqlite3/20220329-050000.sql index 19a59394c..8bb3f69d3 100644 --- a/internal/migrate/sqlite3/20220329-050000.sql +++ b/internal/migrate/sqlite3/20220329-050000.sql @@ -1 +1 @@ -CREATE INDEX idx_albums_album_filter ON albums (album_filter); \ No newline at end of file +CREATE INDEX IF NOT EXISTS idx_albums_album_filter ON albums (album_filter); \ No newline at end of file diff --git a/internal/migrate/sqlite3/20220329-061000.sql b/internal/migrate/sqlite3/20220329-061000.sql index 5c3099f37..27d2b5609 100644 --- a/internal/migrate/sqlite3/20220329-061000.sql +++ b/internal/migrate/sqlite3/20220329-061000.sql @@ -1,2 +1 @@ -DROP INDEX IF EXISTS idx_files_photo_id; CREATE INDEX IF NOT EXISTS idx_files_photo_id ON files (photo_id, file_primary); \ No newline at end of file diff --git a/internal/migrate/sqlite3/20220329-071000.sql b/internal/migrate/sqlite3/20220329-071000.sql index 5f7a77cb2..bf2a234bd 100644 --- a/internal/migrate/sqlite3/20220329-071000.sql +++ b/internal/migrate/sqlite3/20220329-071000.sql @@ -1 +1 @@ -UPDATE files SET photo_taken_at = (SELECT photos.taken_at_local FROM photos WHERE photos.id = files.photo_id) WHERE 1; \ No newline at end of file +UPDATE files SET photo_taken_at = (SELECT p.taken_at_local FROM photos p WHERE p.id = photo_id) WHERE photo_id IS NOT NULL; \ No newline at end of file diff --git a/internal/migrate/sqlite3/20220329-082000.sql b/internal/migrate/sqlite3/20220329-082000.sql index 1d3612551..cf9e06d25 100644 --- a/internal/migrate/sqlite3/20220329-082000.sql +++ b/internal/migrate/sqlite3/20220329-082000.sql @@ -1 +1 @@ -UPDATE files SET media_id = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN (HEX(100000000000 - photo_id) || '-' || 1 + file_sidecar - file_primary || '-' || file_uid) END; \ No newline at end of file +UPDATE files SET media_id = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN (HEX(100000000000 - photo_id) || '-' || (1 + file_sidecar - file_primary) || '-' || file_uid) ELSE NULL END WHERE photo_id IS NOT NULL; \ No newline at end of file diff --git a/internal/migrate/sqlite3/20220329-092000.sql b/internal/migrate/sqlite3/20220329-092000.sql index 1d043c933..7a16ed6ec 100644 --- a/internal/migrate/sqlite3/20220329-092000.sql +++ b/internal/migrate/sqlite3/20220329-092000.sql @@ -1 +1 @@ -UPDATE files SET time_index = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN (100000000000000 - CAST(photo_taken_at AS UNSIGNED) || '-' || media_id) END; \ No newline at end of file +UPDATE files SET time_index = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN ((100000000000000 - CAST(photo_taken_at AS UNSIGNED)) || '-' || media_id) ELSE NULL END WHERE media_id IS NOT NULL; \ No newline at end of file