Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi value fields #181

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ url = "2.3"
[dependencies.diesel]
version = "2.0.2"
default_features = false
features = ["libsqlite3-sys", "r2d2", "sqlite"]
features = ["libsqlite3-sys", "r2d2", "sqlite", "returning_clauses_for_sqlite_3_35"]

[dependencies.image]
version = "0.24.4"
Expand Down
61 changes: 61 additions & 0 deletions migrations/2022-11-12-143833_multiple_artists/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-- songs
CREATE TEMPORARY TABLE songs_backup(id, path, parent, track_number, disc_number, title, year, album, artwork, duration, lyricist, composer, genre, label);
INSERT INTO songs_backup SELECT * FROM songs;
DROP TABLE songs;
CREATE TABLE songs (
id INTEGER PRIMARY KEY NOT NULL,
path TEXT NOT NULL,
parent TEXT NOT NULL,
track_number INTEGER,
disc_number INTEGER,
title TEXT,
artist TEXT,
album_artist TEXT,
year INTEGER,
album TEXT,
artwork TEXT,
duration INTEGER,
lyricist TEXT,
composer TEXT,
genre TEXT,
label TEXT,
UNIQUE(path)
);
INSERT INTO songs
SELECT s.id, s.path, s.parent, s.track_number, s.disc_number, s.title, s.year, s.album, s.artwork, s.duration, s.lyricist, s.composer, s.genre, s.label, a.name AS artist, aa.name AS album_artist
FROM songs_backup s
INNER JOIN song_artists sa ON sa.song = s.id
INNER JOIN artists a ON a.id = sa.artist
INNER JOIN song_album_artists saa ON saa.song = s.id
INNER JOIN artists aa ON aa.id = saa.artist
GROUP BY s.id;
DROP TABLE songs_backup;
DROP TABLE song_artists;
DROP TABLE song_album_artists;

-- directories
CREATE TEMPORARY TABLE directories_backup(id, path, parent, year, album, artwork, date_added);
INSERT INTO directories_backup SELECT * FROM directories;
DROP TABLE directories;
CREATE TABLE directories (
id INTEGER PRIMARY KEY NOT NULL,
path TEXT NOT NULL,
parent TEXT,
artist TEXT,
year TEXT,
album TEXT,
artwork TEXT,
date_added INTEGER NOT NULL,
UNIQUE(path)
);
INSERT INTO directories
SELECT d.id, d.path, d.parent, d.year, d.album, d.artwork, d.date_added, a.name AS artist
FROM directories_backup d
INNER JOIN directory_artists da ON da.directory = d.id
INNER JOIN artists a ON a.id = da.artist
GROUP BY d.id;
DROP TABLE directories_backup;
DROP TABLE directory_artists;

-- artists
DROP TABLE artists;
93 changes: 93 additions & 0 deletions migrations/2022-11-12-143833_multiple_artists/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
CREATE TABLE artists (
id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
UNIQUE(name)
);

-- songs
CREATE TEMPORARY TABLE songs_backup(id, path, parent, track_number, disc_number, title, artist, album_artist, year, album, artwork, duration, lyricist, composer, genre, label);
INSERT INTO songs_backup SELECT * FROM songs;
DROP TABLE songs;
CREATE TABLE songs (
id INTEGER PRIMARY KEY NOT NULL,
path TEXT NOT NULL,
parent TEXT NOT NULL,
track_number INTEGER,
disc_number INTEGER,
title TEXT,
year INTEGER,
album TEXT,
artwork TEXT,
duration INTEGER,
lyricist TEXT,
composer TEXT,
genre TEXT,
label TEXT,
UNIQUE(path)
);
INSERT INTO songs SELECT id, path, parent, track_number, disc_number, title, year, album, artwork, duration, lyricist, composer, genre, label FROM songs_backup;

CREATE TABLE song_artists (
song INTEGER NOT NULL,
artist INTEGER NOT NULL,
PRIMARY KEY (song, artist),
FOREIGN KEY(song) REFERENCES songs(id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(artist) REFERENCES artists(id) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE(song, artist) ON CONFLICT IGNORE
);

CREATE TABLE song_album_artists (
song INTEGER NOT NULL,
artist INTEGER NOT NULL,
PRIMARY KEY (song, artist),
FOREIGN KEY(song) REFERENCES songs(id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(artist) REFERENCES artists(id) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE(song, artist) ON CONFLICT IGNORE
);

INSERT OR IGNORE INTO artists SELECT NULL, s.artist FROM songs_backup s;
INSERT INTO song_artists
SELECT s.id as song, a.id as artist
FROM songs_backup s, artists a
WHERE s.artist == a.name;

INSERT OR IGNORE INTO artists SELECT NULL, s.album_artist AS name FROM songs_backup s;
INSERT INTO song_album_artists
SELECT s.id as song, a.id as album_artist
FROM songs_backup s, artists a
WHERE s.artist == a.name;

DROP TABLE songs_backup;

-- directories
CREATE TEMPORARY TABLE directories_backup(id, path, parent, artist, year, album, artwork, date_added);
INSERT INTO directories_backup SELECT * FROM directories;
DROP TABLE directories;
CREATE TABLE directories (
id INTEGER PRIMARY KEY NOT NULL,
path TEXT NOT NULL,
parent TEXT,
year TEXT,
album TEXT,
artwork TEXT,
date_added INTEGER NOT NULL,
UNIQUE(path)
);
INSERT INTO directories SELECT id, path, parent, year, album, artwork, date_added FROM directories_backup;

CREATE TABLE directory_artists (
directory INTEGER NOT NULL,
artist INTEGER NOT NULL,
PRIMARY KEY (directory, artist),
FOREIGN KEY(directory) REFERENCES directories(id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(artist) REFERENCES artists(id) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE(directory, artist) ON CONFLICT IGNORE
);

INSERT OR IGNORE INTO artists SELECT NULL, d.artist AS name FROM directories_backup d;
INSERT INTO directory_artists
SELECT d.id as directory, a.id as artist
FROM directories_backup d, artists a
WHERE d.artist == a.name;

DROP TABLE directories_backup;
Empty file added src/app/artists.rs
Empty file.
Loading
Loading