From 0dc06414c0def9d245a2c5d2710f3d2943cbb9c2 Mon Sep 17 00:00:00 2001 From: Nasr Date: Fri, 31 Jan 2025 11:11:47 +0700 Subject: [PATCH] fix tuple elements --- crates/dojo/types/src/schema.rs | 8 ++++---- crates/torii/sqlite/src/lib.rs | 29 ++++++++++++----------------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/crates/dojo/types/src/schema.rs b/crates/dojo/types/src/schema.rs index 861d626259..d95a87249e 100644 --- a/crates/dojo/types/src/schema.rs +++ b/crates/dojo/types/src/schema.rs @@ -293,7 +293,7 @@ impl Ty { } (Ty::Tuple(t1), Ty::Tuple(t2)) => { if t1.len() != t2.len() { - Some(Ty::Tuple(t2.clone())) + Some(Ty::Tuple(t1.iter().filter_map(|ty| if !t2.contains(ty) { Some(ty.clone()) } else { None }).collect())) } else { // Compare each tuple element recursively let diff_elements: Vec = @@ -306,21 +306,21 @@ impl Ty { if a1 == a2 { None } else { - Some(Ty::Array(a2.clone())) + Some(Ty::Array(a1.clone())) } } (Ty::ByteArray(b1), Ty::ByteArray(b2)) => { if b1 == b2 { None } else { - Some(Ty::ByteArray(b2.clone())) + Some(Ty::ByteArray(b1.clone())) } } (Ty::Primitive(p1), Ty::Primitive(p2)) => { if p1 == p2 { None } else { - Some(Ty::Primitive(*p2)) + Some(Ty::Primitive(*p1)) } } // Different types entirely - we cannot diff them diff --git a/crates/torii/sqlite/src/lib.rs b/crates/torii/sqlite/src/lib.rs index 4621c8b238..33eff2c959 100644 --- a/crates/torii/sqlite/src/lib.rs +++ b/crates/torii/sqlite/src/lib.rs @@ -899,25 +899,20 @@ fn add_columns_recursive( } } Ty::Tuple(tuple) => { - let tuple_diff = - if let Some(upgrade_diff) = upgrade_diff { upgrade_diff.as_tuple() } else { None }; + let elements_to_process = if let Some(diff) = upgrade_diff.and_then(|d| d.as_tuple()) { + // Only process elements from the diff + diff.iter().filter_map(|m| { + tuple.iter().position(|member| member == m) + .map(|idx| (idx, m)) + }).collect() + } else { + // Process all elements + tuple.iter().enumerate().collect::>() + }; - for (idx, member) in tuple.iter().enumerate() { + for (idx, member) in elements_to_process { let mut new_path = path.to_vec(); new_path.push(idx.to_string()); - - let member_diff = if let Some(diff) = tuple_diff { - if let Some((_, m)) = - diff.iter().enumerate().find(|(i, _)| *i == idx && *i >= tuple.len()) - { - Some(m) - } else { - continue; - } - } else { - None - }; - add_columns_recursive( &new_path, member, @@ -925,7 +920,7 @@ fn add_columns_recursive( alter_table_queries, indices, table_id, - member_diff, + None, )?; } }