This includes the patches to get from the initial version of the database to its current state - for the purpose of documentation. Beware, if you already have an existing database, running this code will likely result in an error and/or lead to some minor data loss. You need to follow these steps to fix your DB: 1. delete all *.sql files from unwind/sql/ 2. remove the .disabled suffix from unwind/sql/00000001-fix-db.sql.disabled 3. start the application once, this will fix up the database 4. revert all changes to unwind/sql/ – or leave it, it doesn't matter.
46 lines
1 KiB
SQL
46 lines
1 KiB
SQL
-- only set original_title if it differs from title,
|
|
-- and normalize media_type with an extra table.
|
|
|
|
CREATE TABLE mediatypes (
|
|
id INTEGER PRIMARY KEY NOT NULL,
|
|
name TEXT NOT NULL UNIQUE
|
|
);;
|
|
|
|
INSERT INTO mediatypes (name)
|
|
SELECT DISTINCT media_type
|
|
FROM movies
|
|
WHERE true;;
|
|
|
|
CREATE TABLE _migrate_movies (
|
|
id TEXT PRIMARY KEY NOT NULL,
|
|
title TEXT NOT NULL,
|
|
original_title TEXT,
|
|
release_year INTEGER NOT NULL,
|
|
media_type_id INTEGER NOT NULL,
|
|
imdb_id TEXT NOT NULL UNIQUE,
|
|
score INTEGER,
|
|
runtime INTEGER,
|
|
genres TEXT NOT NULL,
|
|
updated TEXT NOT NULL,
|
|
FOREIGN KEY(media_type_id) REFERENCES mediatypes(id)
|
|
);;
|
|
|
|
INSERT INTO _migrate_movies
|
|
SELECT
|
|
id,
|
|
title,
|
|
(CASE WHEN original_title=title THEN NULL ELSE original_title END),
|
|
release_year,
|
|
(SELECT id FROM mediatypes WHERE name=media_type) AS media_type_id,
|
|
imdb_id,
|
|
score,
|
|
runtime,
|
|
genres,
|
|
updated
|
|
FROM movies
|
|
WHERE true;;
|
|
|
|
DROP TABLE movies;;
|
|
|
|
ALTER TABLE _migrate_movies
|
|
RENAME TO movies;;
|