feat(sql): set PRAGMA query_only to avoid writing on read-only connections

Co-authored-by: iequidoo <dgreshilov@gmail.com>
This commit is contained in:
link2xt
2024-10-20 14:51:46 +00:00
committed by GitHub
parent b13f2709be
commit 06a6cc48d2
2 changed files with 100 additions and 16 deletions

View File

@@ -93,7 +93,13 @@ impl Pool {
}
/// Retrieves a connection from the pool.
pub async fn get(&self) -> Result<PooledConnection> {
///
/// Sets `query_only` pragma to the provided value
/// to prevent accidentaly misuse of connection
/// for writing when reading is intended.
/// Only pass `query_only=false` if you want
/// to use the connection for writing.
pub async fn get(&self, query_only: bool) -> Result<PooledConnection> {
let permit = self.inner.semaphore.clone().acquire_owned().await?;
let mut connections = self.inner.connections.lock();
let conn = connections
@@ -104,6 +110,15 @@ impl Pool {
conn: Some(conn),
_permit: permit,
};
conn.pragma_update(
None,
"query_only",
if query_only {
"1".to_string()
} else {
"0".to_string()
},
)?;
Ok(conn)
}
}