#9430·turso

MVCC sync does not differentiate between swapped columns

Author: LeMikaelFCreated Sep 30, 2026Updated Sep 30, 2026
Labelsmvccsync

In MVCC sync, when a transaction on the remote DB swaps three columns, for example:

sql
BEGIN;
ALTER TABLE core RENAME COLUMN a TO temp;
ALTER TABLE core RENAME COLUMN b TO a;
ALTER TABLE core RENAME COLUMN temp TO b;
COMMIT;

The next pull doesn't know the columns were swapped, because it matches them by name, so it doesn't modify the local schema and the local table is permanently desynced with the remote. The root cause is that columns don't have a stable identity and are matched by name only.

I think addressing it would require adding metadata to keep track of the columns. This would also help with the unpleasant parsing and string matching we need to do in order to apply remote DDL locally.

This bug can't be reproduced locally because the dev sync server doesn't behave exactly like Turso Cloud (I didn't try to understand exactly why).

POC output:

== after bootstrap
[Text("x"), Text("A1"), Text("B1")]
== remote after swap
ID     A      B  
x      B1     A1     
== local after pull (should match remote)
[Text("x"), Text("A1"), Text("B1")]

POC script:

bash
#!/bin/sh
set -e

DB=swap-poc-$(openssl rand -hex 4)
turso db create "$DB" --tursodb --group test-canary 1>/dev/null
turso db shell "$DB" "CREATE TABLE core (id TEXT PRIMARY KEY, a TEXT, b TEXT)"
turso db shell "$DB" "INSERT INTO core VALUES ('x', 'A1', 'B1')"
export TURSO_URL=""
TURSO_URL="$(turso db show "$DB" --http-url)"
export TURSO_TOKEN=""
TURSO_TOKEN="$(turso db tokens create "$DB")"

DIR=$(mktemp -d)
trap 'rm -rf $DIR' EXIT
mkdir "$DIR/src"
cp Cargo.lock "$DIR/"
cat > "$DIR/Cargo.toml" <<EOF
[package]
name = "swap_poc"
version = "0.1.0"
edition = "2021"

[dependencies]
turso = { path = "$PWD/bindings/rust", features = ["sync"] }
tokio = { version = "1", features = ["full"] }

[workspace]
EOF
cat > "$DIR/src/main.rs" <<'EOF'
use turso::sync::Builder;

#[tokio::main]
async fn main() -> turso::Result<()> {
    let args: Vec<String> = std::env::args().collect();
    let db = Builder::new_remote(&args[2])
        .with_remote_url(std::env::var("TURSO_URL").unwrap())
        .with_auth_token(std::env::var("TURSO_TOKEN").unwrap())
        .build()
        .await?;
    if args[1] == "pull" {
        db.pull().await?;
    }
    let conn = db.connect().await?;
    let mut rows = conn.query("SELECT id, a, b FROM core", ()).await?;
    while let Some(row) = rows.next().await? {
        let values: Vec<_> = (0..row.column_count())
            .map(|i| row.get_value(i))
            .collect::<turso::Result<_>>()?;
        println!("{values:?}");
    }
    Ok(())
}
EOF
cargo build -q --manifest-path "$DIR/Cargo.toml"

echo "== after bootstrap"
"$DIR"/target/debug/swap_poc bootstrap $DIR/local.db

turso db shell "$DB" "BEGIN; ALTER TABLE core RENAME COLUMN a TO t; ALTER TABLE core RENAME COLUMN b TO a; ALTER TABLE core RENAME COLUMN t TO b; COMMIT;"

echo "== remote after swap"
turso db shell "$DB" "SELECT id, a, b FROM core"

echo "== local after pull (should match remote)"
"$DIR"/target/debug/swap_poc pull "$DIR/local.db"