From ce709957d6ef561bcdf93786a94f06a3e8b14021 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Thu, 21 Apr 2022 21:56:53 +0200 Subject: [PATCH] Search: Update files.media_id and time_index on MariaDB and SQLite #2265 --- internal/entity/file.go | 4 ++-- internal/migrate/dialect_mysql.go | 7 ++++++- internal/migrate/dialect_sqlite3.go | 9 +++++++-- internal/migrate/mysql/20220329-082000.sql | 2 +- internal/migrate/mysql/20220421-200000.sql | 1 + internal/migrate/sqlite3/20220329-082000.sql | 2 +- internal/migrate/sqlite3/20220329-092000.sql | 2 +- internal/migrate/sqlite3/20220421-200000.sql | 1 + 8 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 internal/migrate/mysql/20220421-200000.sql create mode 100644 internal/migrate/sqlite3/20220421-200000.sql diff --git a/internal/entity/file.go b/internal/entity/file.go index 35f85eb9a..5ef8c1039 100644 --- a/internal/entity/file.go +++ b/internal/entity/file.go @@ -133,7 +133,7 @@ func (m File) RegenerateIndex() { gorm.Expr(filesTable), gorm.Expr(photosTable), updateWhere).Error) Log("files", "regenerate media_id", - Db().Exec("UPDATE ? SET media_id = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN CONCAT(HEX(100000000000 - photo_id), '-', 1 + file_sidecar - file_primary, '-', file_uid) ELSE NULL END WHERE ?", + Db().Exec("UPDATE ? SET media_id = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN CONCAT((10000000000 - photo_id), '-', 1 + file_sidecar - file_primary, '-', file_uid) ELSE NULL END WHERE ?", gorm.Expr(filesTable), updateWhere).Error) Log("files", "regenerate time_index", @@ -145,7 +145,7 @@ func (m File) RegenerateIndex() { gorm.Expr(filesTable), gorm.Expr(photosTable), updateWhere).Error) Log("files", "regenerate media_id", - Db().Exec("UPDATE ? 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 ?", + Db().Exec("UPDATE ? SET media_id = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN ((10000000000 - photo_id) || '-' || (1 + file_sidecar - file_primary) || '-' || file_uid) ELSE NULL END WHERE ?", gorm.Expr(filesTable), updateWhere).Error) Log("files", "regenerate time_index", diff --git a/internal/migrate/dialect_mysql.go b/internal/migrate/dialect_mysql.go index 08915bc1e..3b963105f 100644 --- a/internal/migrate/dialect_mysql.go +++ b/internal/migrate/dialect_mysql.go @@ -60,7 +60,7 @@ var DialectMySQL = Migrations{ { ID: "20220329-082000", Dialect: "mysql", - Statements: []string{"UPDATE files SET media_id = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN CONCAT(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 CONCAT((10000000000 - photo_id), '-', 1 + file_sidecar - file_primary, '-', file_uid) END;"}, }, { ID: "20220329-090000", @@ -77,4 +77,9 @@ var DialectMySQL = Migrations{ Dialect: "mysql", Statements: []string{"UPDATE files SET time_index = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN CONCAT(100000000000000 - CAST(photo_taken_at AS UNSIGNED), '-', media_id) END;"}, }, + { + ID: "20220421-200000", + Dialect: "mysql", + Statements: []string{"CREATE OR REPLACE INDEX idx_files_missing_root ON files (file_missing, file_root);"}, + }, } diff --git a/internal/migrate/dialect_sqlite3.go b/internal/migrate/dialect_sqlite3.go index 9b5f4b525..439ea5ba7 100644 --- a/internal/migrate/dialect_sqlite3.go +++ b/internal/migrate/dialect_sqlite3.go @@ -40,7 +40,7 @@ var DialectSQLite3 = Migrations{ { ID: "20220329-082000", Dialect: "sqlite3", - Statements: []string{"UPDATE files SET media_id = CASE WHEN file_uid <> '' AND photo_id > 0 AND 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;"}, + Statements: []string{"UPDATE files SET media_id = CASE WHEN photo_id IS NOT NULL AND file_missing = 0 AND deleted_at IS NULL THEN ((10000000000 - photo_id) || '-' || (1 + file_sidecar - file_primary) || '-' || file_uid) END WHERE 1;"}, }, { ID: "20220329-091000", @@ -50,6 +50,11 @@ var DialectSQLite3 = Migrations{ { ID: "20220329-092000", Dialect: "sqlite3", - Statements: []string{"UPDATE files SET time_index = CASE WHEN media_id IS NOT NULL AND photo_taken_at IS NOT NULL THEN (((100000000000000 - strftime('%Y%m%d%H%M%S', photo_taken_at)) || '-' || media_id) ELSE NULL END WHERE photo_id IS NOT NULL;"}, + Statements: []string{"UPDATE files SET time_index = CASE WHEN media_id IS NOT NULL AND photo_taken_at IS NOT NULL THEN ((100000000000000 - strftime('%Y%m%d%H%M%S', photo_taken_at)) || '-' || media_id) ELSE NULL END WHERE photo_id IS NOT NULL;"}, + }, + { + ID: "20220421-200000", + Dialect: "sqlite3", + Statements: []string{"CREATE INDEX IF NOT EXISTS idx_files_missing_root ON files (file_missing, file_root);"}, }, } diff --git a/internal/migrate/mysql/20220329-082000.sql b/internal/migrate/mysql/20220329-082000.sql index e5f0e1878..e0ffd5dd4 100644 --- a/internal/migrate/mysql/20220329-082000.sql +++ b/internal/migrate/mysql/20220329-082000.sql @@ -1 +1 @@ -UPDATE files SET media_id = CASE WHEN file_missing = 0 AND deleted_at IS NULL THEN CONCAT(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 CONCAT((10000000000 - photo_id), '-', 1 + file_sidecar - file_primary, '-', file_uid) END; \ No newline at end of file diff --git a/internal/migrate/mysql/20220421-200000.sql b/internal/migrate/mysql/20220421-200000.sql new file mode 100644 index 000000000..0236bb38e --- /dev/null +++ b/internal/migrate/mysql/20220421-200000.sql @@ -0,0 +1 @@ +CREATE OR REPLACE INDEX idx_files_missing_root ON files (file_missing, file_root); \ No newline at end of file diff --git a/internal/migrate/sqlite3/20220329-082000.sql b/internal/migrate/sqlite3/20220329-082000.sql index 46915f5b2..cb20004bf 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_uid <> '' AND photo_id > 0 AND 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 +UPDATE files SET media_id = CASE WHEN photo_id IS NOT NULL AND file_missing = 0 AND deleted_at IS NULL THEN ((10000000000 - photo_id) || '-' || (1 + file_sidecar - file_primary) || '-' || file_uid) END WHERE 1; \ No newline at end of file diff --git a/internal/migrate/sqlite3/20220329-092000.sql b/internal/migrate/sqlite3/20220329-092000.sql index 918b4e906..ab4b21656 100644 --- a/internal/migrate/sqlite3/20220329-092000.sql +++ b/internal/migrate/sqlite3/20220329-092000.sql @@ -1 +1 @@ -UPDATE files SET time_index = CASE WHEN media_id IS NOT NULL AND photo_taken_at IS NOT NULL THEN (((100000000000000 - strftime('%Y%m%d%H%M%S', photo_taken_at)) || '-' || media_id) ELSE NULL END WHERE photo_id IS NOT NULL; \ No newline at end of file +UPDATE files SET time_index = CASE WHEN media_id IS NOT NULL AND photo_taken_at IS NOT NULL THEN ((100000000000000 - strftime('%Y%m%d%H%M%S', photo_taken_at)) || '-' || media_id) ELSE NULL END WHERE photo_id IS NOT NULL; \ No newline at end of file diff --git a/internal/migrate/sqlite3/20220421-200000.sql b/internal/migrate/sqlite3/20220421-200000.sql new file mode 100644 index 000000000..2c59e0e47 --- /dev/null +++ b/internal/migrate/sqlite3/20220421-200000.sql @@ -0,0 +1 @@ +CREATE INDEX IF NOT EXISTS idx_files_missing_root ON files (file_missing, file_root); \ No newline at end of file