1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use sqlx::{
migrate::{MigrationSource, Migrator},
Connection, Executor, PgConnection, PgPool,
};
use std::{path::Path, thread};
use tokio::runtime::Runtime;
use uuid::Uuid;
#[derive(Debug)]
pub struct TestPg {
pub server_url: String,
pub dbname: String,
}
impl TestPg {
pub fn new<S>(server_url: String, migrations: S) -> Self
where
S: MigrationSource<'static> + Send + Sync + 'static,
{
let uuid = Uuid::new_v4();
let dbname = format!("test_{}", uuid);
let dbname_cloned = dbname.clone();
let tdb = Self { server_url, dbname };
let server_url = tdb.server_url();
let url = tdb.url();
thread::spawn(move || {
let rt = Runtime::new().unwrap();
rt.block_on(async move {
let mut conn = PgConnection::connect(&server_url).await.unwrap();
conn.execute(format!(r#"CREATE DATABASE "{}""#, dbname_cloned).as_str())
.await
.unwrap();
let mut conn = PgConnection::connect(&url).await.unwrap();
let m = Migrator::new(migrations).await.unwrap();
m.run(&mut conn).await.unwrap();
});
})
.join()
.expect("failed to create database");
tdb
}
pub fn server_url(&self) -> String {
self.server_url.clone()
}
pub fn url(&self) -> String {
format!("{}/{}", self.server_url, self.dbname)
}
pub async fn get_pool(&self) -> PgPool {
PgPool::connect(&self.url()).await.unwrap()
}
}
impl Drop for TestPg {
fn drop(&mut self) {
let server_url = self.server_url();
let dbname = self.dbname.clone();
thread::spawn(move || {
let rt = Runtime::new().unwrap();
rt.block_on(async move {
let mut conn = PgConnection::connect(&server_url).await.unwrap();
sqlx::query(&format!(r#"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND datname = '{}'"#, dbname))
.execute( &mut conn)
.await
.expect("Terminate all other connections");
conn.execute(format!(r#"DROP DATABASE "{}""#, dbname).as_str())
.await
.expect("Error while querying the drop database");
});
})
.join()
.expect("failed to drop database");
}
}
impl Default for TestPg {
fn default() -> Self {
Self::new(
"postgres://postgres:postgres@localhost:5432".to_string(),
Path::new("./migrations"),
)
}
}
#[cfg(test)]
mod tests {
use crate::postgres::TestPg;
#[tokio::test]
async fn test_postgres_should_create_and_drop() {
let tdb = TestPg::default();
let pool = tdb.get_pool().await;
sqlx::query("INSERT INTO todos (title) VALUES ('test')")
.execute(&pool)
.await
.unwrap();
let (id, title) = sqlx::query_as::<_, (i32, String)>("SELECT id, title FROM todos")
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(id, 1);
assert_eq!(title, "test");
}
}