-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathmain.rs
More file actions
31 lines (25 loc) · 1004 Bytes
/
main.rs
File metadata and controls
31 lines (25 loc) · 1004 Bytes
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
use sqlx::sqlite::SqliteOwnedBuf;
use sqlx::{Connection, SqliteConnection};
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
let mut conn = SqliteConnection::connect("sqlite::memory:").await?;
sqlx::raw_sql(
"create table notes(id integer primary key, body text not null);
insert into notes(body) values ('hello'), ('world');",
)
.execute(&mut conn)
.await?;
let snapshot: SqliteOwnedBuf = conn.serialize(None).await?;
let bytes: &[u8] = snapshot.as_ref();
conn.close().await?;
let owned = SqliteOwnedBuf::try_from(bytes)?;
let mut restored = SqliteConnection::connect("sqlite::memory:").await?;
restored.deserialize(None, owned, false).await?;
let rows = sqlx::query_as::<_, (i64, String)>("select id, body from notes order by id")
.fetch_all(&mut restored)
.await?;
assert_eq!(rows.len(), 2);
assert_eq!(rows[0].1, "hello");
assert_eq!(rows[1].1, "world");
Ok(())
}