forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_agreement.rs
83 lines (69 loc) · 2.57 KB
/
schema_agreement.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use anyhow::{bail, Result};
use scylla::transport::errors::QueryError;
use scylla::transport::session::Session;
use scylla::SessionBuilder;
use std::env;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<()> {
// Create connection
let uri = env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
println!("Connecting to {} ...", uri);
let session: Session = SessionBuilder::new()
.known_node(uri)
.schema_agreement_interval(Duration::from_secs(1)) // check every second for schema agreement if not agreed first check
.build()
.await?;
let schema_version = session.await_schema_agreement().await?;
println!("Schema version: {}", schema_version);
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
match session.await_schema_agreement().await {
Ok(_schema_version) => println!("Schema is in agreement in time"),
Err(QueryError::RequestTimeout(_)) => println!("Schema is NOT in agreement in time"),
Err(err) => bail!(err),
};
session
.query(
"CREATE TABLE IF NOT EXISTS examples_ks.schema_agreement (a int, b int, c text, primary key (a, b))",
&[],
)
.await?;
session.await_schema_agreement().await?;
session
.query(
"INSERT INTO examples_ks.schema_agreement (a, b, c) VALUES (?, ?, ?)",
(3, 4, "def"),
)
.await?;
session.await_schema_agreement().await?;
session
.query(
"INSERT INTO examples_ks.schema_agreement (a, b, c) VALUES (1, 2, 'abc')",
&[],
)
.await?;
let prepared = session
.prepare("INSERT INTO examples_ks.schema_agreement (a, b, c) VALUES (?, 7, ?)")
.await?;
session
.execute(&prepared, (42_i32, "I'm prepared!"))
.await?;
session
.execute(&prepared, (43_i32, "I'm prepared 2!"))
.await?;
session
.execute(&prepared, (44_i32, "I'm prepared 3!"))
.await?;
// Rows can be parsed as tuples
let result = session
.query("SELECT a, b, c FROM examples_ks.schema_agreement", &[])
.await?;
let mut iter = result.rows_typed::<(i32, i32, String)>()?;
while let Some((a, b, c)) = iter.next().transpose()? {
println!("a, b, c: {}, {}, {}", a, b, c);
}
println!("Ok.");
let schema_version = session.await_schema_agreement().await?;
println!("Schema version: {}", schema_version);
Ok(())
}