Extract a Db::close method and remove deref to PgPool

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-08-20 14:58:14 +02:00
parent 0b9767651b
commit b13da81a84
2 changed files with 16 additions and 23 deletions

View file

@ -328,13 +328,20 @@ impl Db {
.fetch_all(&self.0)
.await
}
}
impl std::ops::Deref for Db {
type Target = sqlx::PgPool;
fn deref(&self) -> &Self::Target {
&self.0
#[cfg(test)]
pub async fn close(&self, db_name: &str) {
let query = "
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '{}' AND pid <> pg_backend_pid();
";
sqlx::query(query)
.bind(db_name)
.execute(&self.0)
.await
.unwrap();
self.0.close().await;
}
}

View file

@ -920,7 +920,7 @@ mod tests {
use crate::{
auth,
db::{self, UserId},
github, rpc, AppState, Config,
github, AppState, Config,
};
use async_std::{sync::RwLockReadGuard, task};
use gpui::{ModelHandle, TestAppContext};
@ -930,7 +930,7 @@ mod tests {
use sqlx::{
migrate::{MigrateDatabase, Migrator},
types::time::OffsetDateTime,
Executor as _, Postgres,
Postgres,
};
use std::{path::Path, sync::Arc, time::Duration};
use zed::{
@ -1645,21 +1645,7 @@ mod tests {
fn drop(&mut self) {
task::block_on(async {
self.peer.reset().await;
self.app_state
.db
.execute(
format!(
"
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '{}' AND pid <> pg_backend_pid();",
self.db_name,
)
.as_str(),
)
.await
.unwrap();
self.app_state.db.close().await;
self.app_state.db.close(&self.db_name).await;
Postgres::drop_database(&self.app_state.config.database_url)
.await
.unwrap();