1
Fork 0

Fix foreign key violation while updating songlist

This commit is contained in:
prescientmoon 2025-04-09 00:20:25 +02:00
parent df292832b8
commit c83de766f2
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
2 changed files with 15 additions and 5 deletions
src

View file

@ -171,12 +171,21 @@ pub fn import_songlist(
let ptt_entries = get_ptt_entries(paths).context("Failed to read ptt entries")?;
let transaction = conn.transaction()?;
transaction.execute("DELETE FROM charts", ())?;
transaction.execute("DELETE FROM songs", ())?;
transaction.pragma_update(None, "defer_foreign_keys", "ON")?;
transaction
.execute("DELETE FROM charts", ())
.with_context(|| anyhow!("Failed to delete all charts"))?;
transaction
.execute("DELETE FROM songs", ())
.with_context(|| anyhow!("Failed to delete all songs"))?;
let songlist: Songlist = serde_json::from_reader(std::io::BufReader::new(
std::fs::File::open(paths.songlist_path())?,
))?;
std::fs::File::open(paths.songlist_path())
.with_context(|| anyhow!("Failed to read songlist file"))?,
))
.with_context(|| anyhow!("Failed to parse songlist file"))?;
let mut song_count = 0;
let mut chart_count = 0;
@ -218,7 +227,7 @@ pub fn import_songlist(
chart_count += 1;
let difficulty = crate::private_server::decode_difficulty(chart.difficulty)
.ok_or_else(|| anyhow!("Invalid difficulty"))?;
.ok_or_else(|| anyhow!("Invalid difficulty {}", chart.difficulty))?;
let level = format!(
"{}{}",

View file

@ -19,6 +19,7 @@ pub fn connect_db(paths: &ShimmeringPaths) -> anyhow::Result<SqlitePool> {
let mut conn = rusqlite::Connection::open(&db_path)
.with_context(|| "Could not connect to sqlite database")?;
conn.pragma_update(None, "journal_mode", "WAL")?;
conn.pragma_update(None, "foreign_keys", "ON")?;
// {{{ Run migrations
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");