feat: conditional SQL statements logging for db-sqlx-postgres
This commit is contained in:
parent
a9f8cc24a6
commit
b30bc67bd4
3 changed files with 26 additions and 6 deletions
|
@ -14,10 +14,13 @@
|
|||
* 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/>.
|
||||
*/
|
||||
use std::str::FromStr;
|
||||
|
||||
use db_core::dev::*;
|
||||
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use sqlx::types::time::OffsetDateTime;
|
||||
use sqlx::ConnectOptions;
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub mod errors;
|
||||
|
@ -42,6 +45,7 @@ pub enum ConnectionOptions {
|
|||
|
||||
pub struct Fresh {
|
||||
pub pool_options: PgPoolOptions,
|
||||
pub disable_logging: bool,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
|
@ -63,11 +67,22 @@ impl Connect for ConnectionOptions {
|
|||
type Pool = Database;
|
||||
async fn connect(self) -> DBResult<Self::Pool> {
|
||||
let pool = match self {
|
||||
Self::Fresh(fresh) => fresh
|
||||
.pool_options
|
||||
.connect(&fresh.url)
|
||||
.await
|
||||
.map_err(|e| DBError::DBError(Box::new(e)))?,
|
||||
Self::Fresh(fresh) => {
|
||||
let mut connect_options =
|
||||
sqlx::postgres::PgConnectOptions::from_str(&fresh.url).unwrap();
|
||||
if fresh.disable_logging {
|
||||
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,
|
||||
};
|
||||
Ok(Database { pool })
|
||||
|
|
|
@ -68,7 +68,11 @@ async fn everyting_works() {
|
|||
|
||||
let url = env::var("POSTGRES_DATABASE_URL").unwrap();
|
||||
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();
|
||||
|
||||
db.migrate().await.unwrap();
|
||||
|
|
|
@ -190,6 +190,7 @@ impl Data {
|
|||
let connection_options = ConnectionOptions::Fresh(Fresh {
|
||||
pool_options,
|
||||
url: s.database.url.clone(),
|
||||
disable_logging: s.debug,
|
||||
});
|
||||
let db = connection_options.connect().await.unwrap();
|
||||
db.migrate().await.unwrap();
|
||||
|
|
Loading…
Reference in a new issue