feat: conditional SQL statements logging for db-sqlx-postgres

This commit is contained in:
realaravinth 2022-05-30 15:48:54 +05:30
parent a9f8cc24a6
commit b30bc67bd4
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
3 changed files with 26 additions and 6 deletions

View file

@ -14,10 +14,13 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::str::FromStr;
use db_core::dev::*; use db_core::dev::*;
use sqlx::postgres::PgPoolOptions; use sqlx::postgres::PgPoolOptions;
use sqlx::types::time::OffsetDateTime; use sqlx::types::time::OffsetDateTime;
use sqlx::ConnectOptions;
use sqlx::PgPool; use sqlx::PgPool;
pub mod errors; pub mod errors;
@ -42,6 +45,7 @@ pub enum ConnectionOptions {
pub struct Fresh { pub struct Fresh {
pub pool_options: PgPoolOptions, pub pool_options: PgPoolOptions,
pub disable_logging: bool,
pub url: String, pub url: String,
} }
@ -63,11 +67,22 @@ impl Connect for ConnectionOptions {
type Pool = Database; type Pool = Database;
async fn connect(self) -> DBResult<Self::Pool> { async fn connect(self) -> DBResult<Self::Pool> {
let pool = match self { let pool = match self {
Self::Fresh(fresh) => fresh Self::Fresh(fresh) => {
.pool_options let mut connect_options =
.connect(&fresh.url) sqlx::postgres::PgConnectOptions::from_str(&fresh.url).unwrap();
.await if fresh.disable_logging {
.map_err(|e| DBError::DBError(Box::new(e)))?, connect_options.disable_statement_logging();
}
sqlx::postgres::PgConnectOptions::from_str(&fresh.url)
.unwrap()
.disable_statement_logging();
fresh
.pool_options
.connect_with(connect_options)
.await
.map_err(|e| DBError::DBError(Box::new(e)))?
}
Self::Existing(conn) => conn.0, Self::Existing(conn) => conn.0,
}; };
Ok(Database { pool }) Ok(Database { pool })

View file

@ -68,7 +68,11 @@ async fn everyting_works() {
let url = env::var("POSTGRES_DATABASE_URL").unwrap(); let url = env::var("POSTGRES_DATABASE_URL").unwrap();
let pool_options = PgPoolOptions::new().max_connections(2); let pool_options = PgPoolOptions::new().max_connections(2);
let connection_options = ConnectionOptions::Fresh(Fresh { pool_options, url }); let connection_options = ConnectionOptions::Fresh(Fresh {
pool_options,
url,
disable_logging: false,
});
let db = connection_options.connect().await.unwrap(); let db = connection_options.connect().await.unwrap();
db.migrate().await.unwrap(); db.migrate().await.unwrap();

View file

@ -190,6 +190,7 @@ impl Data {
let connection_options = ConnectionOptions::Fresh(Fresh { let connection_options = ConnectionOptions::Fresh(Fresh {
pool_options, pool_options,
url: s.database.url.clone(), url: s.database.url.clone(),
disable_logging: s.debug,
}); });
let db = connection_options.connect().await.unwrap(); let db = connection_options.connect().await.unwrap();
db.migrate().await.unwrap(); db.migrate().await.unwrap();