Update path hash database migrations

This commit is contained in:
viktorstrate
2020-06-30 15:35:45 +02:00
parent 642afe4966
commit f2bcdf883f
2 changed files with 44 additions and 1 deletions

View File

@@ -20,7 +20,7 @@ CREATE TABLE IF NOT EXISTS album (
title varchar(256) NOT NULL,
parent_album int,
owner_id int NOT NULL,
path varchar(512) NOT NULL,
path varchar(1024) NOT NULL,
path_hash varchar(32) NOT NULL UNIQUE,
PRIMARY KEY (album_id),

View File

@@ -0,0 +1,43 @@
-- Update database to hash indexed paths
CREATE PROCEDURE MigratePathHashIfNeeded()
BEGIN
-- Add path hash for photo table if it doesn't exist
IF NOT EXISTS( SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'photo'
AND table_schema = DATABASE()
AND column_name = 'path_hash') THEN
-- Remove unique index from photo.path
ALTER TABLE photo DROP INDEX path;
-- Add path_hash and set it to the md5 hash based of the path attribute
ALTER TABLE photo ADD path_hash varchar(32);
UPDATE photo p SET path_hash = md5(p.path);
ALTER TABLE photo MODIFY path_hash varchar(32) NOT NULL UNIQUE;
END IF;
-- Add path hash for album table if it doesn't exist
IF NOT EXISTS( SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'album'
AND table_schema = DATABASE()
AND column_name = 'path_hash') THEN
-- Remove unique index from album.path
ALTER TABLE album DROP INDEX path;
-- Add path_hash and set it to the md5 hash based of the path attribute
ALTER TABLE album ADD path_hash varchar(32);
UPDATE album a SET path_hash = md5(a.path);
ALTER TABLE album MODIFY path_hash varchar(32) NOT NULL UNIQUE;
END IF;
END; -- MigratePathHashIfNeeded procedure end
CALL MigratePathHashIfNeeded();
DROP PROCEDURE MigratePathHashIfNeeded;