Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(torii-sqlite): primitive upgrades towards bigger types #2931

Merged
merged 11 commits into from
Feb 6, 2025
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/upgrade_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
world.model_reader(&namespace, &name).await?
};
let new_schema = model.schema().await?;
let schema_diff = new_schema.diff(&prev_schema);
let schema_diff = prev_schema.diff(&new_schema);

Check warning on line 97 in crates/torii/indexer/src/processors/upgrade_event.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/indexer/src/processors/upgrade_event.rs#L97

Added line #L97 was not covered by tests
// No changes to the schema. This can happen if torii is re-run with a fresh database.
// As the register model fetches the latest schema from the chain.
if schema_diff.is_none() {
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/indexer/src/processors/upgrade_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
world.model_reader(&namespace, &name).await?
};
let new_schema = model.schema().await?;
let schema_diff = new_schema.diff(&prev_schema);
let schema_diff = prev_schema.diff(&new_schema);

Check warning on line 95 in crates/torii/indexer/src/processors/upgrade_model.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/indexer/src/processors/upgrade_model.rs#L95

Added line #L95 was not covered by tests
// No changes to the schema. This can happen if torii is re-run with a fresh database.
// As the register model fetches the latest schema from the chain.
if schema_diff.is_none() {
Expand Down
31 changes: 29 additions & 2 deletions crates/torii/sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use anyhow::{anyhow, Context, Result};
use dojo_types::naming::get_tag;
use dojo_types::primitive::SqlType;
use dojo_types::schema::{Struct, Ty};
use dojo_world::config::WorldMetadata;
use dojo_world::contracts::abigen::model::Layout;
Expand Down Expand Up @@ -887,7 +888,10 @@
alter_table_queries.push(format!("ALTER TABLE [{table_id}] DROP COLUMN [{name}]"));
alter_table_queries
.push(format!("ALTER TABLE [{table_id}] ADD COLUMN [{name}] {sql_type}"));
alter_table_queries.push(format!("UPDATE [{table_id}] SET [{name}] = {sql_value}"));
alter_table_queries.push(format!(
"UPDATE [{table_id}] SET [{name}] = (SELECT {sql_value} FROM tmp_values_{name} \
WHERE tmp_values_{name}.internal_id = [{table_id}].internal_id)"
));

Check warning on line 894 in crates/torii/sqlite/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/sqlite/src/lib.rs#L891-L894

Added lines #L891 - L894 were not covered by tests
alter_table_queries.push(format!("DROP TABLE tmp_values_{name}"));
alter_table_queries.push(format!(
"CREATE INDEX IF NOT EXISTS [idx_{table_id}_{name}] ON [{table_id}] ([{name}]);"
Expand Down Expand Up @@ -1032,7 +1036,30 @@
let column_name =
if column_prefix.is_empty() { "value".to_string() } else { column_prefix };

add_column(&column_name, p.to_sql_type().as_ref());
if let Some(upgrade_diff) = upgrade_diff {
if let Some(old_primitive) = upgrade_diff.as_primitive() {

Check warning on line 1040 in crates/torii/sqlite/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/sqlite/src/lib.rs#L1040

Added line #L1040 was not covered by tests
// For upgrades to larger numeric types, convert to hex string padded to 64
// chars
let sql_value = if old_primitive.to_sql_type() == SqlType::Integer
&& p.to_sql_type() == SqlType::Text

Check warning on line 1044 in crates/torii/sqlite/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/sqlite/src/lib.rs#L1043-L1044

Added lines #L1043 - L1044 were not covered by tests
{
// Convert integer to hex string with '0x' prefix and proper padding
format!("printf('%064x', [{column_name}])")

Check warning on line 1047 in crates/torii/sqlite/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/sqlite/src/lib.rs#L1047

Added line #L1047 was not covered by tests
} else {
format!("[{column_name}]")

Check warning on line 1049 in crates/torii/sqlite/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/sqlite/src/lib.rs#L1049

Added line #L1049 was not covered by tests
};

modify_column(
alter_table_queries,
&column_name,
p.to_sql_type().as_ref(),
&sql_value,
);
}

Check warning on line 1058 in crates/torii/sqlite/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/sqlite/src/lib.rs#L1052-L1058

Added lines #L1052 - L1058 were not covered by tests
} else {
// New column
add_column(&column_name, p.to_sql_type().as_ref());
}
}
}

Expand Down
Loading