Skip to content

Disable column type changes in an automigration #3178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/core/src/db/relational_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,7 @@ impl RelationalDB {
Ok(self.inner.alter_table_access_mut_tx(tx, name, access)?)
}

#[allow(unused)]
pub(crate) fn alter_table_row_type(
&self,
tx: &mut MutTx,
Expand Down
12 changes: 3 additions & 9 deletions crates/core/src/db/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use spacetimedb_lib::AlgebraicValue;
use spacetimedb_primitives::{ColSet, TableId};
use spacetimedb_schema::auto_migrate::{AutoMigratePlan, ManualMigratePlan, MigratePlan};
use spacetimedb_schema::def::TableDef;
use spacetimedb_schema::schema::{column_schemas_from_defs, IndexSchema, Schema, SequenceSchema, TableSchema};
use spacetimedb_schema::schema::{IndexSchema, Schema, SequenceSchema, TableSchema};
use std::sync::Arc;

/// The logger used for by [`update_database`] and friends.
Expand Down Expand Up @@ -222,14 +222,8 @@ fn auto_migrate_database(
);
stdb.drop_sequence(tx, sequence_schema.sequence_id)?;
}
spacetimedb_schema::auto_migrate::AutoMigrateStep::ChangeColumns(table_name) => {
let table_def = plan.new.stored_in_table_def(table_name).unwrap();
let table_id = stdb.table_id_from_name_mut(tx, table_name).unwrap().unwrap();
let column_schemas = column_schemas_from_defs(plan.new, &table_def.columns, table_id);

log!(logger, "Changing columns of table `{}`", table_name);

stdb.alter_table_row_type(tx, table_id, column_schemas)?;
spacetimedb_schema::auto_migrate::AutoMigrateStep::ChangeColumns(_table_name) => {
anyhow::bail!("Unsupported: Changing column types");
}
spacetimedb_schema::auto_migrate::AutoMigrateStep::ChangeAccess(table_name) => {
let table_def = plan.new.stored_in_table_def(table_name).unwrap();
Expand Down
72 changes: 16 additions & 56 deletions smoketests/tests/auto_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@


class AddTableAutoMigration(Smoketest):
MODULE_CODE_INIT = """
MODULE_CODE = """
use spacetimedb::{log, ReducerContext, Table, SpacetimeType};
use PersonKind::*;

#[spacetimedb::table(name = person, public)]
pub struct Person {
name: String,
kind: PersonKind,
}

#[spacetimedb::reducer]
pub fn add_person(ctx: &ReducerContext, name: String, kind: String) {
let kind = kind_from_string(kind);
ctx.db.person().insert(Person { name, kind });
pub fn add_person(ctx: &ReducerContext, name: String) {
ctx.db.person().insert(Person { name });
}

#[spacetimedb::reducer]
pub fn print_persons(ctx: &ReducerContext, prefix: String) {
for person in ctx.db.person().iter() {
let kind = kind_to_string(person.kind);
log::info!("{prefix}: {} - {kind}", person.name);
log::info!("{}: {}", prefix, person.name);
}
}

Expand All @@ -43,47 +39,11 @@ class AddTableAutoMigration(Smoketest):

#[spacetimedb::client_visibility_filter]
const PERSON_VISIBLE: spacetimedb::Filter = spacetimedb::Filter::Sql("SELECT * FROM person");
"""

MODULE_CODE = MODULE_CODE_INIT + """
#[derive(SpacetimeType, Clone, Copy, PartialEq, Eq)]
pub enum PersonKind {
Student,
}

fn kind_from_string(_: String) -> PersonKind {
Student
}

fn kind_to_string(Student: PersonKind) -> &'static str {
"Student"
}
"""

MODULE_CODE_UPDATED = (
MODULE_CODE_INIT
MODULE_CODE
+ """
#[derive(SpacetimeType, Clone, Copy, PartialEq, Eq)]
pub enum PersonKind {
Student,
Professor,
}

fn kind_from_string(kind: String) -> PersonKind {
match &*kind {
"Student" => Student,
"Professor" => Professor,
_ => panic!(),
}
}

fn kind_to_string(kind: PersonKind) -> &'static str {
match kind {
Student => "Student",
Professor => "Professor",
}
}

#[spacetimedb::table(name = book, public)]
pub struct Book {
isbn: String,
Expand Down Expand Up @@ -129,14 +89,14 @@ def test_add_table_auto_migration(self):
logging.info("Initial publish complete")
# initial module code is already published by test framework

self.call("add_person", "Robert", "Student")
self.call("add_person", "Julie", "Student")
self.call("add_person", "Samantha", "Student")
self.call("add_person", "Robert")
self.call("add_person", "Julie")
self.call("add_person", "Samantha")
self.call("print_persons", "BEFORE")
logs = self.logs(100)
self.assertIn("BEFORE: Samantha - Student", logs)
self.assertIn("BEFORE: Julie - Student", logs)
self.assertIn("BEFORE: Robert - Student", logs)
self.assertIn("BEFORE: Samantha", logs)
self.assertIn("BEFORE: Julie", logs)
self.assertIn("BEFORE: Robert", logs)

logging.info(
"Initial operations complete, updating module without clear",
Expand All @@ -160,16 +120,16 @@ def test_add_table_auto_migration(self):

self.logs(100)

self.call("add_person", "Husserl", "Professor")
self.call("add_person", "Husserl")
self.call("add_book", "1234567890")
self.call("print_persons", "AFTER_PERSON")
self.call("print_books", "AFTER_BOOK")

logs = self.logs(100)
self.assertIn("AFTER_PERSON: Samantha - Student", logs)
self.assertIn("AFTER_PERSON: Julie - Student", logs)
self.assertIn("AFTER_PERSON: Robert - Student", logs)
self.assertIn("AFTER_PERSON: Husserl - Professor", logs)
self.assertIn("AFTER_PERSON: Samantha", logs)
self.assertIn("AFTER_PERSON: Julie", logs)
self.assertIn("AFTER_PERSON: Robert", logs)
self.assertIn("AFTER_PERSON: Husserl", logs)
self.assertIn("AFTER_BOOK: 1234567890", logs)


Expand Down
Loading