Skip to content

Commit fa7a28c

Browse files
fix ViewId::SENTINEL
1 parent 223dc20 commit fa7a28c

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

crates/datastore/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub enum DatastoreError {
4040
#[derive(Error, Debug)]
4141
pub enum ViewError {
4242
#[error("view '{0}' not found")]
43-
ViewNotFound(String),
43+
NotFound(Box<str>),
4444
#[error("failed to deserialize view arguments from row")]
4545
DeserializeArgs,
4646
#[error("failed to deserialize view return value: {0}")]

crates/datastore/src/locking_tx_datastore/mut_tx.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ use super::{
88
tx_state::{IndexIdMap, PendingSchemaChange, TxState, TxTableForInsertion},
99
SharedMutexGuard, SharedWriteGuard,
1010
};
11-
use crate::system_tables::{
12-
system_tables, ConnectionIdViaU128, IdentityViaU256, StConnectionCredentialsFields, StConnectionCredentialsRow,
13-
StViewArgFields, StViewArgRow, StViewColumnFields, StViewFields, StViewParamFields, StViewParamRow,
14-
StViewSubFields, StViewSubRow, ST_CONNECTION_CREDENTIALS_ID, ST_VIEW_ARG_ID, ST_VIEW_COLUMN_ID, ST_VIEW_ID,
15-
ST_VIEW_PARAM_ID, ST_VIEW_SUB_ID,
16-
};
1711
use crate::traits::{InsertFlags, RowTypeForTable, TxData, UpdateFlags};
12+
use crate::{
13+
error::ViewError,
14+
system_tables::{
15+
system_tables, ConnectionIdViaU128, IdentityViaU256, StConnectionCredentialsFields, StConnectionCredentialsRow,
16+
StViewArgFields, StViewArgRow, StViewColumnFields, StViewFields, StViewParamFields, StViewParamRow,
17+
StViewSubFields, StViewSubRow, ST_CONNECTION_CREDENTIALS_ID, ST_VIEW_ARG_ID, ST_VIEW_COLUMN_ID, ST_VIEW_ID,
18+
ST_VIEW_PARAM_ID, ST_VIEW_SUB_ID,
19+
},
20+
};
1821
use crate::{
1922
error::{IndexError, SequenceError, TableError},
2023
system_tables::{
@@ -51,7 +54,7 @@ use spacetimedb_sats::{
5154
AlgebraicType, AlgebraicValue, ProductType, ProductValue, WithTypespace,
5255
};
5356
use spacetimedb_schema::{
54-
def::{ModuleDef, ViewColumnDef, ViewDef},
57+
def::{ModuleDef, ViewColumnDef, ViewDef, ViewParamDef},
5558
schema::{ColumnSchema, ConstraintSchema, IndexSchema, RowLevelSecuritySchema, SequenceSchema, TableSchema},
5659
};
5760
use spacetimedb_table::{
@@ -341,16 +344,21 @@ impl MutTxId {
341344

342345
let ViewDef {
343346
name,
344-
is_anonymous,
345-
is_public,
346-
params,
347+
param_columns,
347348
return_columns,
348349
..
349350
} = view_def;
350351

351-
let view_id = self.insert_into_st_view(name.clone().into(), table_id, *is_public, *is_anonymous)?;
352-
self.insert_into_st_view_param(view_id, params)?;
352+
let view_name: Box<str> = name.clone().into();
353+
354+
// `create_table` inserts into `st_view` and updates the table schema.
355+
let view_id = self
356+
.view_id_from_name(&view_name)?
357+
.ok_or(ViewError::NotFound(view_name))?;
358+
359+
self.insert_into_st_view_param(view_id, param_columns)?;
353360
self.insert_into_st_view_column(view_id, return_columns)?;
361+
354362
Ok((view_id, table_id))
355363
}
356364

@@ -411,6 +419,10 @@ impl MutTxId {
411419

412420
table_schema.update_table_id(table_id);
413421

422+
if let Some(info) = table_schema.view_info.as_mut() {
423+
info.view_id = self.insert_into_st_view(table_name.clone(), table_id, true, info.is_anonymous)?;
424+
}
425+
414426
// Generate the full definition of the table, with the generated indexes, constraints, sequences...
415427

416428
// Insert the columns into `st_column`.
@@ -528,18 +540,15 @@ impl MutTxId {
528540

529541
/// For each parameter of a view, insert a row into `st_view_param`.
530542
/// This does not include the context parameter.
531-
fn insert_into_st_view_param(&mut self, view_id: ViewDatabaseId, params: &ProductType) -> Result<()> {
532-
for (i, field) in params.elements.iter().enumerate() {
543+
fn insert_into_st_view_param(&mut self, view_id: ViewDatabaseId, params: &[ViewParamDef]) -> Result<()> {
544+
for ViewParamDef { name, col_id, ty, .. } in params {
533545
self.insert_via_serialize_bsatn(
534546
ST_VIEW_PARAM_ID,
535547
&StViewParamRow {
536548
view_id,
537-
param_pos: i.into(),
538-
param_name: field
539-
.name
540-
.clone()
541-
.unwrap_or_else(|| format!("param_{i}").into_boxed_str()),
542-
param_type: field.algebraic_type.clone().into(),
549+
param_pos: *col_id,
550+
param_name: name.clone().into(),
551+
param_type: ty.clone().into(),
543552
},
544553
)?;
545554
}

0 commit comments

Comments
 (0)