From 91a6b4de08d15fca6169133757aed7171dca19de Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Tue, 11 Aug 2020 14:31:04 +0200 Subject: [PATCH] Merge database migrations... ...in preparation of v1.0.0. This fixes #60 --- .../migrations/001_initial_setup.down.sql | 9 ++ .../migrations/001_initial_setup.up.sql | 115 ++++++++++++++++++ api/database/migrations/001_user.down.sql | 2 - api/database/migrations/001_user.up.sql | 19 --- api/database/migrations/002_photo.down.sql | 4 - api/database/migrations/002_photo.up.sql | 55 --------- .../migrations/003_site_info.down.sql | 1 - api/database/migrations/003_site_info.up.sql | 3 - api/database/migrations/004_shares.down.sql | 2 - api/database/migrations/004_shares.up.sql | 12 -- .../migrations/005_utf8_migration.down.sql | 9 -- .../migrations/005_utf8_migration.up.sql | 9 -- .../migrations/006_favorite_photos.down.sql | 2 - .../migrations/006_favorite_photos.up.sql | 2 - api/database/migrations/007_path_hash.up.sql | 43 ------- .../migrations/008_video_support.down.sql | 17 --- .../migrations/008_video_support.up.sql | 31 ----- 17 files changed, 124 insertions(+), 211 deletions(-) create mode 100644 api/database/migrations/001_initial_setup.down.sql create mode 100644 api/database/migrations/001_initial_setup.up.sql delete mode 100644 api/database/migrations/001_user.down.sql delete mode 100644 api/database/migrations/001_user.up.sql delete mode 100644 api/database/migrations/002_photo.down.sql delete mode 100644 api/database/migrations/002_photo.up.sql delete mode 100644 api/database/migrations/003_site_info.down.sql delete mode 100644 api/database/migrations/003_site_info.up.sql delete mode 100644 api/database/migrations/004_shares.down.sql delete mode 100644 api/database/migrations/004_shares.up.sql delete mode 100644 api/database/migrations/005_utf8_migration.down.sql delete mode 100644 api/database/migrations/005_utf8_migration.up.sql delete mode 100644 api/database/migrations/006_favorite_photos.down.sql delete mode 100644 api/database/migrations/006_favorite_photos.up.sql delete mode 100644 api/database/migrations/007_path_hash.up.sql delete mode 100644 api/database/migrations/008_video_support.down.sql delete mode 100644 api/database/migrations/008_video_support.up.sql diff --git a/api/database/migrations/001_initial_setup.down.sql b/api/database/migrations/001_initial_setup.down.sql new file mode 100644 index 00000000..874c6f7e --- /dev/null +++ b/api/database/migrations/001_initial_setup.down.sql @@ -0,0 +1,9 @@ +DROP TABLE IF EXISTS site_info; +DROP TABLE IF EXISTS access_token; +DROP TABLE IF EXISTS media_url; +DROP TABLE IF EXISTS share_token; +DROP TABLE IF EXISTS media; +DROP TABLE IF EXISTS video_metadata; +DROP TABLE IF EXISTS media_exif; +DROP TABLE IF EXISTS album; +DROP TABLE IF EXISTS user; \ No newline at end of file diff --git a/api/database/migrations/001_initial_setup.up.sql b/api/database/migrations/001_initial_setup.up.sql new file mode 100644 index 00000000..1ca7f484 --- /dev/null +++ b/api/database/migrations/001_initial_setup.up.sql @@ -0,0 +1,115 @@ +-- Users and authentication +CREATE TABLE IF NOT EXISTS user ( + user_id int NOT NULL AUTO_INCREMENT, + username varchar(256) NOT NULL UNIQUE, + password varchar(256), + root_path varchar(512), + admin boolean NOT NULL DEFAULT 0, + + PRIMARY KEY (user_id) +) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +CREATE TABLE IF NOT EXISTS access_token ( + token_id int NOT NULL AUTO_INCREMENT, + user_id int NOT NULL, + value char(24) NOT NULL UNIQUE, + expire timestamp NOT NULL, + + PRIMARY KEY (token_id), + FOREIGN KEY (user_id) REFERENCES user(user_id) ON DELETE CASCADE +) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +CREATE TABLE IF NOT EXISTS site_info ( + initial_setup boolean NOT NULL DEFAULT TRUE +) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- Video related +CREATE TABLE IF NOT EXISTS video_metadata ( + metadata_id int NOT NULL AUTO_INCREMENT, + + width int(6) NOT NULL, + height int(6) NOT NULL, + duration double NOT NULL, + codec varchar(128), + framerate double, + bitrate int(24), + color_profile varchar(128), + audio varchar(128), + + PRIMARY KEY (metadata_id) +) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- Media related +CREATE TABLE IF NOT EXISTS album ( + album_id int NOT NULL AUTO_INCREMENT, + title varchar(256) NOT NULL, + parent_album int, + owner_id int NOT NULL, + path varchar(1024) NOT NULL, + path_hash varchar(32) NOT NULL UNIQUE, + + PRIMARY KEY (album_id), + FOREIGN KEY (parent_album) REFERENCES album(album_id) ON DELETE CASCADE, + FOREIGN KEY (owner_id) REFERENCES user(user_id) ON DELETE CASCADE +) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +CREATE TABLE IF NOT EXISTS media_exif ( + exif_id int NOT NULL AUTO_INCREMENT, + camera varchar(256), + maker varchar(256), + lens varchar(256), + dateShot timestamp NULL, + exposure varchar(256), + aperture float, + iso int(6), + focal_length float, + flash varchar(256), + orientation int(1), + exposure_program int(1), + + PRIMARY KEY (exif_id) +) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +CREATE TABLE IF NOT EXISTS media ( + media_id int NOT NULL AUTO_INCREMENT, + title varchar(256) NOT NULL, + path varchar(1024) NOT NULL, + path_hash varchar(32) NOT NULL UNIQUE, + album_id int NOT NULL, + exif_id int, + favorite boolean DEFAULT FALSE, + media_type varchar(64) NOT NULL, + video_metadata_id int, + + PRIMARY KEY (media_id), + FOREIGN KEY (album_id) REFERENCES album(album_id) ON DELETE CASCADE, + FOREIGN KEY (exif_id) REFERENCES media_exif(exif_id) ON DELETE CASCADE, + FOREIGN KEY (video_metadata_id) REFERENCES video_metadata(metadata_id) ON DELETE CASCADE +) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +CREATE TABLE IF NOT EXISTS media_url ( + url_id int NOT NULL AUTO_INCREMENT, + media_id int NOT NULL, + media_name varchar(512) NOT NULL, + width int NOT NULL, + height int NOT NULL, + purpose varchar(64) NOT NULL, + content_type varchar(64) NOT NULL, + + PRIMARY KEY (url_id), + FOREIGN KEY (media_id) REFERENCES media(media_id) ON DELETE CASCADE +) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- Public shares +CREATE TABLE IF NOT EXISTS share_token ( + token_id int AUTO_INCREMENT, + value char(24) NOT NULL UNIQUE, + owner_id int NOT NULL, + expire timestamp NULL DEFAULT NULL, + password varchar(256), + album_id int, + media_id int, + + PRIMARY KEY (token_id) + -- CHECK (album_id IS NOT NULL OR media_id IS NOT NULL) +) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; diff --git a/api/database/migrations/001_user.down.sql b/api/database/migrations/001_user.down.sql deleted file mode 100644 index 9aae9512..00000000 --- a/api/database/migrations/001_user.down.sql +++ /dev/null @@ -1,2 +0,0 @@ -DROP TABLE IF EXISTS user; -DROP TABLE IF NOT EXISTS access_token; diff --git a/api/database/migrations/001_user.up.sql b/api/database/migrations/001_user.up.sql deleted file mode 100644 index 2eb90950..00000000 --- a/api/database/migrations/001_user.up.sql +++ /dev/null @@ -1,19 +0,0 @@ -CREATE TABLE IF NOT EXISTS user ( - user_id int NOT NULL AUTO_INCREMENT, - username varchar(256) NOT NULL UNIQUE, - password varchar(256), - root_path varchar(512), - admin boolean NOT NULL DEFAULT 0, - - PRIMARY KEY (user_id) -); - -CREATE TABLE IF NOT EXISTS access_token ( - token_id int NOT NULL AUTO_INCREMENT, - user_id int NOT NULL, - value char(24) NOT NULL UNIQUE, - expire timestamp NOT NULL, - - PRIMARY KEY (token_id), - FOREIGN KEY (user_id) REFERENCES user(user_id) ON DELETE CASCADE -); diff --git a/api/database/migrations/002_photo.down.sql b/api/database/migrations/002_photo.down.sql deleted file mode 100644 index e3995cc6..00000000 --- a/api/database/migrations/002_photo.down.sql +++ /dev/null @@ -1,4 +0,0 @@ -DROP TABLE IF EXISTS photo; -DROP TABLE IF EXISTS album; -DROP TABLE IF EXISTS photo_url; -DROP TABLE IF EXISTS photo_exif; diff --git a/api/database/migrations/002_photo.up.sql b/api/database/migrations/002_photo.up.sql deleted file mode 100644 index 805a3702..00000000 --- a/api/database/migrations/002_photo.up.sql +++ /dev/null @@ -1,55 +0,0 @@ -CREATE TABLE IF NOT EXISTS photo_exif ( - exif_id int NOT NULL AUTO_INCREMENT, - camera varchar(256), - maker varchar(256), - lens varchar(256), - dateShot timestamp NULL, - exposure varchar(256), - aperture float, - iso int(6), - focal_length float, - flash varchar(256), - orientation int(1), - exposure_program int(1), - - PRIMARY KEY (exif_id) -); - -CREATE TABLE IF NOT EXISTS album ( - album_id int NOT NULL AUTO_INCREMENT, - title varchar(256) NOT NULL, - parent_album int, - owner_id int NOT NULL, - path varchar(1024) NOT NULL, - path_hash varchar(32) NOT NULL UNIQUE, - - PRIMARY KEY (album_id), - FOREIGN KEY (parent_album) REFERENCES album(album_id) ON DELETE CASCADE, - FOREIGN KEY (owner_id) REFERENCES user(user_id) ON DELETE CASCADE -); - -CREATE TABLE IF NOT EXISTS photo ( - photo_id int NOT NULL AUTO_INCREMENT, - title varchar(256) NOT NULL, - path varchar(1024) NOT NULL, - path_hash varchar(32) NOT NULL UNIQUE, - album_id int NOT NULL, - exif_id int, - - PRIMARY KEY (photo_id), - FOREIGN KEY (album_id) REFERENCES album(album_id) ON DELETE CASCADE, - FOREIGN KEY (exif_id) REFERENCES photo_exif(exif_id) ON DELETE CASCADE -); - -CREATE TABLE IF NOT EXISTS photo_url ( - url_id int NOT NULL AUTO_INCREMENT, - photo_id int NOT NULL, - photo_name varchar(512) NOT NULL, - width int NOT NULL, - height int NOT NULL, - purpose varchar(64) NOT NULL, - content_type varchar(64) NOT NULL, - - PRIMARY KEY (url_id), - FOREIGN KEY (photo_id) REFERENCES photo(photo_id) ON DELETE CASCADE -); \ No newline at end of file diff --git a/api/database/migrations/003_site_info.down.sql b/api/database/migrations/003_site_info.down.sql deleted file mode 100644 index ea7aaf50..00000000 --- a/api/database/migrations/003_site_info.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS site_info; diff --git a/api/database/migrations/003_site_info.up.sql b/api/database/migrations/003_site_info.up.sql deleted file mode 100644 index 1f72c4d4..00000000 --- a/api/database/migrations/003_site_info.up.sql +++ /dev/null @@ -1,3 +0,0 @@ -CREATE TABLE IF NOT EXISTS site_info ( - initial_setup boolean NOT NULL DEFAULT TRUE -); diff --git a/api/database/migrations/004_shares.down.sql b/api/database/migrations/004_shares.down.sql deleted file mode 100644 index 15dc8df5..00000000 --- a/api/database/migrations/004_shares.down.sql +++ /dev/null @@ -1,2 +0,0 @@ - -DROP TABLE IF EXISTS share_token; diff --git a/api/database/migrations/004_shares.up.sql b/api/database/migrations/004_shares.up.sql deleted file mode 100644 index 687ef657..00000000 --- a/api/database/migrations/004_shares.up.sql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE TABLE IF NOT EXISTS share_token ( - token_id int AUTO_INCREMENT, - value char(24) NOT NULL UNIQUE, - owner_id int NOT NULL, - expire timestamp NULL DEFAULT NULL, - password varchar(256), - album_id int, - photo_id int, - - PRIMARY KEY (token_id) - -- CHECK (album_id IS NOT NULL OR photo_id IS NOT NULL) -); diff --git a/api/database/migrations/005_utf8_migration.down.sql b/api/database/migrations/005_utf8_migration.down.sql deleted file mode 100644 index 393e20e8..00000000 --- a/api/database/migrations/005_utf8_migration.down.sql +++ /dev/null @@ -1,9 +0,0 @@ --- Migrate all tables in database to use utf8 for better language support -ALTER TABLE access_token CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; -ALTER TABLE album CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; -ALTER TABLE photo CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; -ALTER TABLE photo_exif CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; -ALTER TABLE photo_url CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; -ALTER TABLE share_token CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; -ALTER TABLE site_info CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; -ALTER TABLE user CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; diff --git a/api/database/migrations/005_utf8_migration.up.sql b/api/database/migrations/005_utf8_migration.up.sql deleted file mode 100644 index 9d0630ff..00000000 --- a/api/database/migrations/005_utf8_migration.up.sql +++ /dev/null @@ -1,9 +0,0 @@ --- Migrate all tables in database to use utf8 for better language support -ALTER TABLE access_token CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE album CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE photo CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE photo_exif CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE photo_url CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE share_token CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE site_info CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -ALTER TABLE user CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; diff --git a/api/database/migrations/006_favorite_photos.down.sql b/api/database/migrations/006_favorite_photos.down.sql deleted file mode 100644 index 9e556e12..00000000 --- a/api/database/migrations/006_favorite_photos.down.sql +++ /dev/null @@ -1,2 +0,0 @@ --- Add favorite attribute to photos -ALTER TABLE photo DROP favorite diff --git a/api/database/migrations/006_favorite_photos.up.sql b/api/database/migrations/006_favorite_photos.up.sql deleted file mode 100644 index c1106060..00000000 --- a/api/database/migrations/006_favorite_photos.up.sql +++ /dev/null @@ -1,2 +0,0 @@ --- Add favorite attribute to photos -ALTER TABLE photo ADD favorite BOOL DEFAULT false diff --git a/api/database/migrations/007_path_hash.up.sql b/api/database/migrations/007_path_hash.up.sql deleted file mode 100644 index 552260db..00000000 --- a/api/database/migrations/007_path_hash.up.sql +++ /dev/null @@ -1,43 +0,0 @@ --- 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) AFTER path; - 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) AFTER path; - 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 diff --git a/api/database/migrations/008_video_support.down.sql b/api/database/migrations/008_video_support.down.sql deleted file mode 100644 index ea6cedc3..00000000 --- a/api/database/migrations/008_video_support.down.sql +++ /dev/null @@ -1,17 +0,0 @@ - -ALTER TABLE media RENAME TO photo; -ALTER TABLE media_url RENAME TO photo_url; -ALTER TABLE media_exif RENAME TO photo_exif; - -ALTER TABLE photo CHANGE COLUMN media_id photo_id int NOT NULL AUTO_INCREMENT; -ALTER TABLE photo_url CHANGE COLUMN media_id photo_id int NOT NULL; -ALTER TABLE photo_url CHANGE COLUMN media_name photo_name varchar(512) NOT NULL; -ALTER TABLE share_token CHANGE COLUMN media_id photo_id int; - -ALTER TABLE photo DROP COLUMN media_type; - -ALTER TABLE photo - DROP FOREIGN KEY photo_ibfk_3, - DROP COLUMN video_metadata_id; - -DROP TABLE video_metadata; \ No newline at end of file diff --git a/api/database/migrations/008_video_support.up.sql b/api/database/migrations/008_video_support.up.sql deleted file mode 100644 index 3e11f561..00000000 --- a/api/database/migrations/008_video_support.up.sql +++ /dev/null @@ -1,31 +0,0 @@ -ALTER TABLE photo RENAME TO media; -ALTER TABLE photo_url RENAME TO media_url; -ALTER TABLE photo_exif RENAME TO media_exif; - -ALTER TABLE media RENAME COLUMN photo_id TO media_id; - -ALTER TABLE media_url - RENAME COLUMN photo_id TO media_id, - RENAME COLUMN photo_name TO media_name; - -ALTER TABLE share_token RENAME COLUMN photo_id TO media_id; - -CREATE TABLE video_metadata ( - metadata_id int NOT NULL AUTO_INCREMENT, - - width int(6) NOT NULL, - height int(6) NOT NULL, - duration double NOT NULL, - codec varchar(128), - framerate double, - bitrate int(24), - color_profile varchar(128), - audio varchar(128), - - PRIMARY KEY (metadata_id) -); - -ALTER TABLE media - ADD COLUMN media_type varchar(64) NOT NULL DEFAULT "photo", - ADD COLUMN video_metadata_id int, - ADD FOREIGN KEY (video_metadata_id) REFERENCES video_metadata(metadata_id);