mirror of
https://github.com/chatmail/core.git
synced 2026-04-18 22:16:30 +03:00
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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user