MVCC sync pull writes values into the wrong columns after DROP COLUMN or RENAME COLUMN
A logical pull from an MVCC database changes each ALTER TABLE into a table refresh. The refresh contains the new CREATE TABLE statement (append_schema_ops in sync/engine/src/database_sync_operations.rs). The replay of the refresh (execute_ddl_idempotent in sync/engine/src/database_replay_generator.rs) only adds the missing columns. It does not drop or rename columns.
Then the row replay puts the values of each record into the local columns by position. After a remote DROP COLUMN or RENAME COLUMN, the local columns are different from the remote columns. As a result, the replica writes values into the wrong columns and gives no error.
Replica table before the pull:
CREATE TABLE t (id TEXT PRIMARY KEY, a TEXT, b TEXT);
INSERT INTO t VALUES ('x', 'A1', 'B1');| Remote change | Replica row after the pull | Correct row |
|---|---|---|
ALTER TABLE t DROP COLUMN a; then UPDATE t SET b = 'B2'; |
id = x, a = B2, b = B1 |
id = x, b = B2 |
ALTER TABLE t RENAME COLUMN a TO a2; then UPDATE t SET a2 = 'A2'; |
id = x, a = A2, b = B1, a2 = NULL |
id = x, a2 = A2, b = B1 |
I did the test with a replay session on main (36da5b2e4). The test replays the refresh and the row upsert that the pull makes for each remote change. I did not do the test with a sync server.
tests module of sync/engine/src/database_tape.rs) #[test]
pub fn tmp_refresh_after_drop_or_rename_column() {
let cases = [
(
"CREATE TABLE t (id TEXT PRIMARY KEY, b TEXT)",
vec["x", "B2"],
),
(
"CREATE TABLE t (id TEXT PRIMARY KEY, a2 TEXT, b TEXT)",
vec["x", "A2", "B1"],
),
];
for (refresh_sql, after) in cases {
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path().to_str().unwrap();
let io: Arc<dyn turso_core::IO> = Arc::new(turso_core::PlatformIO::new().unwrap());
let db = turso_core::Database::open_file(io.clone(), db_path, Arc::new(SqliteDialect))
.unwrap();
let db = Arc::new(DatabaseTape::new(db));
let mut gen = genawaiter::sync::Gen::new({
let db = db.clone();
|coro| async move {
let coro: Coro<()> = coro.into();
let conn = db.connect(&coro).await.unwrap();
conn.execute("CREATE TABLE t (id TEXT PRIMARY KEY, a TEXT, b TEXT)")
.unwrap();
conn.execute("INSERT INTO t VALUES ('x', 'A1', 'B1')").unwrap();
let opts = DatabaseReplaySessionOpts {
use_implicit_rowid: true,
};
let mut session = db.start_replay_session(&coro, opts).await.unwrap();
let refresh = DatabaseSchemaReplay::Refresh {
kind: DatabaseSchemaKind::Table,
name: "t".to_string(),
sql: refresh_sql.to_string(),
};
session
.replay(&coro, DatabaseTapeOperation::SchemaReplay(refresh))
.await
.unwrap();
let mut values = crate::alloc::Vec::new();
for value in after {
values.push(turso_core::Value::from_text(value.to_string()));
}
let upsert = DatabaseTapeRowChange {
change_id: 0,
change_time: 0,
change: DatabaseTapeRowChangeType::Insert { after: values },
table_name: "t".to_string(),
id: 1,
};
session
.replay(&coro, DatabaseTapeOperation::RowChange(upsert))
.await
.unwrap();
session
.replay(&coro, DatabaseTapeOperation::Commit)
.await
.unwrap();
let mut stmt = conn.prepare("SELECT * FROM t").unwrap();
let values = {
let row = run_stmt_once(&coro, &mut stmt).await.unwrap().unwrap();
row.get_values().cloned().collect::<Vec<_>>()
};
let names = (0..values.len())
.map(|i| stmt.get_column_name(i).to_string())
.collect::<Vec<_>>();
println!("REPRO {refresh_sql}: {names:?} = {values:?}");
}
});
loop {
match gen.resume_with(Ok(())) {
genawaiter::GeneratorState::Yielded(..) => io.step().unwrap(),
genawaiter::GeneratorState::Complete(()) => break,
}
}
}
}Run it with cargo test -p turso_sync_engine --lib -- tmp_refresh_after_drop_or_rename_column --nocapture.
Source: tursodatabase/turso