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-core): add entities deletions to susbcription broker #2106

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,16 @@
?, ?, ?) ON CONFLICT(id) DO UPDATE SET \
executed_at=EXCLUDED.executed_at, event_id=EXCLUDED.event_id \
RETURNING *";
let entity_updated: EntityUpdated = sqlx::query_as(insert_entities)
let mut entity_updated: EntityUpdated = sqlx::query_as(insert_entities)
.bind(&entity_id)
.bind(&keys_str)
.bind(event_id)
.bind(utc_dt_string_from_timestamp(block_timestamp))
.fetch_one(&self.pool)
.await?;

entity_updated.updated_model = Some(entity.clone());

let path = vec![entity.name()];
self.build_set_entity_queries_recursive(
path,
Expand Down Expand Up @@ -253,8 +255,18 @@
pub async fn delete_entity(&mut self, keys: Vec<FieldElement>, entity: Ty) -> Result<()> {
let entity_id = format!("{:#x}", poseidon_hash_many(&keys));
let path = vec![entity.name()];
// delete entity models data
self.build_delete_entity_queries_recursive(path, &entity_id, &entity);
self.query_queue.execute_all().await?;

// delete entity
let entity_deleted =

Check warning on line 263 in crates/torii/core/src/sql.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/sql.rs#L263

Added line #L263 was not covered by tests
sqlx::query_as::<_, EntityUpdated>("DELETE FROM entities WHERE id = ? RETURNING *")
.bind(entity_id)
.fetch_one(&self.pool)
.await?;

SimpleBroker::publish(entity_deleted);

Check warning on line 269 in crates/torii/core/src/sql.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/sql.rs#L269

Added line #L269 was not covered by tests
Ok(())
}

Expand Down
4 changes: 4 additions & 0 deletions crates/torii/core/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::fmt;

use chrono::{DateTime, Utc};
use dojo_types::schema::Ty;
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use starknet::core::types::FieldElement;
Expand Down Expand Up @@ -37,6 +38,9 @@ pub struct Entity {
pub executed_at: DateTime<Utc>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
// if updated_model is None, then the entity has been deleted
#[sqlx(skip)]
pub updated_model: Option<Ty>,
}

#[derive(FromRow, Deserialize, Debug, Clone)]
Expand Down
8 changes: 8 additions & 0 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@
// total count of rows without limit and offset
let total_count: u32 = sqlx::query_scalar(&count_query).fetch_one(&self.pool).await?;

if total_count == 0 {
return Ok((Vec::new(), 0));
}

Check warning on line 245 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L242-L245

Added lines #L242 - L245 were not covered by tests
// query to filter with limit and offset
let query = format!(
r#"
Expand Down Expand Up @@ -335,6 +339,10 @@
let total_count =
sqlx::query_scalar(&count_query).bind(&keys_pattern).fetch_one(&self.pool).await?;

if total_count == 0 {
return Ok((Vec::new(), 0));

Check warning on line 343 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L343

Added line #L343 was not covered by tests
}

let models_query = format!(
r#"
SELECT group_concat({model_relation_table}.model_id) as model_ids
Expand Down
Loading