Skip to content

Commit 2be4215

Browse files
authored
Allow converting new ModuleDef to old TableSchema (#1630)
1 parent 98faf7d commit 2be4215

File tree

22 files changed

+594
-100
lines changed

22 files changed

+594
-100
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/benches/special.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ fn serialize_benchmarks<
155155
);
156156
});
157157

158+
#[allow(deprecated)]
158159
let mut table = spacetimedb_table::table::Table::new(
159160
TableSchema::from_def(0.into(), RawTableDefV8::from_product(name, T::product_type().clone())).into(),
160161
spacetimedb_table::indexes::SquashedOffset::COMMITTED_STATE,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ pub fn autogen_csharp_tuple(ctx: &GenCtx, name: &str, tuple: &ProductType, names
267267
autogen_csharp_product_table_common(ctx, name, tuple, None, namespace)
268268
}
269269

270+
#[allow(deprecated)]
270271
pub fn autogen_csharp_table(ctx: &GenCtx, table: &TableDesc, namespace: &str) -> String {
271272
let tuple = ctx.typespace[table.data].as_product().unwrap();
272273
autogen_csharp_product_table_common(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ fn find_product_type(ctx: &GenCtx, ty: AlgebraicTypeRef) -> &ProductType {
324324

325325
/// Generate a file which defines a `struct` corresponding to the `table`'s `ProductType`,
326326
/// and implements `spacetimedb_sdk::table::TableType` for it.
327+
#[allow(deprecated)]
327328
pub fn autogen_rust_table(ctx: &GenCtx, table: &TableDesc) -> String {
328329
let mut output = CodeIndenter::new(String::new());
329330
let out = &mut output;
@@ -847,6 +848,7 @@ fn print_spacetime_module_struct_defn(ctx: &GenCtx, out: &mut Indenter, items: &
847848
/// Define the `handle_table_update` method,
848849
/// which dispatches on the table name in a `TableUpdate` message
849850
/// to call an appropriate method on the `ClientCache`.
851+
#[allow(deprecated)]
850852
fn print_handle_table_update_defn(_ctx: &GenCtx, out: &mut Indenter, items: &[GenItem]) {
851853
out.delimited_block(
852854
"fn handle_table_update(&self, table_update: TableUpdate, client_cache: &mut ClientCache, callbacks: &mut RowCallbackReminders) {",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ fn typescript_field_name(field_name: String) -> String {
498498
pub fn autogen_typescript_tuple(ctx: &GenCtx, name: &str, tuple: &ProductType) -> String {
499499
autogen_typescript_product_table_common(ctx, name, tuple, None)
500500
}
501+
#[allow(deprecated)]
501502
pub fn autogen_typescript_table(ctx: &GenCtx, table: &TableDesc) -> String {
502503
let tuple = ctx.typespace[table.data].as_product().unwrap();
503504
autogen_typescript_product_table_common(

crates/core/src/db/datastore/locking_tx_datastore/mut_tx.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl MutTxId {
8484
return Err(TableError::System(table_schema.table_name.clone()).into());
8585
}
8686

87+
#[allow(deprecated)]
8788
TableSchema::from_def(0.into(), table_schema.clone())
8889
.validated()
8990
.map_err(|err| DBError::Schema(SchemaErrors(err)))?;
@@ -111,6 +112,7 @@ impl MutTxId {
111112
.read_col(StTableFields::TableId)?;
112113

113114
// Generate the full definition of the table, with the generated indexes, constraints, sequences...
115+
#[allow(deprecated)]
114116
let table_schema = TableSchema::from_def(table_id, table_schema);
115117

116118
// Insert the columns into `st_columns`
@@ -346,6 +348,7 @@ impl MutTxId {
346348
.read_col(StIndexFields::IndexId)?;
347349

348350
// Construct the index schema.
351+
#[allow(deprecated)]
349352
let mut index = IndexSchema::from_def(table_id, index.clone());
350353
index.index_id = index_id;
351354

@@ -637,6 +640,7 @@ impl MutTxId {
637640
// TODO: Can we return early here?
638641

639642
let (table, ..) = self.get_or_create_insert_table_mut(table_id)?;
643+
#[allow(deprecated)]
640644
let mut constraint = ConstraintSchema::from_def(table_id, constraint);
641645
constraint.constraint_id = constraint_id;
642646
// This won't clone-write when creating a table but likely to otherwise.

crates/core/src/db/datastore/system_tables.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ st_fields_enum!(enum StScheduledFields {
245245
/// |----------|-------------|----------- |------------- |
246246
/// | 4 | "customers" | "user" | "public" |
247247
fn st_table_schema() -> TableSchema {
248+
#[allow(deprecated)]
248249
TableSchema::from_def(
249250
ST_TABLE_ID,
250251
RawTableDefV8::new(
@@ -268,6 +269,7 @@ fn st_table_schema() -> TableSchema {
268269
/// |----------|---------|----------|--------------------|
269270
/// | 1 | 0 | "id" | AlgebraicType::U32 |
270271
fn st_column_schema() -> TableSchema {
272+
#[allow(deprecated)]
271273
TableSchema::from_def(
272274
ST_COLUMN_ID,
273275
RawTableDefV8::new(
@@ -293,6 +295,7 @@ fn st_column_schema() -> TableSchema {
293295
/// |----------|----------|-------------|---------|-----------|------------|
294296
/// | 1 | | "ix_sample" | [1] | false | "btree" |
295297
fn st_index_schema() -> TableSchema {
298+
#[allow(deprecated)]
296299
TableSchema::from_def(
297300
ST_INDEX_ID,
298301
RawTableDefV8::new(
@@ -318,6 +321,7 @@ fn st_index_schema() -> TableSchema {
318321
/// |-------------|-------------------|-----------|-------|-----------|-----------|----------|--------|-----------|
319322
/// | 1 | "seq_customer_id" | 1 | 100 | 10 | 1200 | 1 | 1 | 200 |
320323
fn st_sequence_schema() -> TableSchema {
324+
#[allow(deprecated)]
321325
TableSchema::from_def(
322326
ST_SEQUENCE_ID,
323327
RawTableDefV8::new(
@@ -346,6 +350,7 @@ fn st_sequence_schema() -> TableSchema {
346350
/// |---------------|-------------------- -|-------------|-------|------------|
347351
/// | 1 | "unique_customer_id" | 1 | 100 | [1, 4] |
348352
fn st_constraint_schema() -> TableSchema {
353+
#[allow(deprecated)]
349354
TableSchema::from_def(
350355
ST_CONSTRAINT_ID,
351356
RawTableDefV8::new(
@@ -381,6 +386,7 @@ fn st_constraint_schema() -> TableSchema {
381386
/// |------------------|----------------|---------------|---------------|---------------------|
382387
/// | <bytes> | <bytes> | 0 | <bytes> | <bytes> |
383388
pub(crate) fn st_module_schema() -> TableSchema {
389+
#[allow(deprecated)]
384390
TableSchema::from_def(
385391
ST_MODULE_ID,
386392
RawTableDefV8::new(
@@ -403,6 +409,7 @@ pub(crate) fn st_module_schema() -> TableSchema {
403409
// -----------------------------------------------------------------------------------------+--------------------------------------------------------
404410
// (__identity_bytes = 0x7452047061ea2502003412941d85a42f89b0702588b823ab55fc4f12e9ea8363) | (__address_bytes = 0x6bdea3ab517f5857dc9b1b5fe99e1b14)
405411
fn st_client_schema() -> TableSchema {
412+
#[allow(deprecated)]
406413
TableSchema::from_def(
407414
ST_CLIENT_ID,
408415
RawTableDefV8::new(
@@ -418,6 +425,7 @@ fn st_client_schema() -> TableSchema {
418425
}
419426

420427
fn st_scheduled_schema() -> TableSchema {
428+
#[allow(deprecated)]
421429
TableSchema::from_def(
422430
ST_SCHEDULED_ID,
423431
RawTableDefV8::new(
@@ -438,6 +446,7 @@ fn st_scheduled_schema() -> TableSchema {
438446
/// |-------------|-----------|
439447
/// | "row_limit" | (U64 = 5) |
440448
pub fn st_var_schema() -> TableSchema {
449+
#[allow(deprecated)]
441450
TableSchema::from_def(
442451
ST_VAR_ID,
443452
RawTableDefV8::new(

crates/core/src/db/update.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ pub fn schema_updates(
213213
known_schema.table_access,
214214
known_schema.scheduled.clone(),
215215
);
216+
#[allow(deprecated)]
216217
let proposed_schema = TableSchema::from_def(known_schema.table_id, proposed_schema_def);
217218

218219
let (schema_is_incompatible, known_schema, proposed_schema) =
@@ -438,6 +439,7 @@ mod tests {
438439

439440
#[test]
440441
fn test_updates_schema_mismatch() {
442+
#[allow(deprecated)]
441443
let current = [Arc::new(TableSchema::from_def(
442444
42.into(),
443445
RawTableDefV8::new(
@@ -468,6 +470,7 @@ mod tests {
468470

469471
#[test]
470472
fn test_updates_orphaned_table() {
473+
#[allow(deprecated)]
471474
let current = [Arc::new(TableSchema::from_def(
472475
42.into(),
473476
RawTableDefV8::new(
@@ -498,6 +501,7 @@ mod tests {
498501
col_type: AlgebraicType::String,
499502
}],
500503
);
504+
#[allow(deprecated)]
501505
let current = [Arc::new(TableSchema::from_def(42.into(), table_def.clone()))];
502506
let proposed = vec![table_def.with_column_index(ColId(0), false)];
503507

@@ -548,6 +552,7 @@ mod tests {
548552

549553
#[test]
550554
fn test_updates_add_constraint() {
555+
#[allow(deprecated)]
551556
let current = [Arc::new(TableSchema::from_def(
552557
42.into(),
553558
RawTableDefV8::new(
@@ -572,6 +577,7 @@ mod tests {
572577

573578
#[test]
574579
fn test_updates_drop_constraint() {
580+
#[allow(deprecated)]
575581
let current = [Arc::new(TableSchema::from_def(
576582
42.into(),
577583
RawTableDefV8::new(

crates/lib/src/db/raw_def/v9.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,34 +408,30 @@ impl RawModuleDefV9Builder {
408408
}
409409

410410
/// Build a new table with a product type.
411-
///
412-
/// This is a convenience method for tests, since in real modules, the product type is initialized via the `SpacetimeType` trait.
413-
#[cfg(feature = "test")]
414-
pub fn build_table_for_tests(
411+
/// Adds the type to the module.
412+
pub fn build_table_with_new_type(
415413
&mut self,
416414
table_name: impl Into<RawIdentifier>,
417415
product_type: spacetimedb_sats::ProductType,
418416
custom_ordering: bool,
419417
) -> RawTableDefBuilder {
420418
let table_name = table_name.into();
421-
let product_type_ref = self.add_type_for_tests([], table_name.clone(), product_type.into(), custom_ordering);
419+
let product_type_ref = self.add_algebraic_type([], table_name.clone(), product_type.into(), custom_ordering);
422420

423421
self.build_table(table_name, product_type_ref)
424422
}
425423

426424
/// Add a type to the typespace, along with a type alias declaring its name.
425+
/// This method should only be use for `AlgebraicType`s not corresponding to a Rust
426+
/// type that implements `SpacetimeType`.
427427
///
428428
/// Returns a reference to the newly-added type.
429429
///
430430
/// NOT idempotent, calling this twice with the same name will cause errors during
431431
/// validation.
432432
///
433433
/// You must set `custom_ordering` if you're not using the default element ordering.
434-
///
435-
/// This is a convenience method for tests, since in real modules, types are added to the
436-
/// typespace via the `SpacetimeType` trait.
437-
#[cfg(feature = "test")]
438-
pub fn add_type_for_tests(
434+
pub fn add_algebraic_type(
439435
&mut self,
440436
scope: impl IntoIterator<Item = RawIdentifier>,
441437
name: impl Into<RawIdentifier>,

crates/primitives/src/attr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ impl Constraints {
211211
Self::new(self.attr | other.attr)
212212
}
213213

214+
/// Add auto-increment constraint to the existing constraints.
215+
/// Returns Err if the result would not be valid.
216+
#[allow(clippy::result_unit_err)]
217+
pub fn push_auto_inc(self) -> Result<Self, ()> {
218+
Self::try_from(self.attr | ColumnAttribute::AUTO_INC)
219+
}
220+
214221
/// Returns the bits representing the constraints.
215222
pub const fn bits(&self) -> u8 {
216223
self.attr.bits()

0 commit comments

Comments
 (0)