diff --git a/api/database/migrations/002_photo.up.sql b/api/database/migrations/002_photo.up.sql index f6b5878e..805a3702 100644 --- a/api/database/migrations/002_photo.up.sql +++ b/api/database/migrations/002_photo.up.sql @@ -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), diff --git a/api/database/migrations/007_path_hash.up.sql b/api/database/migrations/007_path_hash.up.sql new file mode 100644 index 00000000..41dc6d2c --- /dev/null +++ b/api/database/migrations/007_path_hash.up.sql @@ -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; \ No newline at end of file