/
verify_tables.rs
62 lines (54 loc) · 1.72 KB
/
verify_tables.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
use welds::prelude::*;
// This struct doesn't have a table in the DB
#[derive(Debug, WeldsModel)]
#[welds(table = "i_dont_exist")]
pub struct NotInDb {
#[welds(primary_key)]
pub id: i32,
}
#[derive(Debug, WeldsModel)]
#[welds(table = "Orders")]
pub struct Order {
#[welds(primary_key)]
// null in db
pub id: i32,
// null int in db
pub product_id: String,
/*
* these are removed to show you what happens when they are missing
pub price: Option<f32>,
pub code: Option<String>,
pub product_id2: Option<i32>,
*/
}
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let connection_string = "sqlite::memory:";
let client = welds::connections::connect(connection_string).await?;
// Build an in memory DB with a schema (Product Table, Orders Table)
let schema = include_str!("../../tests/testlib/databases/sqlite/01_create_tables.sql");
client.execute(schema, &[]).await?;
// the NotInDb struct will only report back that is isn't in the database
println!();
let diff = welds::check::schema::<NotInDb>(client.as_ref()).await?;
for d in diff {
println!("{}", d);
}
// Get all the things that are different from the Order struct and the order table in the DB
let diff = welds::check::schema::<Order>(client.as_ref()).await?;
for d in &diff {
println!("{}", d);
}
println!();
// let look over just the columns that have changed types
// on the Orders table/struct
println!("Orders Changed Columns:");
for d in &diff {
if let Some(changed) = d.kind.as_changed() {
if changed.type_changed() {
println!("{}", changed);
}
}
}
Ok(())
}