Skip to content

Commit b94dd48

Browse files
fix test
1 parent 46db72b commit b94dd48

3 files changed

Lines changed: 51 additions & 29 deletions

File tree

crates/cli/src/subcommands/subscribe.rs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use reqwest::Url;
66
use serde_json::Value;
77
use spacetimedb_client_api_messages::websocket::{self as ws, JsonFormat};
88
use spacetimedb_data_structures::map::HashMap;
9-
use spacetimedb_lib::db::raw_def::v9::{RawMiscModuleExportV9, RawModuleDefV9};
9+
use spacetimedb_lib::db::raw_def::v9::RawModuleDefV9;
1010
use spacetimedb_lib::de::serde::{DeserializeWrapper, SeedWrapper};
1111
use spacetimedb_lib::ser::serde::SerializeWrapper;
1212
use std::io;
@@ -86,33 +86,11 @@ fn reformat_update<'a>(
8686
msg.tables
8787
.iter()
8888
.map(|upd| {
89-
let product_type_ref = schema
90-
.tables
91-
.iter()
92-
.find(|tbl| tbl.name == upd.table_name)
93-
.map(|table_def| table_def.product_type_ref)
94-
.or_else(|| {
95-
schema
96-
.misc_exports
97-
.iter()
98-
.filter_map(|misc_export| match misc_export {
99-
RawMiscModuleExportV9::View(view_def) => Some(view_def),
100-
_ => None,
101-
})
102-
.find(|view_def| view_def.name == upd.table_name)
103-
.map(|view_def| view_def.return_type.clone())
104-
.and_then(|return_type| {
105-
return_type
106-
.as_option()
107-
.map(|inner| inner.clone().into_ref())
108-
.or_else(|| return_type.as_array().map(|inner| inner.elem_ty.clone().into_ref()))
109-
.transpose()
110-
.expect("views must return an option or a vec")
111-
})
112-
})
113-
.context("table not found in schema")?;
114-
115-
let table_ty = schema.typespace.resolve(product_type_ref);
89+
let table_ty = schema.typespace.resolve(
90+
schema
91+
.type_ref_for_table_like(&upd.table_name)
92+
.context("table not found in schema")?,
93+
);
11694

11795
let reformat_row = |row: &str| -> anyhow::Result<Value> {
11896
// TODO: can the following two calls be merged into a single call to reduce allocations?

crates/lib/src/db/raw_def/v9.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,50 @@ pub struct RawModuleDefV9 {
9797
pub row_level_security: Vec<RawRowLevelSecurityDefV9>,
9898
}
9999

100+
impl RawModuleDefV9 {
101+
/// Find a [`RawTableDefV9`] by name in this raw module def
102+
fn find_table_def(&self, table_name: &str) -> Option<&RawTableDefV9> {
103+
self.tables
104+
.iter()
105+
.find(|table_def| table_def.name.as_ref() == table_name)
106+
}
107+
108+
/// Find a [`RawViewDefV9`] by name in this raw module def
109+
fn find_view_def(&self, view_name: &str) -> Option<&RawViewDefV9> {
110+
self.misc_exports.iter().find_map(|misc_export| match misc_export {
111+
RawMiscModuleExportV9::View(view_def) if view_def.name.as_ref() == view_name => Some(view_def),
112+
_ => None,
113+
})
114+
}
115+
116+
/// Find and return the product type ref for a table in this module def
117+
fn type_ref_for_table(&self, table_name: &str) -> Option<AlgebraicTypeRef> {
118+
self.find_table_def(table_name)
119+
.map(|table_def| table_def.product_type_ref)
120+
}
121+
122+
/// Find and return the product type ref for a view in this module def
123+
fn type_ref_for_view(&self, view_name: &str) -> Option<AlgebraicTypeRef> {
124+
self.find_view_def(view_name)
125+
.map(|view_def| &view_def.return_type)
126+
.and_then(|return_type| {
127+
return_type
128+
.as_option()
129+
.and_then(|inner| inner.clone().into_ref().ok())
130+
.or_else(|| {
131+
return_type
132+
.as_array()
133+
.and_then(|inner| inner.elem_ty.clone().into_ref().ok())
134+
})
135+
})
136+
}
137+
138+
/// Find and return the product type ref for a table or view in this module def
139+
pub fn type_ref_for_table_like(&self, name: &str) -> Option<AlgebraicTypeRef> {
140+
self.type_ref_for_table(name).or_else(|| self.type_ref_for_view(name))
141+
}
142+
}
143+
100144
/// The definition of a database table.
101145
///
102146
/// This struct holds information about the table, including its name, columns, indexes,

smoketests/tests/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def test_subscribing_with_different_identities(self):
458458
self.call("insert_player", "Bob")
459459

460460
# Subscribe to `my_player` as Bob
461-
sub = self.subscribe("select * from my_player", n=1)
461+
sub = self.subscribe("select * from my_player")
462462
events = sub()
463463

464464
# Project out the identity field.

0 commit comments

Comments
 (0)