SurrealDB - add field to schema
- We have a schema and some entries in the databas.
- We change the schema adding another field, but that field does not exist in the data that is already in the database.
- For new data we have to supply the new field as well.
- For old data we don't have to make changes but when we SELECT the data we need to have a struct where this field is Option.
examples/surrealdb/add-field-to-schema/src/main.rs
use serde::{Deserialize, Serialize}; use surrealdb::engine::local::Mem; use surrealdb::{RecordId, Surreal}; #[derive(Debug, Deserialize, Serialize)] struct EntryNummer { id: RecordId, number: u32, } #[derive(Debug, Deserialize, Serialize)] struct EntryWithName { id: RecordId, number: u32, name: Option<String>, } #[tokio::main] async fn main() -> surrealdb::Result<()> { let dbh = Surreal::new::<Mem>(()).await?; dbh.use_ns("demo").use_db("demo").await?; dbh.query("DEFINE TABLE entry SCHEMAFULL").await?; dbh.query("DEFINE FIELD number ON TABLE entry TYPE int") .await?; let res = dbh .query( "CREATE entry CONTENT { number: 42, };", ) .await?; match res.check() { Ok(val) => println!("Success: {val:?}"), Err(err) => println!("Error: {err}"), } println!("---------"); let mut entries = dbh.query("SELECT * FROM entry").await?; let entries: Vec<EntryNummer> = entries.take(0)?; assert_eq!(entries.len(), 1); assert_eq!(entries[0].number, 42); for entry in entries { println!("{} {}", entry.id, entry.number); } println!("---------"); let res = dbh .query("DEFINE FIELD name ON TABLE entry TYPE string") .await?; match res.check() { Ok(val) => println!("Success: {val:?}"), Err(err) => println!("Error: {err}"), } println!("---------"); let res = dbh .query( "CREATE entry CONTENT { number: 23, name: 'twenty three', };", ) .await?; match res.check() { Ok(val) => println!("Success: {val:?}"), Err(err) => println!("Error: {err}"), } println!("---------"); let mut entries = dbh .query("SELECT * FROM entry ORDER BY number DESC") .await?; let entries: Vec<EntryWithName> = entries.take(0)?; assert_eq!(entries.len(), 2); assert_eq!(entries[0].number, 42); assert_eq!(entries[0].name, None); assert_eq!(entries[1].number, 23); assert_eq!(entries[1].name, Some(String::from("twenty three"))); for entry in entries { println!( "{} {} {}", entry.id, entry.number, entry.name.unwrap_or("missing".to_string()) ); } println!("---------"); Ok(()) } #[cfg(test)] mod test { use super::main; #[test] fn check() { main().unwrap(); } }
Success: Response { client: Surreal { router: OnceLock(Router { sender: Sender { .. }, last_id: 2, features: {Backup, LiveQueries} }), engine: PhantomData<surrealdb::api::engine::any::Any> }, results: {0: (Stats { execution_time: Some(1.434877ms) }, Ok(Array(Array([Object(Object({"id": Thing(Thing { tb: "entry", id: String("1iqktq46inqa3ekw677r") }), "number": Number(Int(42))}))]))))}, live_queries: {} }