Skip to content

Commit 3383fd5

Browse files
fix ViewId::SENTINEL
1 parent 7df3e77 commit 3383fd5

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::{
@@ -356,16 +359,21 @@ impl MutTxId {
356359

357360
let ViewDef {
358361
name,
359-
is_anonymous,
360-
is_public,
361-
params,
362+
param_columns,
362363
return_columns,
363364
..
364365
} = view_def;
365366

366-
let view_id = self.insert_into_st_view(name.clone().into(), table_id, *is_public, *is_anonymous)?;
367-
self.insert_into_st_view_param(view_id, params)?;
367+
let view_name: Box<str> = name.clone().into();
368+
369+
// `create_table` inserts into `st_view` and updates the table schema.
370+
let view_id = self
371+
.view_id_from_name(&view_name)?
372+
.ok_or(ViewError::NotFound(view_name))?;
373+
374+
self.insert_into_st_view_param(view_id, param_columns)?;
368375
self.insert_into_st_view_column(view_id, return_columns)?;
376+
369377
Ok((view_id, table_id))
370378
}
371379

@@ -426,6 +434,10 @@ impl MutTxId {
426434

427435
table_schema.update_table_id(table_id);
428436

437+
if let Some(info) = table_schema.view_info.as_mut() {
438+
info.view_id = self.insert_into_st_view(table_name.clone(), table_id, true, info.is_anonymous)?;
439+
}
440+
429441
// Generate the full definition of the table, with the generated indexes, constraints, sequences...
430442

431443
// Insert the columns into `st_column`.
@@ -543,18 +555,15 @@ impl MutTxId {
543555

544556
/// For each parameter of a view, insert a row into `st_view_param`.
545557
/// This does not include the context parameter.
546-
fn insert_into_st_view_param(&mut self, view_id: ViewDatabaseId, params: &ProductType) -> Result<()> {
547-
for (i, field) in params.elements.iter().enumerate() {
558+
fn insert_into_st_view_param(&mut self, view_id: ViewDatabaseId, params: &[ViewParamDef]) -> Result<()> {
559+
for ViewParamDef { name, col_id, ty, .. } in params {
548560
self.insert_via_serialize_bsatn(
549561
ST_VIEW_PARAM_ID,
550562
&StViewParamRow {
551563
view_id,
552-
param_pos: i.into(),
553-
param_name: field
554-
.name
555-
.clone()
556-
.unwrap_or_else(|| format!("param_{i}").into_boxed_str()),
557-
param_type: field.algebraic_type.clone().into(),
564+
param_pos: *col_id,
565+
param_name: name.clone().into(),
566+
param_type: ty.clone().into(),
558567
},
559568
)?;
560569
}

0 commit comments

Comments
 (0)