Fix foreign key constraints for updates

This commit is contained in:
Elias Projahn 2020-10-10 09:58:38 +02:00
parent 78c1c3e540
commit d8247d1361
2 changed files with 85 additions and 60 deletions

View file

@ -56,7 +56,7 @@ CREATE TABLE recordings (
CREATE TABLE performances (
id BIGINT NOT NULL PRIMARY KEY,
recording BIGINT NOT NULL REFERENCES recordings(id) ON DELETE CASCADE,
person BIGINT REFERENCES persons(id) ON DELETE CASCADE,
ensemble BIGINT REFERENCES ensembles(id) ON DELETE CASCADE,
person BIGINT REFERENCES persons(id),
ensemble BIGINT REFERENCES ensembles(id),
role BIGINT REFERENCES instruments(id)
);

View file

@ -24,9 +24,13 @@ impl Database {
}
pub fn update_person(&self, person: Person) {
self.defer_foreign_keys();
self.c
.transaction(|| {
diesel::replace_into(persons::table)
.values(person)
.execute(&self.c)
})
.expect("Failed to insert person!");
}
@ -52,9 +56,13 @@ impl Database {
}
pub fn update_instrument(&self, instrument: Instrument) {
self.defer_foreign_keys();
self.c
.transaction(|| {
diesel::replace_into(instruments::table)
.values(instrument)
.execute(&self.c)
})
.expect("Failed to insert instrument!");
}
@ -82,12 +90,14 @@ impl Database {
pub fn update_work(&self, work_insertion: WorkInsertion) {
let id = work_insertion.work.id;
self.defer_foreign_keys();
self.c
.transaction(|| {
self.delete_work(id);
diesel::insert_into(works::table)
.values(work_insertion.work)
.execute(&self.c)
.expect("Failed to insert work!");
.execute(&self.c)?;
for instrument_id in work_insertion.instrument_ids {
diesel::insert_into(instrumentations::table)
@ -96,8 +106,7 @@ impl Database {
work: id,
instrument: instrument_id,
})
.execute(&self.c)
.expect("Failed to insert instrumentation!");
.execute(&self.c)?;
}
for part_insertion in work_insertion.parts {
@ -105,8 +114,7 @@ impl Database {
diesel::insert_into(work_parts::table)
.values(part_insertion.part)
.execute(&self.c)
.expect("Failed to insert work part!");
.execute(&self.c)?;
for instrument_id in part_insertion.instrument_ids {
diesel::insert_into(part_instrumentations::table)
@ -115,17 +123,19 @@ impl Database {
work_part: part_id,
instrument: instrument_id,
})
.execute(&self.c)
.expect("Failed to insert part instrumentation!");
.execute(&self.c)?;
}
}
for section in work_insertion.sections {
diesel::insert_into(work_sections::table)
.values(section)
.execute(&self.c)
.expect("Failed to insert work section!");
.execute(&self.c)?;
}
diesel::result::QueryResult::Ok(())
})
.expect("Failed to update work!");
}
pub fn get_work(&self, id: i64) -> Option<Work> {
@ -221,9 +231,13 @@ impl Database {
}
pub fn update_ensemble(&self, ensemble: Ensemble) {
self.defer_foreign_keys();
self.c
.transaction(|| {
diesel::replace_into(ensembles::table)
.values(ensemble)
.execute(&self.c)
})
.expect("Failed to insert ensemble!");
}
@ -251,19 +265,24 @@ impl Database {
pub fn update_recording(&self, recording_insertion: RecordingInsertion) {
let id = recording_insertion.recording.id;
self.defer_foreign_keys();
self.c
.transaction(|| {
self.delete_recording(id);
diesel::insert_into(recordings::table)
.values(recording_insertion.recording)
.execute(&self.c)
.expect("Failed to insert recording!");
.execute(&self.c)?;
for performance in recording_insertion.performances {
diesel::insert_into(performances::table)
.values(performance)
.execute(&self.c)
.expect("Failed to insert performance!");
.execute(&self.c)?;
}
diesel::result::QueryResult::Ok(())
})
.expect("Failed to insert performance!");
}
pub fn get_recording(&self, id: i64) -> Option<Recording> {
@ -342,4 +361,10 @@ impl Database {
.load::<Recording>(&self.c)
.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!");
}
}