@@ -71,6 +71,8 @@ pub const ST_VIEW_ID: TableId = TableId(12);
7171pub const ST_VIEW_PARAM_ID : TableId = TableId ( 13 ) ;
7272/// The static ID of the table that tracks view columns
7373pub const ST_VIEW_COLUMN_ID : TableId = TableId ( 14 ) ;
74+ /// The static ID of the table that tracks the tables a view reads
75+ pub const ST_VIEW_READS_TABLE_ID : TableId = TableId ( 15 ) ;
7476
7577pub ( crate ) const ST_CONNECTION_CREDENTIALS_NAME : & str = "st_connection_credentials" ;
7678pub const ST_TABLE_NAME : & str = "st_table" ;
@@ -86,6 +88,7 @@ pub(crate) const ST_ROW_LEVEL_SECURITY_NAME: &str = "st_row_level_security";
8688pub ( crate ) const ST_VIEW_NAME : & str = "st_view" ;
8789pub ( crate ) const ST_VIEW_PARAM_NAME : & str = "st_view_param" ;
8890pub ( crate ) const ST_VIEW_COLUMN_NAME : & str = "st_view_column" ;
91+ pub ( crate ) const ST_VIEW_READS_TABLE_NAME : & str = "st_view_reads_table" ;
8992/// Reserved range of sequence values used for system tables.
9093///
9194/// Ids for user-created tables will start at `ST_RESERVED_SEQUENCE_RANGE`.
@@ -114,7 +117,7 @@ pub enum SystemTable {
114117 st_row_level_security,
115118}
116119
117- pub fn system_tables ( ) -> [ TableSchema ; 14 ] {
120+ pub fn system_tables ( ) -> [ TableSchema ; 15 ] {
118121 [
119122 // The order should match the `id` of the system table, that start with [ST_TABLE_IDX].
120123 st_table_schema ( ) ,
@@ -131,6 +134,7 @@ pub fn system_tables() -> [TableSchema; 14] {
131134 st_view_schema ( ) ,
132135 st_view_param_schema ( ) ,
133136 st_view_column_schema ( ) ,
137+ st_view_reads_table_schema ( ) ,
134138 ]
135139}
136140
@@ -173,6 +177,7 @@ pub(crate) const ST_CONNECTION_CREDENTIALS_IDX: usize = 10;
173177pub ( crate ) const ST_VIEW_IDX : usize = 11 ;
174178pub ( crate ) const ST_VIEW_PARAM_IDX : usize = 12 ;
175179pub ( crate ) const ST_VIEW_COLUMN_IDX : usize = 13 ;
180+ pub ( crate ) const ST_VIEW_READS_TABLE_IDX : usize = 14 ;
176181
177182macro_rules! st_fields_enum {
178183 ( $( #[ $attr: meta] ) * enum $ty_name: ident { $( $name: expr, $var: ident = $discr: expr, ) * } ) => {
@@ -239,6 +244,12 @@ st_fields_enum!(enum StViewColumnFields {
239244 "col_type" , ColType = 3 ,
240245} ) ;
241246// WARNING: For a stable schema, don't change the field names and discriminants.
247+ st_fields_enum ! ( enum StViewReadsTableFields {
248+ "view_id" , ViewId = 0 ,
249+ "sender" , Sender = 1 ,
250+ "table_id" , TableId = 2 ,
251+ } ) ;
252+ // WARNING: For a stable schema, don't change the field names and discriminants.
242253st_fields_enum ! ( enum StViewParamFields {
243254 "view_id" , ViewId = 0 ,
244255 "param_pos" , ParamPos = 1 ,
@@ -375,6 +386,19 @@ fn system_module_def() -> ModuleDef {
375386 . with_unique_constraint ( st_view_param_unique_cols)
376387 . with_index_no_accessor_name ( btree ( st_view_param_unique_cols) ) ;
377388
389+ let st_view_reads_table_type = builder. add_type :: < StViewReadsTableRow > ( ) ;
390+ builder
391+ . build_table (
392+ ST_VIEW_READS_TABLE_NAME ,
393+ * st_view_reads_table_type. as_ref ( ) . expect ( "should be ref" ) ,
394+ )
395+ . with_type ( TableType :: System )
396+ . with_index_no_accessor_name ( btree ( [
397+ StViewReadsTableFields :: ViewId . col_id ( ) ,
398+ StViewReadsTableFields :: Sender . col_id ( ) ,
399+ StViewReadsTableFields :: TableId . col_id ( ) ,
400+ ] ) ) ;
401+
378402 let st_index_type = builder. add_type :: < StIndexRow > ( ) ;
379403 builder
380404 . build_table ( ST_INDEX_NAME , * st_index_type. as_ref ( ) . expect ( "should be ref" ) )
@@ -474,6 +498,7 @@ fn system_module_def() -> ModuleDef {
474498 validate_system_table :: < StViewFields > ( & result, ST_VIEW_NAME ) ;
475499 validate_system_table :: < StViewParamFields > ( & result, ST_VIEW_PARAM_NAME ) ;
476500 validate_system_table :: < StViewColumnFields > ( & result, ST_VIEW_COLUMN_NAME ) ;
501+ validate_system_table :: < StViewReadsTableFields > ( & result, ST_VIEW_READS_TABLE_NAME ) ;
477502
478503 result
479504}
@@ -539,6 +564,7 @@ lazy_static::lazy_static! {
539564 m. insert( "st_view_view_name_idx_btree" , IndexId ( 15 ) ) ;
540565 m. insert( "st_view_param_view_id_param_pos_idx_btree" , IndexId ( 16 ) ) ;
541566 m. insert( "st_view_column_view_id_col_pos_idx_btree" , IndexId ( 17 ) ) ;
567+ m. insert( "st_view_reads_table_view_id_sender_table_id_idx_btree" , IndexId ( 18 ) ) ;
542568 m
543569 } ;
544570}
@@ -666,6 +692,10 @@ pub fn st_view_column_schema() -> TableSchema {
666692 st_schema ( ST_VIEW_COLUMN_NAME , ST_VIEW_COLUMN_ID )
667693}
668694
695+ pub fn st_view_reads_table_schema ( ) -> TableSchema {
696+ st_schema ( ST_VIEW_READS_TABLE_NAME , ST_VIEW_READS_TABLE_ID )
697+ }
698+
669699/// If `table_id` refers to a known system table, return its schema.
670700///
671701/// Used when restoring from a snapshot; system tables are reinstantiated with this schema,
@@ -688,6 +718,7 @@ pub(crate) fn system_table_schema(table_id: TableId) -> Option<TableSchema> {
688718 ST_VIEW_ID => Some ( st_view_schema ( ) ) ,
689719 ST_VIEW_PARAM_ID => Some ( st_view_param_schema ( ) ) ,
690720 ST_VIEW_COLUMN_ID => Some ( st_view_column_schema ( ) ) ,
721+ ST_VIEW_READS_TABLE_ID => Some ( st_view_reads_table_schema ( ) ) ,
691722 _ => None ,
692723 }
693724}
@@ -855,6 +886,20 @@ pub struct StViewParamRow {
855886 pub param_type : AlgebraicTypeViaBytes ,
856887}
857888
889+ /// System Table [ST_VIEW_READS_TABLE_NAME]
890+ ///
891+ /// | view_id | sender | table_id |
892+ /// |---------|----------------|----------|
893+ /// | 1 | (some = 0x...) | 4097 |
894+ #[ derive( Debug , Clone , PartialEq , Eq , SpacetimeType ) ]
895+ #[ sats( crate = spacetimedb_lib) ]
896+ pub struct StViewReadsTableRow {
897+ /// A foreign key referencing [`ST_VIEW_NAME`].
898+ pub view_id : ViewId ,
899+ pub sender : Option < Identity > ,
900+ pub table_id : TableId ,
901+ }
902+
858903/// System Table [ST_INDEX_NAME]
859904///
860905/// | index_id | table_id | index_name | index_algorithm |
0 commit comments