database: Allow tracks without medium

This commit is contained in:
Elias Projahn 2023-02-11 11:13:13 +01:00
parent 7eb85f094f
commit a3f585aadf
4 changed files with 35 additions and 5 deletions

View file

@ -0,0 +1,15 @@
CREATE TABLE "old_tracks" (
"id" TEXT NOT NULL PRIMARY KEY,
"medium" TEXT NOT NULL REFERENCES "mediums"("id") ON DELETE CASCADE,
"index" INTEGER NOT NULL,
"recording" TEXT NOT NULL REFERENCES "recordings"("id"),
"work_parts" TEXT NOT NULL,
"source_index" INTEGER NOT NULL,
"path" TEXT NOT NULL,
"last_used" BIGINT,
"last_played" BIGINT
);
INSERT INTO "old_tracks" SELECT * FROM "tracks" WHERE "medium" IS NOT NULL;
DROP TABLE "tracks";
ALTER TABLE "old_tracks" RENAME TO "tracks";

View file

@ -0,0 +1,15 @@
CREATE TABLE "new_tracks" (
"id" TEXT NOT NULL PRIMARY KEY,
"medium" TEXT REFERENCES "mediums"("id") ON DELETE CASCADE,
"index" INTEGER NOT NULL,
"recording" TEXT NOT NULL REFERENCES "recordings"("id"),
"work_parts" TEXT NOT NULL,
"source_index" INTEGER NOT NULL,
"path" TEXT NOT NULL,
"last_used" BIGINT,
"last_played" BIGINT
);
INSERT INTO "new_tracks" SELECT * FROM "tracks";
DROP TABLE "tracks";
ALTER TABLE "new_tracks" RENAME TO "tracks";

View file

@ -93,7 +93,7 @@ struct MediumRow {
#[table_name = "tracks"]
struct TrackRow {
pub id: String,
pub medium: String,
pub medium: Option<String>,
pub index: i32,
pub recording: String,
pub work_parts: String,
@ -150,7 +150,7 @@ impl Database {
let track_row = TrackRow {
id: generate_id(),
medium: medium_id.to_owned(),
medium: Some(medium_id.to_owned()),
index: index as i32,
recording: track.recording.id.clone(),
work_parts,
@ -208,7 +208,7 @@ impl Database {
let mut mediums: Vec<Medium> = Vec::new();
let rows = mediums::table
.inner_join(tracks::table.on(tracks::medium.eq(mediums::id)))
.inner_join(tracks::table.on(tracks::medium.eq(mediums::id.nullable())))
.inner_join(recordings::table.on(recordings::id.eq(tracks::recording)))
.inner_join(performances::table.on(performances::recording.eq(recordings::id)))
.inner_join(persons::table.on(persons::id.nullable().eq(performances::person)))
@ -230,7 +230,7 @@ impl Database {
let mut mediums: Vec<Medium> = Vec::new();
let rows = mediums::table
.inner_join(tracks::table.on(tracks::medium.eq(tracks::id)))
.inner_join(tracks::table.on(tracks::medium.eq(tracks::id.nullable())))
.inner_join(recordings::table.on(recordings::id.eq(tracks::recording)))
.inner_join(performances::table.on(performances::recording.eq(recordings::id)))
.inner_join(ensembles::table.on(ensembles::id.nullable().eq(performances::ensemble)))

View file

@ -69,7 +69,7 @@ diesel::table! {
diesel::table! {
tracks (id) {
id -> Text,
medium -> Text,
medium -> Nullable<Text>,
index -> Integer,
recording -> Text,
work_parts -> Text,