feat: migration canonicalize problems

This commit is contained in:
Asger Juul Brunshøj 2025-04-06 21:11:09 +02:00
parent b37386b9e8
commit e5853268de

View File

@ -13,6 +13,43 @@ pub async fn run_migrations(db: &Database) -> Result<(), Box<dyn std::error::Err
if is_at_version(db, 3).await? {
migrate_to_v4(db).await?;
}
if is_at_version(db, 4).await? {
canonicalize_all_problems(db).await?;
}
Ok(())
}
pub async fn canonicalize_all_problems(db: &Database) -> Result<(), Box<dyn std::error::Error>> {
tracing::warn!("CANONICALIZING ALL PROBLEMS");
db.write(|txn| {
let mut walls_table = txn.open_table(db::v4::TABLE_WALLS)?;
let walls_dump = walls_table
.iter()?
.map(|el| {
let (k, v) = el.unwrap();
(k.value(), v.value())
})
.collect::<BTreeMap<_, _>>();
for (wall_uid, mut wall) in walls_dump.into_iter() {
wall.problems = wall
.problems
.into_iter()
.map(|mut problem| {
problem.pattern = problem.pattern.canonicalize();
problem
})
.collect();
walls_table.insert(wall_uid, wall)?;
}
Ok(())
})
.await?;
Ok(())
}