blob: 37769d2ee7cfd49c0a064821a0a3b8f9a07c0773 (
plain)
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
|
use anyhow::{Error, Result};
use rusqlite_migration::{Migrations, M};
use tokio_rusqlite::Connection;
pub mod files;
mod utils;
pub async fn init(path: &str) -> Result<Connection> {
let connection = Connection::open(path)
.await
.map_err(|err| Error::msg(format!("Error opening connection: {err}")))?;
apply_migrations(&connection).await?;
set_pragma(&connection, "foreign_keys", "ON").await?;
set_pragma(&connection, "journal_mode", "wal").await?;
Ok(connection)
}
async fn apply_migrations(conn: &Connection) -> Result<()> {
let migrations = Migrations::new(vec![
M::up(include_str!("migrations/01-init.sql")),
M::up(include_str!("migrations/02-strict-tables.sql")),
]);
Ok(conn
.call(move |conn| {
migrations
.to_latest(conn)
.map_err(|migration_err| tokio_rusqlite::Error::Other(Box::new(migration_err)))
})
.await?)
}
async fn set_pragma(
conn: &Connection,
key: impl Into<String>,
value: impl Into<String>,
) -> Result<()> {
let key = key.into();
let value = value.into();
Ok(conn
.call(move |conn| {
conn.pragma_update(None, &key, &value)
.map_err(tokio_rusqlite::Error::Rusqlite)
})
.await?)
}
|