blob: 935e05acbfa0e604dbc5f35d27059ce9b749fe47 (
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
|
use anyhow::{Error, Result};
use rusqlite_migration::{Migrations, M};
use tokio_rusqlite::Connection;
pub mod files;
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)).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))
.await?)
}
|