tursodatabase / libsql-client-rs

libSQL Rust client library can be used to communicate with sqld natively over HTTP protocol with native Rust interface.
MIT License
75 stars 27 forks source link

RANDOM ROWID is not supported by sqlite3-parser #67

Open devpikachu opened 7 months ago

devpikachu commented 7 months ago

This bug was observed whilst running migrations via geni, which depends on this crate.

Given the following SQL statement:

CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    uid TEXT UNIQUE NOT NULL,
    display_name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL,
    password BLOB NOT NULL,
    salt BLOB NOT NULL,
    created_at TEXT NOT NULL,
    created_by INTEGER,
    updated_at TEXT,
    updated_by INTEGER,
    FOREIGN KEY (created_by) REFERENCES users(id),
    FOREIGN KEY (updated_by) REFERENCES users(id)
) RANDOM ROWID;

The thread panics with the following error:

🚀 RUST_BACKTRACE=1 ./geni.sh up
12:43:45 [INFO] Running migration 1706354475
thread 'main' panicked at /tmp/cargo-installmSZF7O/release/build/sqlite3-parser-98bd5a2c8e53e2a9/out/parse.rs:576:13:
internal error: entered unreachable code
stack backtrace:
   0: rust_begin_unwind
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panicking.rs:72:14
   2: core::panicking::panic
             at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panicking.rs:127:5
   3: sqlite3_parser::parser::parse::yyParser::yy_reduce
   4: sqlite3_parser::parser::parse::yyParser::sqlite3Parser
   5: <sqlite3_parser::lexer::sql::Parser as fallible_iterator::FallibleIterator>::next
   6: fallible_iterator::FallibleIterator::fold
   7: libsql_client::local::Client::execute
   8: <geni::database_drivers::sqlite::SqliteDriver as geni::database_drivers::DatabaseDriver>::execute::{{closure}}
   9: geni::main::{{closure}}
  10: tokio::runtime::park::CachedParkThread::block_on
  11: geni::main

This corrupts the database file and a full wipe is needed. Removing the RANDOM ROWID statement makes the migrations occur correctly and the schema is as-defined.