mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 11:47:25 +01:00
Fix foreign key constraints for updates
This commit is contained in:
parent
78c1c3e540
commit
d8247d1361
2 changed files with 85 additions and 60 deletions
|
|
@ -56,7 +56,7 @@ CREATE TABLE recordings (
|
||||||
CREATE TABLE performances (
|
CREATE TABLE performances (
|
||||||
id BIGINT NOT NULL PRIMARY KEY,
|
id BIGINT NOT NULL PRIMARY KEY,
|
||||||
recording BIGINT NOT NULL REFERENCES recordings(id) ON DELETE CASCADE,
|
recording BIGINT NOT NULL REFERENCES recordings(id) ON DELETE CASCADE,
|
||||||
person BIGINT REFERENCES persons(id) ON DELETE CASCADE,
|
person BIGINT REFERENCES persons(id),
|
||||||
ensemble BIGINT REFERENCES ensembles(id) ON DELETE CASCADE,
|
ensemble BIGINT REFERENCES ensembles(id),
|
||||||
role BIGINT REFERENCES instruments(id)
|
role BIGINT REFERENCES instruments(id)
|
||||||
);
|
);
|
||||||
|
|
@ -24,9 +24,13 @@ impl Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_person(&self, person: Person) {
|
pub fn update_person(&self, person: Person) {
|
||||||
|
self.defer_foreign_keys();
|
||||||
|
self.c
|
||||||
|
.transaction(|| {
|
||||||
diesel::replace_into(persons::table)
|
diesel::replace_into(persons::table)
|
||||||
.values(person)
|
.values(person)
|
||||||
.execute(&self.c)
|
.execute(&self.c)
|
||||||
|
})
|
||||||
.expect("Failed to insert person!");
|
.expect("Failed to insert person!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,9 +56,13 @@ impl Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_instrument(&self, instrument: Instrument) {
|
pub fn update_instrument(&self, instrument: Instrument) {
|
||||||
|
self.defer_foreign_keys();
|
||||||
|
self.c
|
||||||
|
.transaction(|| {
|
||||||
diesel::replace_into(instruments::table)
|
diesel::replace_into(instruments::table)
|
||||||
.values(instrument)
|
.values(instrument)
|
||||||
.execute(&self.c)
|
.execute(&self.c)
|
||||||
|
})
|
||||||
.expect("Failed to insert instrument!");
|
.expect("Failed to insert instrument!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -82,12 +90,14 @@ impl Database {
|
||||||
pub fn update_work(&self, work_insertion: WorkInsertion) {
|
pub fn update_work(&self, work_insertion: WorkInsertion) {
|
||||||
let id = work_insertion.work.id;
|
let id = work_insertion.work.id;
|
||||||
|
|
||||||
|
self.defer_foreign_keys();
|
||||||
|
self.c
|
||||||
|
.transaction(|| {
|
||||||
self.delete_work(id);
|
self.delete_work(id);
|
||||||
|
|
||||||
diesel::insert_into(works::table)
|
diesel::insert_into(works::table)
|
||||||
.values(work_insertion.work)
|
.values(work_insertion.work)
|
||||||
.execute(&self.c)
|
.execute(&self.c)?;
|
||||||
.expect("Failed to insert work!");
|
|
||||||
|
|
||||||
for instrument_id in work_insertion.instrument_ids {
|
for instrument_id in work_insertion.instrument_ids {
|
||||||
diesel::insert_into(instrumentations::table)
|
diesel::insert_into(instrumentations::table)
|
||||||
|
|
@ -96,8 +106,7 @@ impl Database {
|
||||||
work: id,
|
work: id,
|
||||||
instrument: instrument_id,
|
instrument: instrument_id,
|
||||||
})
|
})
|
||||||
.execute(&self.c)
|
.execute(&self.c)?;
|
||||||
.expect("Failed to insert instrumentation!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for part_insertion in work_insertion.parts {
|
for part_insertion in work_insertion.parts {
|
||||||
|
|
@ -105,8 +114,7 @@ impl Database {
|
||||||
|
|
||||||
diesel::insert_into(work_parts::table)
|
diesel::insert_into(work_parts::table)
|
||||||
.values(part_insertion.part)
|
.values(part_insertion.part)
|
||||||
.execute(&self.c)
|
.execute(&self.c)?;
|
||||||
.expect("Failed to insert work part!");
|
|
||||||
|
|
||||||
for instrument_id in part_insertion.instrument_ids {
|
for instrument_id in part_insertion.instrument_ids {
|
||||||
diesel::insert_into(part_instrumentations::table)
|
diesel::insert_into(part_instrumentations::table)
|
||||||
|
|
@ -115,17 +123,19 @@ impl Database {
|
||||||
work_part: part_id,
|
work_part: part_id,
|
||||||
instrument: instrument_id,
|
instrument: instrument_id,
|
||||||
})
|
})
|
||||||
.execute(&self.c)
|
.execute(&self.c)?;
|
||||||
.expect("Failed to insert part instrumentation!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for section in work_insertion.sections {
|
for section in work_insertion.sections {
|
||||||
diesel::insert_into(work_sections::table)
|
diesel::insert_into(work_sections::table)
|
||||||
.values(section)
|
.values(section)
|
||||||
.execute(&self.c)
|
.execute(&self.c)?;
|
||||||
.expect("Failed to insert work section!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
diesel::result::QueryResult::Ok(())
|
||||||
|
})
|
||||||
|
.expect("Failed to update work!");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_work(&self, id: i64) -> Option<Work> {
|
pub fn get_work(&self, id: i64) -> Option<Work> {
|
||||||
|
|
@ -221,9 +231,13 @@ impl Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_ensemble(&self, ensemble: Ensemble) {
|
pub fn update_ensemble(&self, ensemble: Ensemble) {
|
||||||
|
self.defer_foreign_keys();
|
||||||
|
self.c
|
||||||
|
.transaction(|| {
|
||||||
diesel::replace_into(ensembles::table)
|
diesel::replace_into(ensembles::table)
|
||||||
.values(ensemble)
|
.values(ensemble)
|
||||||
.execute(&self.c)
|
.execute(&self.c)
|
||||||
|
})
|
||||||
.expect("Failed to insert ensemble!");
|
.expect("Failed to insert ensemble!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -251,19 +265,24 @@ impl Database {
|
||||||
pub fn update_recording(&self, recording_insertion: RecordingInsertion) {
|
pub fn update_recording(&self, recording_insertion: RecordingInsertion) {
|
||||||
let id = recording_insertion.recording.id;
|
let id = recording_insertion.recording.id;
|
||||||
|
|
||||||
|
self.defer_foreign_keys();
|
||||||
|
self.c
|
||||||
|
.transaction(|| {
|
||||||
self.delete_recording(id);
|
self.delete_recording(id);
|
||||||
|
|
||||||
diesel::insert_into(recordings::table)
|
diesel::insert_into(recordings::table)
|
||||||
.values(recording_insertion.recording)
|
.values(recording_insertion.recording)
|
||||||
.execute(&self.c)
|
.execute(&self.c)?;
|
||||||
.expect("Failed to insert recording!");
|
|
||||||
|
|
||||||
for performance in recording_insertion.performances {
|
for performance in recording_insertion.performances {
|
||||||
diesel::insert_into(performances::table)
|
diesel::insert_into(performances::table)
|
||||||
.values(performance)
|
.values(performance)
|
||||||
.execute(&self.c)
|
.execute(&self.c)?;
|
||||||
.expect("Failed to insert performance!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
diesel::result::QueryResult::Ok(())
|
||||||
|
})
|
||||||
|
.expect("Failed to insert performance!");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_recording(&self, id: i64) -> Option<Recording> {
|
pub fn get_recording(&self, id: i64) -> Option<Recording> {
|
||||||
|
|
@ -342,4 +361,10 @@ impl Database {
|
||||||
.load::<Recording>(&self.c)
|
.load::<Recording>(&self.c)
|
||||||
.expect("Error loading recordings!")
|
.expect("Error loading recordings!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn defer_foreign_keys(&self) {
|
||||||
|
diesel::sql_query("PRAGMA defer_foreign_keys = ON;")
|
||||||
|
.execute(&self.c)
|
||||||
|
.expect("Failed to enable defer_foreign_keys_pragma!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue