From a3f585aadf075a1b7b681083189c4cd5ab83929c Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sat, 11 Feb 2023 11:13:13 +0100 Subject: [PATCH] database: Allow tracks without medium --- .../down.sql | 15 +++++++++++++++ .../up.sql | 15 +++++++++++++++ crates/database/src/medium.rs | 8 ++++---- crates/database/src/schema.rs | 2 +- 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 crates/database/migrations/2023-02-11-094238_tracks_without_medium/down.sql create mode 100644 crates/database/migrations/2023-02-11-094238_tracks_without_medium/up.sql diff --git a/crates/database/migrations/2023-02-11-094238_tracks_without_medium/down.sql b/crates/database/migrations/2023-02-11-094238_tracks_without_medium/down.sql new file mode 100644 index 0000000..2a4d3c8 --- /dev/null +++ b/crates/database/migrations/2023-02-11-094238_tracks_without_medium/down.sql @@ -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"; \ No newline at end of file diff --git a/crates/database/migrations/2023-02-11-094238_tracks_without_medium/up.sql b/crates/database/migrations/2023-02-11-094238_tracks_without_medium/up.sql new file mode 100644 index 0000000..671478b --- /dev/null +++ b/crates/database/migrations/2023-02-11-094238_tracks_without_medium/up.sql @@ -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"; \ No newline at end of file diff --git a/crates/database/src/medium.rs b/crates/database/src/medium.rs index 68dd104..d475287 100644 --- a/crates/database/src/medium.rs +++ b/crates/database/src/medium.rs @@ -93,7 +93,7 @@ struct MediumRow { #[table_name = "tracks"] struct TrackRow { pub id: String, - pub medium: String, + pub medium: Option, 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 = 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 = 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))) diff --git a/crates/database/src/schema.rs b/crates/database/src/schema.rs index 6dbcc03..fe72d06 100644 --- a/crates/database/src/schema.rs +++ b/crates/database/src/schema.rs @@ -69,7 +69,7 @@ diesel::table! { diesel::table! { tracks (id) { id -> Text, - medium -> Text, + medium -> Nullable, index -> Integer, recording -> Text, work_parts -> Text,