Skip to content

Commit 05ba068

Browse files
Add st_view_reads_table system table
1 parent a952ba5 commit 05ba068

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

crates/datastore/src/locking_tx_datastore/committed_state.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::{
88
};
99
use crate::system_tables::{
1010
ST_CONNECTION_CREDENTIALS_ID, ST_CONNECTION_CREDENTIALS_IDX, ST_VIEW_COLUMN_ID, ST_VIEW_COLUMN_IDX, ST_VIEW_ID,
11-
ST_VIEW_IDX, ST_VIEW_PARAM_ID, ST_VIEW_PARAM_IDX,
11+
ST_VIEW_IDX, ST_VIEW_PARAM_ID, ST_VIEW_PARAM_IDX, ST_VIEW_READS_TABLE_ID, ST_VIEW_READS_TABLE_IDX,
1212
};
1313
use crate::{
1414
db_metrics::DB_METRICS,
@@ -259,6 +259,7 @@ impl CommittedState {
259259
self.create_table(ST_VIEW_ID, schemas[ST_VIEW_IDX].clone());
260260
self.create_table(ST_VIEW_PARAM_ID, schemas[ST_VIEW_PARAM_IDX].clone());
261261
self.create_table(ST_VIEW_COLUMN_ID, schemas[ST_VIEW_COLUMN_IDX].clone());
262+
self.create_table(ST_VIEW_READS_TABLE_ID, schemas[ST_VIEW_READS_TABLE_IDX].clone());
262263

263264
// Insert the sequences into `st_sequences`
264265
let (st_sequences, blob_store, pool) =

crates/datastore/src/locking_tx_datastore/datastore.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ mod tests {
12521252
ST_CONSTRAINT_NAME, ST_INDEX_ID, ST_INDEX_NAME, ST_MODULE_NAME, ST_RESERVED_SEQUENCE_RANGE,
12531253
ST_ROW_LEVEL_SECURITY_ID, ST_ROW_LEVEL_SECURITY_NAME, ST_SCHEDULED_ID, ST_SCHEDULED_NAME, ST_SEQUENCE_ID,
12541254
ST_SEQUENCE_NAME, ST_TABLE_NAME, ST_VAR_ID, ST_VAR_NAME, ST_VIEW_COLUMN_ID, ST_VIEW_COLUMN_NAME, ST_VIEW_ID,
1255-
ST_VIEW_NAME, ST_VIEW_PARAM_ID, ST_VIEW_PARAM_NAME,
1255+
ST_VIEW_NAME, ST_VIEW_PARAM_ID, ST_VIEW_PARAM_NAME, ST_VIEW_READS_TABLE_ID, ST_VIEW_READS_TABLE_NAME,
12561256
};
12571257
use crate::traits::{IsolationLevel, MutTx};
12581258
use crate::Result;
@@ -1708,6 +1708,7 @@ mod tests {
17081708
TableRow { id: ST_VIEW_ID.into(), name: ST_VIEW_NAME, ty: StTableType::System, access: StAccess::Public, primary_key: Some(StViewFields::ViewId.into()) },
17091709
TableRow { id: ST_VIEW_PARAM_ID.into(), name: ST_VIEW_PARAM_NAME, ty: StTableType::System, access: StAccess::Public, primary_key: None },
17101710
TableRow { id: ST_VIEW_COLUMN_ID.into(), name: ST_VIEW_COLUMN_NAME, ty: StTableType::System, access: StAccess::Public, primary_key: None },
1711+
TableRow { id: ST_VIEW_READS_TABLE_ID.into(), name: ST_VIEW_READS_TABLE_NAME, ty: StTableType::System, access: StAccess::Public, primary_key: None },
17111712

17121713
]));
17131714
#[rustfmt::skip]
@@ -1783,6 +1784,10 @@ mod tests {
17831784
ColRow { table: ST_VIEW_COLUMN_ID.into(), pos: 1, name: "col_pos", ty: ColId::get_type() },
17841785
ColRow { table: ST_VIEW_COLUMN_ID.into(), pos: 2, name: "col_name", ty: AlgebraicType::String },
17851786
ColRow { table: ST_VIEW_COLUMN_ID.into(), pos: 3, name: "col_type", ty: AlgebraicType::bytes() },
1787+
1788+
ColRow { table: ST_VIEW_READS_TABLE_ID.into(), pos: 0, name: "view_id", ty: ViewId::get_type() },
1789+
ColRow { table: ST_VIEW_READS_TABLE_ID.into(), pos: 1, name: "sender", ty: AlgebraicType::option(AlgebraicType::identity()) },
1790+
ColRow { table: ST_VIEW_READS_TABLE_ID.into(), pos: 2, name: "table_id", ty: TableId::get_type() },
17861791
]));
17871792
#[rustfmt::skip]
17881793
assert_eq!(query.scan_st_indexes()?, map_array([
@@ -1803,6 +1808,7 @@ mod tests {
18031808
IndexRow { id: 15, table: ST_VIEW_ID.into(), col: col(1), name: "st_view_view_name_idx_btree", },
18041809
IndexRow { id: 16, table: ST_VIEW_PARAM_ID.into(), col: col_list![0, 1], name: "st_view_param_view_id_param_pos_idx_btree", },
18051810
IndexRow { id: 17, table: ST_VIEW_COLUMN_ID.into(), col: col_list![0, 1], name: "st_view_column_view_id_col_pos_idx_btree", },
1811+
IndexRow { id: 18, table: ST_VIEW_READS_TABLE_ID.into(), col: col_list![0, 1, 2], name: "st_view_reads_table_view_id_sender_table_id_idx_btree", },
18061812
]));
18071813
let start = ST_RESERVED_SEQUENCE_RANGE as i128 + 1;
18081814
#[rustfmt::skip]
@@ -2258,6 +2264,7 @@ mod tests {
22582264
IndexRow { id: 15, table: ST_VIEW_ID.into(), col: col(1), name: "st_view_view_name_idx_btree", },
22592265
IndexRow { id: 16, table: ST_VIEW_PARAM_ID.into(), col: col_list![0, 1], name: "st_view_param_view_id_param_pos_idx_btree", },
22602266
IndexRow { id: 17, table: ST_VIEW_COLUMN_ID.into(), col: col_list![0, 1], name: "st_view_column_view_id_col_pos_idx_btree", },
2267+
IndexRow { id: 18, table: ST_VIEW_READS_TABLE_ID.into(), col: col_list![0, 1, 2], name: "st_view_reads_table_view_id_sender_table_id_idx_btree", },
22612268
IndexRow { id: seq_start, table: FIRST_NON_SYSTEM_ID, col: col(0), name: "Foo_id_idx_btree", },
22622269
IndexRow { id: seq_start + 1, table: FIRST_NON_SYSTEM_ID, col: col(1), name: "Foo_name_idx_btree", },
22632270
IndexRow { id: seq_start + 2, table: FIRST_NON_SYSTEM_ID, col: col(2), name: "Foo_age_idx_btree", },

crates/datastore/src/system_tables.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ pub const ST_VIEW_ID: TableId = TableId(12);
7171
pub const ST_VIEW_PARAM_ID: TableId = TableId(13);
7272
/// The static ID of the table that tracks view columns
7373
pub 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

7577
pub(crate) const ST_CONNECTION_CREDENTIALS_NAME: &str = "st_connection_credentials";
7678
pub const ST_TABLE_NAME: &str = "st_table";
@@ -86,6 +88,7 @@ pub(crate) const ST_ROW_LEVEL_SECURITY_NAME: &str = "st_row_level_security";
8688
pub(crate) const ST_VIEW_NAME: &str = "st_view";
8789
pub(crate) const ST_VIEW_PARAM_NAME: &str = "st_view_param";
8890
pub(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;
173177
pub(crate) const ST_VIEW_IDX: usize = 11;
174178
pub(crate) const ST_VIEW_PARAM_IDX: usize = 12;
175179
pub(crate) const ST_VIEW_COLUMN_IDX: usize = 13;
180+
pub(crate) const ST_VIEW_READS_TABLE_IDX: usize = 14;
176181

177182
macro_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.
242253
st_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

Comments
 (0)