Skip to content

Commit c32f297

Browse files
authored
Update TableSchema & system tables to resemble ABI V9 (#1697)
1 parent a7e2321 commit c32f297

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2782
-2983
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bench/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ spacetimedb-client-api = { path = "../client-api" }
3737
spacetimedb-testing = { path = "../testing" }
3838
spacetimedb-primitives = { path = "../primitives" }
3939
spacetimedb-table = { path = "../table" }
40-
spacetimedb-schema.workspace = true
40+
spacetimedb-schema = { workspace = true, features = ["test"] }
4141

4242
anyhow.workspace = true
4343
anymap.workspace = true

crates/bench/benches/special.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ use spacetimedb_bench::{
66
schemas::{create_sequential, u32_u64_str, u32_u64_u64, u64_u64_u32, BenchTable, RandomTable},
77
spacetime_module::BENCHMARKS_MODULE,
88
};
9-
use spacetimedb_lib::db::raw_def::RawTableDefV8;
109
use spacetimedb_lib::{sats, ProductValue};
1110
use spacetimedb_schema::schema::TableSchema;
1211
use spacetimedb_testing::modules::start_runtime;
13-
use std::hint::black_box;
1412
use std::time::{Duration, Instant};
13+
use std::{hint::black_box, sync::Arc};
1514

1615
#[global_allocator]
1716
static GLOBAL: MiMalloc = MiMalloc;
@@ -155,9 +154,10 @@ fn serialize_benchmarks<
155154
);
156155
});
157156

158-
#[allow(deprecated)]
157+
let mut table_schema = TableSchema::from_product_type(T::product_type());
158+
table_schema.table_name = name.into();
159159
let mut table = spacetimedb_table::table::Table::new(
160-
TableSchema::from_def(0.into(), RawTableDefV8::from_product(name, T::product_type().clone())).into(),
160+
Arc::new(table_schema),
161161
spacetimedb_table::indexes::SquashedOffset::COMMITTED_STATE,
162162
);
163163
let mut blob_store = spacetimedb_table::blob_store::HashMapBlobStore::default();

crates/bench/src/spacetime_module.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ impl BenchDatabase for SpacetimeModule {
8282
.await
8383
});
8484

85-
for thing in module.client.module.catalog().iter() {
86-
log::trace!("SPACETIME_MODULE: LOADED: {} {:?}", thing.0, thing.1.ty());
85+
for table in module.client.module.info.module_def.tables() {
86+
log::trace!("SPACETIME_MODULE: LOADED TABLE: {:?}", table);
87+
}
88+
for reducer in module.client.module.info.module_def.reducers() {
89+
log::trace!("SPACETIME_MODULE: LOADED REDUCER: {:?}", reducer);
8790
}
8891
Ok(SpacetimeModule {
8992
runtime,

crates/bench/src/spacetime_raw.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ use crate::{
55
};
66
use spacetimedb::db::relational_db::{tests_utils::TestDB, RelationalDB};
77
use spacetimedb::execution_context::ExecutionContext;
8-
use spacetimedb_lib::db::raw_def::{RawIndexDefV8, RawTableDefV8};
98
use spacetimedb_lib::sats::AlgebraicValue;
10-
use spacetimedb_primitives::{ColId, TableId};
9+
use spacetimedb_primitives::{ColId, IndexId, TableId};
10+
use spacetimedb_schema::{
11+
def::{BTreeAlgorithm, IndexAlgorithm},
12+
schema::{IndexSchema, TableSchema},
13+
};
1114
use std::hint::black_box;
1215
use tempdir::TempDir;
1316

@@ -38,21 +41,39 @@ impl BenchDatabase for SpacetimeRaw {
3841
fn create_table<T: BenchTable>(&mut self, index_strategy: IndexStrategy) -> ResultBench<Self::TableId> {
3942
let name = table_name::<T>(index_strategy);
4043
self.db.with_auto_commit(&ExecutionContext::default(), |tx| {
41-
let table_def = RawTableDefV8::from_product(&name, T::product_type());
42-
let table_id = self.db.create_table(tx, table_def)?;
44+
let mut table_schema = TableSchema::from_product_type(T::product_type());
45+
table_schema.table_name = name.clone().into();
46+
let table_id = self.db.create_table(tx, table_schema)?;
4347
self.db.rename_table(tx, table_id, &name)?;
4448
match index_strategy {
4549
IndexStrategy::Unique0 => {
46-
self.db
47-
.create_index(tx, table_id, RawIndexDefV8::btree("id".into(), ColId(0), true))?;
50+
self.db.create_index(
51+
tx,
52+
IndexSchema {
53+
index_id: IndexId::SENTINEL,
54+
table_id,
55+
index_name: "id".into(),
56+
index_algorithm: IndexAlgorithm::BTree(BTreeAlgorithm {
57+
columns: ColId(0).into(),
58+
}),
59+
},
60+
true,
61+
)?;
4862
}
4963
IndexStrategy::NoIndex => (),
5064
IndexStrategy::BTreeEachColumn => {
5165
for (i, column) in T::product_type().elements.iter().enumerate() {
5266
self.db.create_index(
5367
tx,
54-
table_id,
55-
RawIndexDefV8::btree(column.name.clone().unwrap(), i, false),
68+
IndexSchema {
69+
index_id: IndexId::SENTINEL,
70+
table_id,
71+
index_name: column.name.clone().unwrap(),
72+
index_algorithm: IndexAlgorithm::BTree(BTreeAlgorithm {
73+
columns: ColId(i as _).into(),
74+
}),
75+
},
76+
false,
5677
)?;
5778
}
5879
}

crates/cli/src/api.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use reqwest::{header, Client, RequestBuilder};
22
use serde::Deserialize;
33
use serde_json::value::RawValue;
44

5+
use spacetimedb_lib::db::raw_def::v9::RawModuleDefV9;
56
use spacetimedb_lib::de::serde::DeserializeWrapper;
67
use spacetimedb_lib::sats::ProductType;
7-
use spacetimedb_lib::{Address, RawModuleDefV8};
8+
use spacetimedb_lib::Address;
89

910
static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
1011

@@ -50,7 +51,7 @@ impl ClientApi {
5051
}
5152

5253
/// Reads the `ModuleDef` from the `schema` endpoint.
53-
pub async fn module_def(&self) -> anyhow::Result<RawModuleDefV8> {
54+
pub async fn module_def(&self) -> anyhow::Result<RawModuleDefV9> {
5455
let res = self
5556
.client
5657
.get(self.con.db_uri("schema"))

crates/cli/src/subcommands/generate/csharp.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use std::ops::Deref;
66

77
use convert_case::{Case, Casing};
88
use spacetimedb_lib::sats::{AlgebraicType, AlgebraicTypeRef, ArrayType, ProductType, SumType};
9-
use spacetimedb_lib::{ReducerDef, TableDesc};
9+
use spacetimedb_lib::ReducerDef;
1010
use spacetimedb_primitives::ColList;
1111
use spacetimedb_schema::schema::TableSchema;
1212

1313
use super::code_indenter::CodeIndenter;
14-
use super::{GenCtx, GenItem};
14+
use super::{GenCtx, GenItem, TableDescHack};
1515

1616
fn scalar_or_string_name(b: &AlgebraicType) -> Option<&str> {
1717
Some(match b {
@@ -279,17 +279,13 @@ pub fn autogen_csharp_tuple(ctx: &GenCtx, name: &str, tuple: &ProductType, names
279279
}
280280

281281
#[allow(deprecated)]
282-
pub fn autogen_csharp_table(ctx: &GenCtx, table: &TableDesc, namespace: &str) -> String {
282+
pub fn autogen_csharp_table(ctx: &GenCtx, table: &TableDescHack, namespace: &str) -> String {
283283
let tuple = ctx.typespace[table.data].as_product().unwrap();
284284
autogen_csharp_product_table_common(
285285
ctx,
286286
csharp_typename(ctx, table.data),
287287
tuple,
288-
Some(
289-
TableSchema::from_def(0.into(), table.schema.clone())
290-
.validated()
291-
.expect("Failed to generate table due to validation errors"),
292-
),
288+
Some(table.schema.clone()),
293289
namespace,
294290
)
295291
}
@@ -317,7 +313,7 @@ fn autogen_csharp_product_table_common(
317313
write!(
318314
output,
319315
" : SpacetimeDB.{parent}<{name}, {namespace}.ReducerEvent>",
320-
parent = if schema.pk().is_some() {
316+
parent = if schema.primary_key.is_some() {
321317
"DatabaseTableWithPrimaryKey"
322318
} else {
323319
"DatabaseTable"
@@ -383,7 +379,7 @@ fn autogen_csharp_product_table_common(
383379

384380
// If this is a table, we want to generate event accessor and indexes
385381
if let Some(schema) = &schema {
386-
let constraints = schema.column_constraints();
382+
let constraints = schema.backcompat_column_constraints();
387383
let mut unique_indexes = Vec::new();
388384
// Declare custom index dictionaries
389385
for col in schema.columns() {
@@ -450,7 +446,7 @@ fn autogen_csharp_access_funcs_for_struct(
450446
_table_name: &str,
451447
schema: &TableSchema,
452448
) {
453-
let constraints = schema.column_constraints();
449+
let constraints = schema.backcompat_column_constraints();
454450
for col in schema.columns() {
455451
let is_unique = constraints[&ColList::new(col.col_pos)].has_unique();
456452

crates/cli/src/subcommands/generate/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ use spacetimedb::host::wasmtime::{Mem, MemView, WasmPointee as _};
1010
use spacetimedb_data_structures::map::HashSet;
1111
use spacetimedb_lib::de::serde::DeserializeWrapper;
1212
use spacetimedb_lib::sats::{AlgebraicType, AlgebraicTypeRef, Typespace};
13-
use spacetimedb_lib::{bsatn, RawModuleDefV8, TableDesc, TypeAlias};
13+
use spacetimedb_lib::{bsatn, RawModuleDefV8, TypeAlias};
1414
use spacetimedb_lib::{RawModuleDef, MODULE_ABI_MAJOR_VERSION};
1515
use spacetimedb_primitives::errno;
1616
use spacetimedb_schema;
1717
use spacetimedb_schema::def::{ModuleDef, ReducerDef, ScopedTypeName, TableDef, TypeDef};
1818
use spacetimedb_schema::identifier::Identifier;
19-
use spacetimedb_schema::schema::TableSchema;
19+
use spacetimedb_schema::schema::{Schema, TableSchema};
2020
use std::fs;
2121
use std::ops::Deref;
2222
use std::path::{Path, PathBuf};
@@ -254,8 +254,8 @@ pub fn generate(module: RawModuleDef, lang: Language, namespace: &str) -> anyhow
254254
let tableset = module.tables().map(|t| t.product_type_ref).collect::<HashSet<_>>();
255255
let tables = module
256256
.tables()
257-
.map(|table| TableDesc {
258-
schema: TableSchema::from_module_def(table, 0.into()).into(),
257+
.map(|table| TableDescHack {
258+
schema: TableSchema::from_module_def(&module, table, (), 0.into()),
259259
data: table.product_type_ref,
260260
})
261261
.sorted_by(|a, b| a.schema.table_name.cmp(&b.schema.table_name));
@@ -337,8 +337,14 @@ trait Lang {
337337
fn generate_globals(&self, module: &ModuleDef, namespace: &str) -> Vec<(String, String)>;
338338
}
339339

340+
/// Backwards-compatibible imitation of `TableDesc` that should be removed once the generators are updated to rely on `ModuleDef`.
341+
pub struct TableDescHack {
342+
schema: TableSchema,
343+
data: AlgebraicTypeRef,
344+
}
345+
340346
pub enum GenItem {
341-
Table(TableDesc),
347+
Table(TableDescHack),
342348
TypeAlias(TypeAlias),
343349
Reducer(spacetimedb_lib::ReducerDef),
344350
}

crates/cli/src/subcommands/generate/rust.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use spacetimedb_lib::sats::AlgebraicTypeRef;
55
use spacetimedb_primitives::ColList;
66
use spacetimedb_schema::def::{ModuleDef, ReducerDef, ScopedTypeName, TableDef, TypeDef};
77
use spacetimedb_schema::identifier::Identifier;
8-
use spacetimedb_schema::schema::TableSchema;
8+
use spacetimedb_schema::schema::{Schema, TableSchema};
99
use spacetimedb_schema::type_for_generate::{
1010
AlgebraicTypeDef, AlgebraicTypeUse, PlainEnumTypeDef, PrimitiveType, ProductTypeDef, SumTypeDef,
1111
};
@@ -304,7 +304,7 @@ pub fn autogen_rust_table(module: &ModuleDef, table: &TableDef) -> String {
304304

305305
out.newline();
306306

307-
let table = TableSchema::from_module_def(table, 0.into())
307+
let table = TableSchema::from_module_def(module, table, (), 0.into())
308308
.validated()
309309
.expect("Failed to generate table due to validation errors");
310310
print_impl_tabletype(module, out, &type_name, product_def, &table);
@@ -430,7 +430,7 @@ fn print_table_filter_methods(
430430
table: &TableSchema,
431431
) {
432432
write!(out, "impl {table_type_name} ");
433-
let constraints = table.column_constraints();
433+
let constraints = table.backcompat_column_constraints();
434434
out.delimited_block(
435435
"{",
436436
|out| {
@@ -796,8 +796,8 @@ fn print_handle_table_update_defn(module: &ModuleDef, out: &mut Indenter) {
796796
out.delimited_block(
797797
"match table_name {",
798798
|out| {
799-
for table_desc in iter_tables(module) {
800-
let table = TableSchema::from_module_def(table_desc, 0.into()).validated().unwrap();
799+
for table_def in iter_tables(module) {
800+
let table = TableSchema::from_module_def(module, table_def, (), 0.into()).validated().unwrap();
801801
writeln!(
802802
out,
803803
"{:?} => client_cache.{}::<{}::{}>(callbacks, table_update),",
@@ -807,8 +807,8 @@ fn print_handle_table_update_defn(module: &ModuleDef, out: &mut Indenter) {
807807
} else {
808808
"handle_table_update_no_primary_key"
809809
},
810-
type_name(module, table_desc.product_type_ref).to_case(Case::Snake),
811-
type_name(module, table_desc.product_type_ref).to_case(Case::Pascal),
810+
type_name(module, table_def.product_type_ref).to_case(Case::Snake),
811+
type_name(module, table_def.product_type_ref).to_case(Case::Pascal),
812812
);
813813
}
814814
writeln!(

crates/cli/src/subcommands/generate/typescript.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use spacetimedb_lib::sats::product_type::IDENTITY_TAG;
88
use spacetimedb_lib::sats::{
99
AlgebraicType, AlgebraicTypeRef, ArrayType, ProductType, ProductTypeElement, SumType, SumTypeVariant,
1010
};
11-
use spacetimedb_lib::{ReducerDef, TableDesc};
11+
use spacetimedb_lib::ReducerDef;
1212
use spacetimedb_primitives::ColList;
1313
use spacetimedb_schema::schema::TableSchema;
1414

1515
use super::code_indenter::CodeIndenter;
16-
use super::{GenCtx, GenItem, INDENT};
16+
use super::{GenCtx, GenItem, TableDescHack, INDENT};
1717

1818
fn scalar_or_string_to_ts(ty: &AlgebraicType) -> Option<(&str, &str)> {
1919
Some(match ty {
@@ -499,13 +499,13 @@ pub fn autogen_typescript_tuple(ctx: &GenCtx, name: &str, tuple: &ProductType) -
499499
autogen_typescript_product_table_common(ctx, name, tuple, None)
500500
}
501501
#[allow(deprecated)]
502-
pub fn autogen_typescript_table(ctx: &GenCtx, table: &TableDesc) -> String {
502+
pub fn autogen_typescript_table(ctx: &GenCtx, table: &TableDescHack) -> String {
503503
let tuple = ctx.typespace[table.data].as_product().unwrap();
504504
autogen_typescript_product_table_common(
505505
ctx,
506506
typescript_typename(ctx, table.data),
507507
tuple,
508-
Some(TableSchema::from_def(0.into(), table.schema.clone())),
508+
Some(table.schema.clone()),
509509
)
510510
}
511511

@@ -767,7 +767,7 @@ fn autogen_typescript_access_funcs_for_struct(
767767
table_name: &str,
768768
table: &TableSchema,
769769
) {
770-
let constraints = table.column_constraints();
770+
let constraints = table.backcompat_column_constraints();
771771
for col in table.columns() {
772772
let is_unique = constraints[&ColList::new(col.col_pos)].has_unique();
773773
let field = &product_type.elements[col.col_pos.idx()];

0 commit comments

Comments
 (0)