Files
packager/rust/src/sqlite.rs

281 lines
7.5 KiB
Rust
Raw Normal View History

2023-08-29 21:34:01 +02:00
use std::fmt;
2023-08-29 21:34:01 +02:00
use std::time;
2023-08-29 21:34:01 +02:00
use base64::Engine as _;
use sha2::{Digest, Sha256};
2023-08-29 21:34:01 +02:00
use tracing::Instrument;
2023-08-29 21:34:00 +02:00
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
2023-08-29 21:34:01 +02:00
use sqlx::ConnectOptions;
2023-08-29 21:34:01 +02:00
pub use sqlx::{Pool as SqlitePool, Sqlite};
2023-08-29 21:34:00 +02:00
use std::str::FromStr as _;
2023-08-29 21:34:01 +02:00
pub use sqlx::Type;
2023-08-29 21:34:00 +02:00
use crate::StartError;
2023-08-29 21:34:01 +02:00
pub type Pool = sqlx::Pool<sqlx::Sqlite>;
pub fn int_to_bool(value: i32) -> bool {
match value {
0 => false,
1 => true,
_ => panic!("got invalid boolean from sqlite"),
}
}
#[tracing::instrument]
2023-08-29 21:34:01 +02:00
pub async fn init_database_pool(url: &str) -> Result<Pool, StartError> {
2023-08-29 21:34:01 +02:00
async {
SqlitePoolOptions::new()
.max_connections(5)
.connect_with(
SqliteConnectOptions::from_str(url)?
.log_statements(log::LevelFilter::Debug)
.log_slow_statements(log::LevelFilter::Warn, time::Duration::from_millis(100))
.pragma("foreign_keys", "1"),
)
.await
}
.instrument(tracing::info_span!("packager::sql::pool"))
.await
.map_err(Into::into)
2023-08-29 21:34:00 +02:00
}
#[tracing::instrument]
2023-08-29 21:34:00 +02:00
pub async fn migrate(url: &str) -> Result<(), StartError> {
2023-08-29 21:34:01 +02:00
async {
let pool = SqlitePoolOptions::new()
.max_connections(5)
.connect_with(
SqliteConnectOptions::from_str(url)?
.pragma("foreign_keys", "0")
.log_statements(log::LevelFilter::Debug),
)
.await?;
sqlx::migrate!().run(&pool).await
}
.instrument(tracing::info_span!("packager::sql::migrate"))
.await?;
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:00 +02:00
Ok(())
}
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
pub enum QueryType {
Insert,
Update,
Select,
Delete,
}
impl fmt::Display for QueryType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::Insert => "insert",
Self::Update => "update",
Self::Select => "select",
Self::Delete => "delete",
}
)
}
}
pub enum Component {
Inventory,
User,
Trips,
}
impl fmt::Display for Component {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::Inventory => "inventory",
Self::User => "user",
Self::Trips => "trips",
}
)
}
}
pub struct QueryClassification {
pub query_type: QueryType,
pub component: Component,
}
pub fn sqlx_query(
2023-08-29 21:34:01 +02:00
classification: &QueryClassification,
2023-08-29 21:34:01 +02:00
query: &str,
labels: &[(&'static str, String)],
) {
2023-08-29 21:34:01 +02:00
let query_id = {
let mut hasher = Sha256::new();
hasher.update(query);
hasher.finalize()
};
2023-08-29 21:34:01 +02:00
// 9 bytes is enough to be unique
// If this is divisible by 3, it means that we can base64-encode it without
// any "=" padding
//
// cannot panic, as the output for sha256 will always be bit
let query_id = &query_id[..9];
2023-08-29 21:34:01 +02:00
let query_id = base64::engine::general_purpose::STANDARD.encode(query_id);
let mut labels = Vec::from(labels);
2023-08-29 21:34:01 +02:00
labels.extend_from_slice(&[
("query_id", query_id),
("query_type", classification.query_type.to_string()),
("query_component", classification.component.to_string()),
]);
metrics::counter!("packager_database_queries_total", 1, &labels);
2023-08-29 21:34:01 +02:00
}
2023-08-29 21:34:01 +02:00
#[macro_export]
macro_rules! query_all {
2023-08-29 21:34:01 +02:00
( $class:expr, $pool:expr, $struct_row:path, $struct_into:path, $query:expr, $( $args:tt )* ) => {
{
use tracing::Instrument as _;
async {
2023-08-29 21:34:01 +02:00
$crate::sqlite::sqlx_query($class, $query, &[]);
2023-08-29 21:34:01 +02:00
let result: Result<Vec<$struct_into>, Error> = sqlx::query_as!(
$struct_row,
$query,
$( $args )*
)
.fetch($pool)
.map_ok(|row: $struct_row| row.try_into())
.try_collect::<Vec<Result<$struct_into, Error>>>()
.await?
.into_iter()
.collect::<Result<Vec<$struct_into>, Error>>();
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
result
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
}.instrument(tracing::info_span!("packager::sql::query", "query"))
}
2023-08-29 21:34:01 +02:00
};
}
#[macro_export]
macro_rules! query_one {
2023-08-29 21:34:01 +02:00
( $class:expr, $pool:expr, $struct_row:path, $struct_into:path, $query:expr, $( $args:tt )*) => {
{
use tracing::Instrument as _;
async {
2023-08-29 21:34:01 +02:00
$crate::sqlite::sqlx_query($class, $query, &[]);
2023-08-29 21:34:01 +02:00
let result: Result<Option<$struct_into>, Error> = sqlx::query_as!(
$struct_row,
$query,
$( $args )*
)
.fetch_optional($pool)
.await?
.map(|row: $struct_row| row.try_into())
.transpose();
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
result
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
}.instrument(tracing::info_span!("packager::sql::query", "query"))
}
2023-08-29 21:34:01 +02:00
};
}
#[macro_export]
macro_rules! query_exists {
2023-08-29 21:34:01 +02:00
( $class:expr, $pool:expr, $query:expr, $( $args:tt )*) => {
{
use tracing::Instrument as _;
async {
2023-08-29 21:34:01 +02:00
$crate::sqlite::sqlx_query($class, $query, &[]);
2023-08-29 21:34:01 +02:00
let result: bool = sqlx::query!(
$query,
$( $args )*
)
.fetch_optional($pool)
.await?
.is_some();
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
Ok(result)
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
}.instrument(tracing::info_span!("packager::sql::query", "query"))
}
2023-08-29 21:34:01 +02:00
};
}
#[macro_export]
macro_rules! execute {
2023-08-29 21:34:01 +02:00
( $class:expr, $pool:expr, $query:expr, $( $args:tt )*) => {
{
use tracing::Instrument as _;
async {
2023-08-29 21:34:01 +02:00
$crate::sqlite::sqlx_query($class, $query, &[]);
2023-08-29 21:34:01 +02:00
let result: Result<sqlx::sqlite::SqliteQueryResult, Error> = sqlx::query!(
$query,
$( $args )*
)
.execute($pool)
.await
.map_err(|e| e.into());
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
result
}.instrument(tracing::info_span!("packager::sql::query", "query"))
}
2023-08-29 21:34:01 +02:00
};
}
#[macro_export]
macro_rules! execute_returning {
2023-08-29 21:34:01 +02:00
( $class:expr, $pool:expr, $query:expr, $t:path, $fn:expr, $( $args:tt )*) => {
{
use tracing::Instrument as _;
async {
2023-08-29 21:34:01 +02:00
$crate::sqlite::sqlx_query($class, $query, &[]);
2023-08-29 21:34:01 +02:00
let result: Result<$t, Error> = sqlx::query!(
$query,
$( $args )*
)
.fetch_one($pool)
.map_ok($fn)
.await
.map_err(Into::into);
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
result
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
}.instrument(tracing::info_span!("packager::sql::query", "query"))
}
2023-08-29 21:34:01 +02:00
};
}
#[macro_export]
macro_rules! execute_returning_uuid {
2023-08-29 21:34:01 +02:00
( $class:expr, $pool:expr, $query:expr, $( $args:tt )*) => {
{
use tracing::Instrument as _;
async {
2023-08-29 21:34:01 +02:00
$crate::sqlite::sqlx_query($class, $query, &[]);
2023-08-29 21:34:01 +02:00
let result: Result<Uuid, Error> = sqlx::query!(
$query,
$( $args )*
)
.fetch_one($pool)
.map_ok(|row| Uuid::try_parse(&row.id))
.await?
.map_err(Into::into);
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
result
2023-08-29 21:34:01 +02:00
2023-08-29 21:34:01 +02:00
}.instrument(tracing::info_span!("packager::sql::query", "query"))
}
2023-08-29 21:34:01 +02:00
};
}