Switch to new sea orm migrations
Signed-off-by: trivernis <trivernis@protonmail.com>feature/rust-migrations
parent
cab349bafb
commit
f563dff064
@ -1,81 +0,0 @@
|
||||
CREATE TABLE storage_locations
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name VARCHAR(128) UNIQUE NOT NULL,
|
||||
path VARCHAR(255) NOT NULL
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE hashes
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX hashes_value_index ON hashes (value);
|
||||
|
||||
|
||||
CREATE TABLE files
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
type INTEGER NOT NULL DEFAULT 0,
|
||||
name VARCHAR(128),
|
||||
comment VARCHAR(1024),
|
||||
storage_id INTEGER NOT NULL,
|
||||
hash_id INTEGER NOT NULL,
|
||||
import_time DATETIME NOT NULL,
|
||||
creation_time DATETIME NOT NULL,
|
||||
change_time DATETIME NOT NULL,
|
||||
FOREIGN KEY (storage_id) REFERENCES storage_locations (id),
|
||||
FOREIGN KEY (hash_id) REFERENCES hashes (id)
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE namespaces
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name VARCHAR(128) UNIQUE NOT NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX namespaces_name_index ON namespaces (name);
|
||||
|
||||
|
||||
CREATE TABLE tags
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
namespace_id INTEGER,
|
||||
name VARCHAR(128) UNIQUE NOT NULL,
|
||||
FOREIGN KEY (namespace_id) REFERENCES namespaces (id)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX tag_name_index ON tags (name);
|
||||
|
||||
|
||||
|
||||
CREATE TABLE sources
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
url VARCHAR(512) NOT NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX sources_value_index ON sources (url);
|
||||
|
||||
|
||||
CREATE TABLE hash_tag_mappings
|
||||
(
|
||||
hash_id INTEGER NOT NULL,
|
||||
tag_id INTEGER NOT NULL,
|
||||
PRIMARY KEY (hash_id, tag_id),
|
||||
FOREIGN KEY (hash_id) REFERENCES hashes (id),
|
||||
FOREIGN KEY (tag_id) REFERENCES tags (id)
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE hash_source_mappings
|
||||
(
|
||||
hash_id INTEGER NOT NULL,
|
||||
source_id INTEGER NOT NULL,
|
||||
PRIMARY KEY (hash_id, source_id),
|
||||
FOREIGN KEY (hash_id) REFERENCES hashes (id),
|
||||
FOREIGN KEY (source_id) REFERENCES sources (id)
|
||||
)
|
@ -1 +0,0 @@
|
||||
ALTER TABLE files RENAME COLUMN type TO file_type;
|
@ -1,3 +0,0 @@
|
||||
-- Add migration script here
|
||||
ALTER TABLE files
|
||||
ADD COLUMN mime_type VARCHAR(128);
|
@ -1,14 +0,0 @@
|
||||
-- Add migration script here
|
||||
CREATE TABLE thumbnails (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
hash_id INTEGER UNIQUE NOT NULL,
|
||||
storage_id INTEGER NOT NULL,
|
||||
file_id INTEGER NOT NULL,
|
||||
height INTEGER NOT NULL,
|
||||
width INTEGER NOT NULL,
|
||||
FOREIGN KEY (hash_id) REFERENCES hashes (id),
|
||||
FOREIGN KEY (storage_id) REFERENCES storage_locations (id),
|
||||
FOREIGN KEY (file_id) REFERENCES files (id)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX thumbnail_file_resolution ON thumbnails (file_id, height, width);
|
@ -1,3 +0,0 @@
|
||||
-- Add migration script here
|
||||
ALTER TABLE thumbnails
|
||||
ADD COLUMN mime VARCHAR(128);
|
@ -1,8 +0,0 @@
|
||||
-- Add migration script here
|
||||
DELETE FROM thumbnails WHERE file_id NOT IN (SELECT MIN(files.id) FROM files GROUP BY hash_id);
|
||||
DELETE FROM files WHERE ROWID NOT IN (SELECT MIN(ROWID) FROM files GROUP BY hash_id);
|
||||
DELETE FROM thumbnails WHERE hash_id NOT IN (SELECT MIN(hashes.id) FROM hashes GROUP BY value);
|
||||
DELETE FROM files WHERE hash_id NOT IN (SELECT MIN(hashes.id) FROM hashes GROUP BY value);
|
||||
DELETE FROM hashes WHERE ROWID NOT IN (SELECT MIN(ROWID) FROM hashes GROUP BY value);
|
||||
CREATE UNIQUE INDEX hash_value_index ON hashes (value);
|
||||
CREATE UNIQUE INDEX file_hash_id ON files (hash_id);
|
@ -1,33 +0,0 @@
|
||||
-- Add migration script here
|
||||
PRAGMA foreign_keys=off;
|
||||
|
||||
ALTER TABLE tags RENAME TO _tags_old;
|
||||
CREATE TABLE tags
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
namespace_id INTEGER,
|
||||
name VARCHAR(128),
|
||||
FOREIGN KEY (namespace_id) REFERENCES namespaces (id)
|
||||
);
|
||||
CREATE UNIQUE INDEX tag_namespace_name_index ON tags (namespace_id, name);
|
||||
|
||||
INSERT INTO tags SELECT * FROM _tags_old;
|
||||
|
||||
DROP TABLE _tags_old;
|
||||
|
||||
ALTER TABLE hash_tag_mappings RENAME TO _hash_tag_mappings_old;
|
||||
CREATE TABLE hash_tag_mappings
|
||||
(
|
||||
hash_id INTEGER NOT NULL,
|
||||
tag_id INTEGER NOT NULL,
|
||||
PRIMARY KEY (hash_id, tag_id),
|
||||
FOREIGN KEY (hash_id) REFERENCES hashes (id),
|
||||
FOREIGN KEY (tag_id) REFERENCES tags (id)
|
||||
);
|
||||
CREATE UNIQUE INDEX hash_tag_mappings_hash_tag ON hash_tag_mappings (hash_id, tag_id);
|
||||
|
||||
INSERT INTO hash_tag_mappings SELECT * FROM _hash_tag_mappings_old;
|
||||
|
||||
DROP TABLE _hash_tag_mappings_old;
|
||||
|
||||
PRAGMA foreign_keys=on;
|
@ -1,3 +0,0 @@
|
||||
-- Add migration script here
|
||||
CREATE INDEX index_hash_tag_mappings_tag_id ON hash_tag_mappings (tag_id);
|
||||
CREATE INDEX index_tag_name ON tags (name);
|
@ -1,2 +0,0 @@
|
||||
-- Add migration script here
|
||||
DROP TABLE thumbnails;
|
@ -1,3 +0,0 @@
|
||||
-- Add migration script here
|
||||
ALTER TABLE files
|
||||
ADD COLUMN size INTEGER;
|
@ -1,107 +0,0 @@
|
||||
-- Add migration script here
|
||||
PRAGMA foreign_keys= off;
|
||||
|
||||
-- create backup files table
|
||||
ALTER TABLE files
|
||||
RENAME TO _files_old;
|
||||
|
||||
-- create backup hashes table
|
||||
ALTER TABLE hashes
|
||||
RENAME TO _hashes_old;
|
||||
|
||||
-- create backup hash_tag_mappings table
|
||||
ALTER TABLE hash_tag_mappings
|
||||
RENAME TO _hash_tag_mappings_old;
|
||||
|
||||
-- create backup hash_source_mappings table
|
||||
ALTER TABLE hash_source_mappings
|
||||
RENAME TO _hash_source_mappings_old;
|
||||
|
||||
-- create content id table
|
||||
CREATE TABLE content_descriptors
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
descriptor BLOB NOT NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX content_descriptor_values ON content_descriptors (descriptor);
|
||||
|
||||
-- create content-id tag mappings table
|
||||
CREATE TABLE cd_tag_mappings
|
||||
(
|
||||
cd_id INTEGER NOT NULL REFERENCES content_descriptors (id),
|
||||
tag_id INTEGER NOT NULL REFERENCES tags (id),
|
||||
PRIMARY KEY (cd_id, tag_id)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX content_descriptor_tag_mapping_unique ON cd_tag_mappings (cd_id, tag_id);
|
||||
CREATE INDEX content_descriptor_tag_mapping_tag ON cd_tag_mappings (tag_id);
|
||||
|
||||
-- create content-id source mappings table
|
||||
CREATE TABLE cd_source_mappings
|
||||
(
|
||||
cd_id INTEGER NOT NULL REFERENCES content_descriptors (id),
|
||||
source_id INTEGER NOT NULL REFERENCES sources (id),
|
||||
PRIMARY KEY (cd_id, source_id)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX content_descriptor_source_mapping_unique ON cd_source_mappings (cd_id, source_id);
|
||||
|
||||
-- create new files table
|
||||
CREATE TABLE files
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
status INTEGER NOT NULL DEFAULT 10,
|
||||
storage_id INTEGER NOT NULL REFERENCES storage_locations (id),
|
||||
cd_id INTEGER NOT NULL REFERENCES content_descriptors (id),
|
||||
mime_type VARCHAR(128) NOT NULL DEFAULT 'application/octet-stream'
|
||||
);
|
||||
|
||||
CREATE INDEX files_contend_descriptor ON files (cd_id);
|
||||
|
||||
-- create metadata table
|
||||
CREATE TABLE file_metadata
|
||||
(
|
||||
file_id INTEGER PRIMARY KEY REFERENCES files (id),
|
||||
size INTEGER NOT NULL,
|
||||
name VARCHAR(128),
|
||||
comment VARCHAR(1024),
|
||||
import_time DATETIME NOT NULL,
|
||||
creation_time DATETIME NOT NULL,
|
||||
change_time DATETIME NOT NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX file_metadata_file_id_unique ON file_metadata (file_id);
|
||||
|
||||
-- add content identifiers from hashes table
|
||||
INSERT INTO content_descriptors
|
||||
SELECT id, value
|
||||
FROM _hashes_old;
|
||||
|
||||
-- add files from files table
|
||||
INSERT INTO files (id, storage_id, cd_id, mime_type)
|
||||
SELECT id, storage_id, hash_id AS content_id, mime_type
|
||||
FROM _files_old;
|
||||
|
||||
-- add metadata from files table
|
||||
INSERT INTO file_metadata
|
||||
SELECT id AS file_id, size, name, comment, import_time, creation_time, change_time
|
||||
FROM _files_old;
|
||||
|
||||
-- add content tag mappings
|
||||
INSERT INTO cd_tag_mappings
|
||||
SELECT hash_id AS content_id, tag_id
|
||||
FROM _hash_tag_mappings_old;
|
||||
|
||||
-- add content id source mappings
|
||||
INSERT INTO cd_source_mappings
|
||||
SELECT hash_id AS content_id, source_id
|
||||
FROM _hash_source_mappings_old;
|
||||
|
||||
-- drop all old tables
|
||||
DROP TABLE _hash_source_mappings_old;
|
||||
DROP TABLE _hash_tag_mappings_old;
|
||||
DROP TABLE _files_old;
|
||||
DROP TABLE _hashes_old;
|
||||
|
||||
pragma foreign_keys= on;
|
@ -1,50 +0,0 @@
|
||||
-- Add migration script here
|
||||
PRAGMA foreign_keys= off;
|
||||
|
||||
-- rename old files table
|
||||
ALTER TABLE files
|
||||
RENAME TO _files_old;
|
||||
-- rename metadata value (because of foreign key constraints)
|
||||
ALTER TABLE file_metadata
|
||||
RENAME TO _file_metadata_old;
|
||||
|
||||
-- create new files table
|
||||
CREATE TABLE files
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
status INTEGER NOT NULL DEFAULT 10,
|
||||
cd_id INTEGER NOT NULL REFERENCES content_descriptors (id),
|
||||
mime_type VARCHAR(128) NOT NULL DEFAULT 'application/octet-stream'
|
||||
);
|
||||
-- add data from files table
|
||||
INSERT INTO files
|
||||
SELECT id, status, cd_id, mime_type
|
||||
FROM _files_old;
|
||||
|
||||
-- create metadata table
|
||||
CREATE TABLE file_metadata
|
||||
(
|
||||
file_id INTEGER PRIMARY KEY REFERENCES files (id),
|
||||
size INTEGER NOT NULL,
|
||||
name VARCHAR(128),
|
||||
comment VARCHAR(1024),
|
||||
import_time DATETIME NOT NULL,
|
||||
creation_time DATETIME NOT NULL,
|
||||
change_time DATETIME NOT NULL
|
||||
);
|
||||
|
||||
-- add back the old values
|
||||
INSERT INTO file_metadata
|
||||
SELECT *
|
||||
FROM _file_metadata_old;
|
||||
|
||||
-- drop old tables
|
||||
DROP TABLE _file_metadata_old;
|
||||
DROP TABLE _files_old;
|
||||
DROP TABLE storage_locations;
|
||||
|
||||
-- create indices on new tables
|
||||
CREATE UNIQUE INDEX file_metadata_file_id_unique ON file_metadata (file_id);
|
||||
CREATE INDEX files_content_descriptor ON files (cd_id);
|
||||
|
||||
PRAGMA foreign_keys= on;
|
@ -1,20 +0,0 @@
|
||||
-- Add migration script here
|
||||
CREATE TABLE sorting_presets (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT
|
||||
);
|
||||
|
||||
CREATE TABLE sort_keys (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
key_type INTEGER NOT NULL DEFAULT 0,
|
||||
ascending INTEGER NOT NULL CHECK (ascending IN (0, 1)),
|
||||
value VARCHAR(128)
|
||||
);
|
||||
|
||||
CREATE TABLE sorting_preset_keys (
|
||||
preset_id INTEGER REFERENCES sorting_presets (id) ON DELETE CASCADE,
|
||||
key_id INTEGER REFERENCES sort_keys (id),
|
||||
key_index INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (preset_id, key_id)
|
||||
);
|
||||
|
||||
CREATE INDEX sorting_preset_index ON sorting_preset_keys (preset_id);
|
@ -1,17 +0,0 @@
|
||||
PRAGMA foreign_keys= off;
|
||||
ALTER TABLE sorting_preset_keys
|
||||
RENAME TO _sorting_preset_keys_old;
|
||||
|
||||
CREATE TABLE sorting_preset_keys
|
||||
(
|
||||
preset_id INTEGER REFERENCES sorting_presets (id) ON DELETE CASCADE,
|
||||
key_id INTEGER REFERENCES sort_keys (id),
|
||||
key_index INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (preset_id, key_id, key_index)
|
||||
);
|
||||
|
||||
INSERT INTO sorting_preset_keys SELECT * FROM _sorting_preset_keys_old;
|
||||
|
||||
DROP TABLE _sorting_preset_keys_old;
|
||||
|
||||
PRAGMA foreign_keys= on;
|
@ -1,5 +0,0 @@
|
||||
CREATE TABLE job_states (
|
||||
job_type INTEGER NOT NULL,
|
||||
value BLOB,
|
||||
PRIMARY KEY (job_type)
|
||||
);
|
Loading…
Reference in New Issue