Beginning work on adding tokio-diesel. #1684

This commit is contained in:
Dessalines 2021-08-03 15:59:20 -04:00
parent b8d7f00d58
commit f6926f8af2
7 changed files with 293 additions and 214 deletions

166
Cargo.lock generated
View File

@ -36,7 +36,7 @@ dependencies = [
"actix-rt", "actix-rt",
"actix_derive", "actix_derive",
"bitflags", "bitflags",
"bytes", "bytes 1.0.1",
"crossbeam-channel", "crossbeam-channel",
"futures-core", "futures-core",
"futures-sink", "futures-sink",
@ -45,9 +45,9 @@ dependencies = [
"log", "log",
"once_cell", "once_cell",
"parking_lot", "parking_lot",
"pin-project-lite", "pin-project-lite 0.2.7",
"smallvec", "smallvec",
"tokio", "tokio 1.8.0",
"tokio-util", "tokio-util",
] ]
@ -58,12 +58,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d5dbeb2d9e51344cb83ca7cc170f1217f9fe25bfc50160e6e200b5c31c1019a" checksum = "1d5dbeb2d9e51344cb83ca7cc170f1217f9fe25bfc50160e6e200b5c31c1019a"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"bytes", "bytes 1.0.1",
"futures-core", "futures-core",
"futures-sink", "futures-sink",
"log", "log",
"pin-project-lite", "pin-project-lite 0.2.7",
"tokio", "tokio 1.8.0",
"tokio-util", "tokio-util",
] ]
@ -81,7 +81,7 @@ dependencies = [
"ahash 0.7.4", "ahash 0.7.4",
"base64 0.13.0", "base64 0.13.0",
"bitflags", "bitflags",
"bytes", "bytes 1.0.1",
"bytestring", "bytestring",
"derive_more", "derive_more",
"encoding_rs", "encoding_rs",
@ -98,14 +98,14 @@ dependencies = [
"once_cell", "once_cell",
"percent-encoding", "percent-encoding",
"pin-project", "pin-project",
"pin-project-lite", "pin-project-lite 0.2.7",
"rand 0.8.4", "rand 0.8.4",
"regex", "regex",
"serde", "serde",
"sha-1 0.9.6", "sha-1 0.9.6",
"smallvec", "smallvec",
"time 0.2.27", "time 0.2.27",
"tokio", "tokio 1.8.0",
] ]
[[package]] [[package]]
@ -139,7 +139,7 @@ checksum = "bc7d7cd957c9ed92288a7c3c96af81fa5291f65247a76a34dac7b6af74e52ba0"
dependencies = [ dependencies = [
"actix-macros", "actix-macros",
"futures-core", "futures-core",
"tokio", "tokio 1.8.0",
] ]
[[package]] [[package]]
@ -156,7 +156,7 @@ dependencies = [
"mio", "mio",
"num_cpus", "num_cpus",
"slab", "slab",
"tokio", "tokio 1.8.0",
] ]
[[package]] [[package]]
@ -167,7 +167,7 @@ checksum = "77f5f9d66a8730d0fae62c26f3424f5751e5518086628a40b7ab6fca4a705034"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"paste", "paste",
"pin-project-lite", "pin-project-lite 0.2.7",
] ]
[[package]] [[package]]
@ -196,7 +196,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e491cbaac2e7fc788dfff99ff48ef317e23b3cf63dbaf7aaab6418f40f92aa94" checksum = "e491cbaac2e7fc788dfff99ff48ef317e23b3cf63dbaf7aaab6418f40f92aa94"
dependencies = [ dependencies = [
"local-waker", "local-waker",
"pin-project-lite", "pin-project-lite 0.2.7",
] ]
[[package]] [[package]]
@ -216,7 +216,7 @@ dependencies = [
"actix-utils", "actix-utils",
"actix-web-codegen", "actix-web-codegen",
"ahash 0.7.4", "ahash 0.7.4",
"bytes", "bytes 1.0.1",
"cfg-if", "cfg-if",
"cookie", "cookie",
"derive_more", "derive_more",
@ -251,11 +251,11 @@ dependencies = [
"actix-codec", "actix-codec",
"actix-http", "actix-http",
"actix-web", "actix-web",
"bytes", "bytes 1.0.1",
"bytestring", "bytestring",
"futures-core", "futures-core",
"pin-project", "pin-project",
"tokio", "tokio 1.8.0",
] ]
[[package]] [[package]]
@ -393,7 +393,7 @@ dependencies = [
"actix-rt", "actix-rt",
"actix-service", "actix-service",
"base64 0.13.0", "base64 0.13.0",
"bytes", "bytes 1.0.1",
"cfg-if", "cfg-if",
"derive_more", "derive_more",
"futures-core", "futures-core",
@ -401,7 +401,7 @@ dependencies = [
"log", "log",
"mime", "mime",
"percent-encoding", "percent-encoding",
"pin-project-lite", "pin-project-lite 0.2.7",
"rand 0.8.4", "rand 0.8.4",
"serde", "serde",
"serde_json", "serde_json",
@ -435,7 +435,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"thiserror", "thiserror",
"tokio", "tokio 1.8.0",
"uuid", "uuid",
] ]
@ -454,7 +454,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"thiserror", "thiserror",
"tokio", "tokio 1.8.0",
"uuid", "uuid",
] ]
@ -585,6 +585,12 @@ version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "bytes"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"
[[package]] [[package]]
name = "bytes" name = "bytes"
version = "1.0.1" version = "1.0.1"
@ -597,7 +603,7 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90706ba19e97b90786e19dc0d5e2abd80008d99d4c0c5d1ad0b5e72cec7c494d" checksum = "90706ba19e97b90786e19dc0d5e2abd80008d99d4c0c5d1ad0b5e72cec7c494d"
dependencies = [ dependencies = [
"bytes", "bytes 1.0.1",
] ]
[[package]] [[package]]
@ -1176,7 +1182,7 @@ dependencies = [
"futures-sink", "futures-sink",
"futures-task", "futures-task",
"memchr", "memchr",
"pin-project-lite", "pin-project-lite 0.2.7",
"pin-utils", "pin-utils",
"proc-macro-hack", "proc-macro-hack",
"proc-macro-nested", "proc-macro-nested",
@ -1252,7 +1258,7 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "825343c4eef0b63f541f8903f395dc5beb362a979b5799a84062527ef1e37726" checksum = "825343c4eef0b63f541f8903f395dc5beb362a979b5799a84062527ef1e37726"
dependencies = [ dependencies = [
"bytes", "bytes 1.0.1",
"fnv", "fnv",
"futures-core", "futures-core",
"futures-sink", "futures-sink",
@ -1260,7 +1266,7 @@ dependencies = [
"http", "http",
"indexmap", "indexmap",
"slab", "slab",
"tokio", "tokio 1.8.0",
"tokio-util", "tokio-util",
"tracing", "tracing",
] ]
@ -1321,7 +1327,7 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "527e8c9ac747e28542699a951517aa9a6945af506cd1f2e1b53a576c17b6cc11" checksum = "527e8c9ac747e28542699a951517aa9a6945af506cd1f2e1b53a576c17b6cc11"
dependencies = [ dependencies = [
"bytes", "bytes 1.0.1",
"fnv", "fnv",
"itoa", "itoa",
] ]
@ -1332,9 +1338,9 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60daa14be0e0786db0f03a9e57cb404c9d756eed2b6c62b9ea98ec5743ec75a9" checksum = "60daa14be0e0786db0f03a9e57cb404c9d756eed2b6c62b9ea98ec5743ec75a9"
dependencies = [ dependencies = [
"bytes", "bytes 1.0.1",
"http", "http",
"pin-project-lite", "pin-project-lite 0.2.7",
] ]
[[package]] [[package]]
@ -1371,7 +1377,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a1e24f3c8b2c8b5eddb82d4cdf07dafe01f5b87f92b81a369dd520a107d33e8" checksum = "4a1e24f3c8b2c8b5eddb82d4cdf07dafe01f5b87f92b81a369dd520a107d33e8"
dependencies = [ dependencies = [
"base64 0.13.0", "base64 0.13.0",
"bytes", "bytes 1.0.1",
"chrono", "chrono",
"futures", "futures",
"http", "http",
@ -1379,7 +1385,7 @@ dependencies = [
"reqwest", "reqwest",
"sha2", "sha2",
"thiserror", "thiserror",
"tokio", "tokio 1.8.0",
] ]
[[package]] [[package]]
@ -1406,7 +1412,7 @@ version = "0.14.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07d6baa1b441335f3ce5098ac421fb6547c46dda735ca1bc6d0153c838f9dd83" checksum = "07d6baa1b441335f3ce5098ac421fb6547c46dda735ca1bc6d0153c838f9dd83"
dependencies = [ dependencies = [
"bytes", "bytes 1.0.1",
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-util", "futures-util",
@ -1416,9 +1422,9 @@ dependencies = [
"httparse", "httparse",
"httpdate", "httpdate",
"itoa", "itoa",
"pin-project-lite", "pin-project-lite 0.2.7",
"socket2", "socket2",
"tokio", "tokio 1.8.0",
"tower-service", "tower-service",
"tracing", "tracing",
"want", "want",
@ -1430,10 +1436,10 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
dependencies = [ dependencies = [
"bytes", "bytes 1.0.1",
"hyper", "hyper",
"native-tls", "native-tls",
"tokio", "tokio 1.8.0",
"tokio-native-tls", "tokio-native-tls",
] ]
@ -1597,7 +1603,7 @@ dependencies = [
"strum", "strum",
"strum_macros", "strum_macros",
"thiserror", "thiserror",
"tokio", "tokio 1.8.0",
"url", "url",
"uuid", "uuid",
] ]
@ -1660,7 +1666,8 @@ dependencies = [
"strum", "strum",
"strum_macros", "strum_macros",
"thiserror", "thiserror",
"tokio", "tokio 1.8.0",
"tokio-diesel 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"url", "url",
"uuid", "uuid",
] ]
@ -1708,7 +1715,7 @@ dependencies = [
"strum", "strum",
"strum_macros", "strum_macros",
"thiserror", "thiserror",
"tokio", "tokio 1.8.0",
"url", "url",
"uuid", "uuid",
] ]
@ -1757,6 +1764,8 @@ dependencies = [
"sha2", "sha2",
"strum", "strum",
"strum_macros", "strum_macros",
"tokio 1.8.0",
"tokio-diesel 0.3.0 (git+https://github.com/mehcode/tokio-diesel)",
"url", "url",
] ]
@ -1867,7 +1876,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"strum", "strum",
"tokio", "tokio 1.8.0",
"url", "url",
] ]
@ -1901,7 +1910,7 @@ dependencies = [
"strum", "strum",
"strum_macros", "strum_macros",
"thiserror", "thiserror",
"tokio", "tokio 1.8.0",
"url", "url",
] ]
@ -1927,7 +1936,7 @@ dependencies = [
"serde_json", "serde_json",
"strum", "strum",
"strum_macros", "strum_macros",
"tokio", "tokio 1.8.0",
] ]
[[package]] [[package]]
@ -2399,6 +2408,12 @@ dependencies = [
"syn 1.0.73", "syn 1.0.73",
] ]
[[package]]
name = "pin-project-lite"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "257b64915a082f7811703966789728173279bdebb956b143dbcd23f6f970a777"
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.7" version = "0.2.7"
@ -2697,7 +2712,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "246e9f61b9bb77df069a947682be06e31ac43ea37862e244a69f177694ea6d22" checksum = "246e9f61b9bb77df069a947682be06e31ac43ea37862e244a69f177694ea6d22"
dependencies = [ dependencies = [
"base64 0.13.0", "base64 0.13.0",
"bytes", "bytes 1.0.1",
"encoding_rs", "encoding_rs",
"futures-core", "futures-core",
"futures-util", "futures-util",
@ -2712,11 +2727,11 @@ dependencies = [
"mime", "mime",
"native-tls", "native-tls",
"percent-encoding", "percent-encoding",
"pin-project-lite", "pin-project-lite 0.2.7",
"serde", "serde",
"serde_json", "serde_json",
"serde_urlencoded", "serde_urlencoded",
"tokio", "tokio 1.8.0",
"tokio-native-tls", "tokio-native-tls",
"url", "url",
"wasm-bindgen", "wasm-bindgen",
@ -3265,6 +3280,17 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]]
name = "tokio"
version = "0.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092"
dependencies = [
"bytes 0.5.6",
"pin-project-lite 0.1.12",
"slab",
]
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.8.0" version = "1.8.0"
@ -3272,17 +3298,55 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "570c2eb13b3ab38208130eccd41be92520388791207fde783bda7c1e8ace28d4" checksum = "570c2eb13b3ab38208130eccd41be92520388791207fde783bda7c1e8ace28d4"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"bytes", "bytes 1.0.1",
"libc", "libc",
"memchr", "memchr",
"mio", "mio",
"num_cpus",
"once_cell", "once_cell",
"parking_lot", "parking_lot",
"pin-project-lite", "pin-project-lite 0.2.7",
"signal-hook-registry", "signal-hook-registry",
"tokio-macros",
"winapi", "winapi",
] ]
[[package]]
name = "tokio-diesel"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "713f06058e12ed5adb542401b9d20f2b18b3fccf6184c5d77b57a31b3d21cc69"
dependencies = [
"async-trait",
"diesel",
"futures",
"r2d2",
"tokio 0.2.25",
]
[[package]]
name = "tokio-diesel"
version = "0.3.0"
source = "git+https://github.com/mehcode/tokio-diesel#f4af42558246ab323600622ba8d08803d3c18842"
dependencies = [
"async-trait",
"diesel",
"futures",
"r2d2",
"tokio 1.8.0",
]
[[package]]
name = "tokio-macros"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54473be61f4ebe4efd09cec9bd5d16fa51d70ea0192213d754d2d500457db110"
dependencies = [
"proc-macro2 1.0.27",
"quote 1.0.9",
"syn 1.0.73",
]
[[package]] [[package]]
name = "tokio-native-tls" name = "tokio-native-tls"
version = "0.3.0" version = "0.3.0"
@ -3290,7 +3354,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b"
dependencies = [ dependencies = [
"native-tls", "native-tls",
"tokio", "tokio 1.8.0",
] ]
[[package]] [[package]]
@ -3300,7 +3364,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc6844de72e57df1980054b38be3a9f4702aba4858be64dd700181a8a6d0e1b6" checksum = "bc6844de72e57df1980054b38be3a9f4702aba4858be64dd700181a8a6d0e1b6"
dependencies = [ dependencies = [
"rustls", "rustls",
"tokio", "tokio 1.8.0",
"webpki", "webpki",
] ]
@ -3310,12 +3374,12 @@ version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1caa0b0c8d94a049db56b5acf8cba99dc0623aab1b26d5b5f5e2d945846b3592" checksum = "1caa0b0c8d94a049db56b5acf8cba99dc0623aab1b26d5b5f5e2d945846b3592"
dependencies = [ dependencies = [
"bytes", "bytes 1.0.1",
"futures-core", "futures-core",
"futures-sink", "futures-sink",
"log", "log",
"pin-project-lite", "pin-project-lite 0.2.7",
"tokio", "tokio 1.8.0",
] ]
[[package]] [[package]]
@ -3340,7 +3404,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09adeb8c97449311ccd28a427f96fb563e7fd31aabf994189879d9da2394b89d" checksum = "09adeb8c97449311ccd28a427f96fb563e7fd31aabf994189879d9da2394b89d"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"pin-project-lite", "pin-project-lite 0.2.7",
"tracing-core", "tracing-core",
] ]

View File

@ -42,3 +42,4 @@ anyhow = "1.0.41"
thiserror = "1.0.26" thiserror = "1.0.26"
background-jobs = "0.9.0" background-jobs = "0.9.0"
reqwest = { version = "0.11.4", features = ["json"] } reqwest = { version = "0.11.4", features = ["json"] }
tokio-diesel = "0.3.0"

View File

@ -24,6 +24,8 @@ url = { version = "2.2.2", features = ["serde"] }
lazy_static = "1.4.0" lazy_static = "1.4.0"
regex = "1.5.4" regex = "1.5.4"
bcrypt = "0.10.0" bcrypt = "0.10.0"
tokio = { version = "1.8.0", features = ["rt", "macros"] }
tokio-diesel = { git = "https://github.com/mehcode/tokio-diesel" }
[dev-dependencies] [dev-dependencies]
serial_test = "0.5.1" serial_test = "0.5.1"

View File

@ -14,10 +14,10 @@ pub struct CommentAggregates {
} }
impl CommentAggregates { impl CommentAggregates {
pub fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> { pub fn read(pool: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
comment_aggregates::table comment_aggregates::table
.filter(comment_aggregates::comment_id.eq(comment_id)) .filter(comment_aggregates::comment_id.eq(comment_id))
.first::<Self>(conn) .first::<Self>(pool)
} }
} }
@ -25,7 +25,7 @@ impl CommentAggregates {
mod tests { mod tests {
use crate::{ use crate::{
aggregates::comment_aggregates::CommentAggregates, aggregates::comment_aggregates::CommentAggregates,
establish_unpooled_connection, setup_connection_pool_for_tests,
Crud, Crud,
Likeable, Likeable,
}; };
@ -40,21 +40,21 @@ mod tests {
#[test] #[test]
#[serial] #[serial]
fn test_crud() { fn test_crud() {
let conn = establish_unpooled_connection(); let pool = setup_connection_pool_for_tests();
let new_person = PersonForm { let new_person = PersonForm {
name: "thommy_comment_agg".into(), name: "thommy_comment_agg".into(),
..PersonForm::default() ..PersonForm::default()
}; };
let inserted_person = Person::create(&conn, &new_person).unwrap(); let inserted_person = Person::create(&pool, &new_person).unwrap();
let another_person = PersonForm { let another_person = PersonForm {
name: "jerry_comment_agg".into(), name: "jerry_comment_agg".into(),
..PersonForm::default() ..PersonForm::default()
}; };
let another_inserted_person = Person::create(&conn, &another_person).unwrap(); let another_inserted_person = Person::create(&pool, &another_person).unwrap();
let new_community = CommunityForm { let new_community = CommunityForm {
name: "TIL_comment_agg".into(), name: "TIL_comment_agg".into(),
@ -62,7 +62,7 @@ mod tests {
..CommunityForm::default() ..CommunityForm::default()
}; };
let inserted_community = Community::create(&conn, &new_community).unwrap(); let inserted_community = Community::create(&pool, &new_community).unwrap();
let new_post = PostForm { let new_post = PostForm {
name: "A test post".into(), name: "A test post".into(),
@ -71,7 +71,7 @@ mod tests {
..PostForm::default() ..PostForm::default()
}; };
let inserted_post = Post::create(&conn, &new_post).unwrap(); let inserted_post = Post::create(&pool, &new_post).unwrap();
let comment_form = CommentForm { let comment_form = CommentForm {
content: "A test comment".into(), content: "A test comment".into(),
@ -80,7 +80,7 @@ mod tests {
..CommentForm::default() ..CommentForm::default()
}; };
let inserted_comment = Comment::create(&conn, &comment_form).unwrap(); let inserted_comment = Comment::create(&pool, &comment_form).unwrap();
let child_comment_form = CommentForm { let child_comment_form = CommentForm {
content: "A test comment".into(), content: "A test comment".into(),
@ -90,7 +90,7 @@ mod tests {
..CommentForm::default() ..CommentForm::default()
}; };
let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap(); let _inserted_child_comment = Comment::create(&pool, &child_comment_form).unwrap();
let comment_like = CommentLikeForm { let comment_like = CommentLikeForm {
comment_id: inserted_comment.id, comment_id: inserted_comment.id,
@ -99,9 +99,9 @@ mod tests {
score: 1, score: 1,
}; };
CommentLike::like(&conn, &comment_like).unwrap(); CommentLike::like(&pool, &comment_like).unwrap();
let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap(); let comment_aggs_before_delete = CommentAggregates::read(&pool, inserted_comment.id).unwrap();
assert_eq!(1, comment_aggs_before_delete.score); assert_eq!(1, comment_aggs_before_delete.score);
assert_eq!(1, comment_aggs_before_delete.upvotes); assert_eq!(1, comment_aggs_before_delete.upvotes);
@ -115,35 +115,35 @@ mod tests {
score: -1, score: -1,
}; };
CommentLike::like(&conn, &comment_dislike).unwrap(); CommentLike::like(&pool, &comment_dislike).unwrap();
let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap(); let comment_aggs_after_dislike = CommentAggregates::read(&pool, inserted_comment.id).unwrap();
assert_eq!(0, comment_aggs_after_dislike.score); assert_eq!(0, comment_aggs_after_dislike.score);
assert_eq!(1, comment_aggs_after_dislike.upvotes); assert_eq!(1, comment_aggs_after_dislike.upvotes);
assert_eq!(1, comment_aggs_after_dislike.downvotes); assert_eq!(1, comment_aggs_after_dislike.downvotes);
// Remove the first comment like // Remove the first comment like
CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap(); CommentLike::remove(&pool, inserted_person.id, inserted_comment.id).unwrap();
let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap(); let after_like_remove = CommentAggregates::read(&pool, inserted_comment.id).unwrap();
assert_eq!(-1, after_like_remove.score); assert_eq!(-1, after_like_remove.score);
assert_eq!(0, after_like_remove.upvotes); assert_eq!(0, after_like_remove.upvotes);
assert_eq!(1, after_like_remove.downvotes); assert_eq!(1, after_like_remove.downvotes);
// Remove the parent post // Remove the parent post
Post::delete(&conn, inserted_post.id).unwrap(); Post::delete(&pool, inserted_post.id).unwrap();
// Should be none found, since the post was deleted // Should be none found, since the post was deleted
let after_delete = CommentAggregates::read(&conn, inserted_comment.id); let after_delete = CommentAggregates::read(&pool, inserted_comment.id);
assert!(after_delete.is_err()); assert!(after_delete.is_err());
// This should delete all the associated rows, and fire triggers // This should delete all the associated rows, and fire triggers
Person::delete(&conn, another_inserted_person.id).unwrap(); Person::delete(&pool, another_inserted_person.id).unwrap();
let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap(); let person_num_deleted = Person::delete(&pool, inserted_person.id).unwrap();
assert_eq!(1, person_num_deleted); assert_eq!(1, person_num_deleted);
// Delete the community // Delete the community
let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap(); let community_num_deleted = Community::delete(&pool, inserted_community.id).unwrap();
assert_eq!(1, community_num_deleted); assert_eq!(1, community_num_deleted);
} }
} }

View File

@ -12,30 +12,34 @@ extern crate diesel_migrations;
#[cfg(test)] #[cfg(test)]
extern crate serial_test; extern crate serial_test;
use diesel::{result::Error, *}; use diesel::{*, r2d2::{ConnectionManager, Pool}, result::Error};
use lemmy_db_schema::{CommunityId, DbUrl, PersonId}; use lemmy_db_schema::{CommunityId, DbUrl, PersonId};
use lemmy_utils::ApiError; use lemmy_utils::{ApiError, settings::structs::Settings};
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{env, env::VarError}; use std::{env, env::VarError};
use url::Url; use url::Url;
use core::pin::Pin;
use core::future::Future;
use tokio_diesel::*;
pub mod aggregates; pub mod aggregates;
pub mod source; pub mod source;
pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>; pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
pub type TokioDieselFuture<'a, T>= Pin<Box<dyn Future<Output = Result<T, AsyncError>> + Send + 'a>>;
pub trait Crud<Form, IdType> { pub trait Crud<'a, Form, IdType> {
fn create(conn: &PgConnection, form: &Form) -> Result<Self, Error> fn create(pool: &'a DbPool, form: &'a Form) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn read(conn: &PgConnection, id: IdType) -> Result<Self, Error> fn read(pool: &'a DbPool, id: IdType) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn update(conn: &PgConnection, id: IdType, form: &Form) -> Result<Self, Error> fn update(pool: &'a DbPool, id: IdType, form: &'a Form) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn delete(_conn: &PgConnection, _id: IdType) -> Result<usize, Error> fn delete(_pool: &'a DbPool, _id: IdType) -> TokioDieselFuture<'a, usize>
where where
Self: Sized, Self: Sized,
{ {
@ -43,76 +47,76 @@ pub trait Crud<Form, IdType> {
} }
} }
pub trait Followable<Form> { pub trait Followable<'a, Form> {
fn follow(conn: &PgConnection, form: &Form) -> Result<Self, Error> fn follow(pool: &'a DbPool, form: &Form) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn follow_accepted( fn follow_accepted(
conn: &PgConnection, pool: &'a DbPool,
community_id: CommunityId, community_id: CommunityId,
person_id: PersonId, person_id: PersonId,
) -> Result<Self, Error> ) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn unfollow(conn: &PgConnection, form: &Form) -> Result<usize, Error> fn unfollow(pool: &'a DbPool, form: &Form) -> TokioDieselFuture<'a, usize>
where where
Self: Sized; Self: Sized;
fn has_local_followers(conn: &PgConnection, community_id: CommunityId) -> Result<bool, Error>; fn has_local_followers(pool: &'a DbPool, community_id: CommunityId) -> Result<bool, Error>;
} }
pub trait Joinable<Form> { pub trait Joinable<'a, Form> {
fn join(conn: &PgConnection, form: &Form) -> Result<Self, Error> fn join(pool: &'a DbPool, form: &Form) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn leave(conn: &PgConnection, form: &Form) -> Result<usize, Error> fn leave(pool: &'a DbPool, form: &Form) -> TokioDieselFuture<'a, usize>
where where
Self: Sized; Self: Sized;
} }
pub trait Likeable<Form, IdType> { pub trait Likeable<'a, Form, IdType> {
fn like(conn: &PgConnection, form: &Form) -> Result<Self, Error> fn like(pool: &'a DbPool, form: &'a Form) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn remove(conn: &PgConnection, person_id: PersonId, item_id: IdType) -> Result<usize, Error> fn remove(pool: &'a DbPool, person_id: PersonId, item_id: IdType) -> TokioDieselFuture<'a, usize>
where where
Self: Sized; Self: Sized;
} }
pub trait Bannable<Form> { pub trait Bannable<'a, Form> {
fn ban(conn: &PgConnection, form: &Form) -> Result<Self, Error> fn ban(pool: &'a DbPool, form: &'a Form) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn unban(conn: &PgConnection, form: &Form) -> Result<usize, Error> fn unban(pool: &'a DbPool, form: &'a Form) -> TokioDieselFuture<'a, usize>
where where
Self: Sized; Self: Sized;
} }
pub trait Saveable<Form> { pub trait Saveable<'a, Form> {
fn save(conn: &PgConnection, form: &Form) -> Result<Self, Error> fn save(pool: &'a DbPool, form: &'a Form) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn unsave(conn: &PgConnection, form: &Form) -> Result<usize, Error> fn unsave(pool: &'a DbPool, form: &'a Form) -> TokioDieselFuture<'a, usize>
where where
Self: Sized; Self: Sized;
} }
pub trait Readable<Form> { pub trait Readable<'a, Form> {
fn mark_as_read(conn: &PgConnection, form: &Form) -> Result<Self, Error> fn mark_as_read(pool: &'a DbPool, form: &'a Form) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn mark_as_unread(conn: &PgConnection, form: &Form) -> Result<usize, Error> fn mark_as_unread(pool: &'a DbPool, form: &'a Form) -> TokioDieselFuture<'a, usize>
where where
Self: Sized; Self: Sized;
} }
pub trait Reportable<Form> { pub trait Reportable<'a, Form> {
fn report(conn: &PgConnection, form: &Form) -> Result<Self, Error> fn report(pool: &'a DbPool, form: &'a Form) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn resolve(conn: &PgConnection, report_id: i32, resolver_id: PersonId) -> Result<usize, Error> fn resolve(pool: &'a DbPool, report_id: i32, resolver_id: PersonId) -> TokioDieselFuture<'a, usize>
where where
Self: Sized; Self: Sized;
fn unresolve(conn: &PgConnection, report_id: i32, resolver_id: PersonId) -> Result<usize, Error> fn unresolve(pool: &'a DbPool, report_id: i32, resolver_id: PersonId) -> TokioDieselFuture<'a, usize>
where where
Self: Sized; Self: Sized;
} }
@ -121,11 +125,11 @@ pub trait DeleteableOrRemoveable {
fn blank_out_deleted_or_removed_info(self) -> Self; fn blank_out_deleted_or_removed_info(self) -> Self;
} }
pub trait ApubObject<Form> { pub trait ApubObject<'a, Form> {
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> fn read_from_apub_id(pool: &'a DbPool, object_id: &'a DbUrl) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
fn upsert(conn: &PgConnection, user_form: &Form) -> Result<Self, Error> fn upsert(pool: &'a DbPool, user_form: &'a Form) -> TokioDieselFuture<'a, Self>
where where
Self: Sized; Self: Sized;
} }
@ -249,7 +253,17 @@ pub fn diesel_option_overwrite_to_url(
embed_migrations!(); embed_migrations!();
pub fn establish_unpooled_connection() -> PgConnection { /// Set up the r2d2 connection pool
pub fn setup_connection_pool() -> DbPool {
let db_url = match get_database_url_from_env() {
Ok(url) => url,
Err(_) => Settings::get().get_database_url(),
};
build_connection_pool(&db_url, Settings::get().database().pool_size())
}
/// Set up the r2d2 connection pool for tests
pub fn setup_connection_pool_for_tests() -> DbPool {
let db_url = match get_database_url_from_env() { let db_url = match get_database_url_from_env() {
Ok(url) => url, Ok(url) => url,
Err(e) => panic!( Err(e) => panic!(
@ -257,10 +271,21 @@ pub fn establish_unpooled_connection() -> PgConnection {
e e
), ),
}; };
let conn = build_connection_pool(&db_url, 10)
PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url)); }
fn build_connection_pool(db_url: &str, pool_size: u32) -> DbPool {
let manager = ConnectionManager::<PgConnection>::new(db_url);
let pool = Pool::builder()
.max_size(pool_size)
.build(manager)
.unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
let conn = pool.get().expect("Missing connection in pool");
// Run the migrations
embedded_migrations::run(&conn).expect("load migrations"); embedded_migrations::run(&conn).expect("load migrations");
conn
pool
} }
lazy_static! { lazy_static! {

View File

@ -1,4 +1,5 @@
use crate::{ApubObject, Crud, DeleteableOrRemoveable, Likeable, Readable, Saveable}; use crate::{ApubObject, Crud, DbPool, TokioDieselFuture, DeleteableOrRemoveable, Likeable, Readable, Saveable};
use tokio_diesel::*;
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{ use lemmy_db_schema::{
naive_now, naive_now,
@ -18,86 +19,85 @@ use lemmy_db_schema::{
PostId, PostId,
}; };
impl Crud<PostForm, PostId> for Post { impl<'a> Crud<'a, PostForm, PostId> for Post {
fn read(conn: &PgConnection, post_id: PostId) -> Result<Self, Error> { fn read(pool: &'a DbPool, post_id: PostId) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
post.find(post_id).first::<Self>(conn) post.find(post_id).first_async(pool)
} }
fn delete(conn: &PgConnection, post_id: PostId) -> Result<usize, Error> { fn delete(pool: &'a DbPool, post_id: PostId) -> TokioDieselFuture<'a, usize> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
diesel::delete(post.find(post_id)).execute(conn) diesel::delete(post.find(post_id)).execute_async(pool)
} }
fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> { fn create(pool: &'a DbPool, new_post: &'a PostForm) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
insert_into(post).values(new_post).get_result::<Self>(conn) insert_into(post).values(new_post).get_result_async(pool)
} }
fn update(conn: &PgConnection, post_id: PostId, new_post: &PostForm) -> Result<Self, Error> { fn update(pool: &'a DbPool, post_id: PostId, new_post: &'a PostForm) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set(new_post) .set(new_post)
.get_result::<Self>(conn) .get_result_async(pool)
} }
} }
pub trait Post_ { pub trait Post_<'a>{
//fn read(conn: &PgConnection, post_id: i32) -> Result<Post, Error>;
fn list_for_community( fn list_for_community(
conn: &PgConnection, pool: &'a DbPool,
the_community_id: CommunityId, the_community_id: CommunityId,
) -> Result<Vec<Post>, Error>; ) -> TokioDieselFuture<'a, Vec<Post>>;
fn update_ap_id(conn: &PgConnection, post_id: PostId, apub_id: DbUrl) -> Result<Post, Error>; fn update_ap_id(pool: &'a DbPool, post_id: PostId, apub_id: DbUrl) -> TokioDieselFuture<'a, Post>;
fn permadelete_for_creator( fn permadelete_for_creator(
conn: &PgConnection, pool: &'a DbPool,
for_creator_id: PersonId, for_creator_id: PersonId,
) -> Result<Vec<Post>, Error>; ) -> TokioDieselFuture<'a, Vec<Post>>;
fn update_deleted(conn: &PgConnection, post_id: PostId, new_deleted: bool) fn update_deleted(pool: &'a DbPool, post_id: PostId, new_deleted: bool)
-> Result<Post, Error>; -> TokioDieselFuture<'a, Post>;
fn update_removed(conn: &PgConnection, post_id: PostId, new_removed: bool) fn update_removed(pool: &'a DbPool, post_id: PostId, new_removed: bool)
-> Result<Post, Error>; -> TokioDieselFuture<'a, Post>;
fn update_removed_for_creator( fn update_removed_for_creator(
conn: &PgConnection, pool: &'a DbPool,
for_creator_id: PersonId, for_creator_id: PersonId,
for_community_id: Option<CommunityId>, for_community_id: Option<CommunityId>,
new_removed: bool, new_removed: bool,
) -> Result<Vec<Post>, Error>; ) -> TokioDieselFuture<'a, Vec<Post>>;
fn update_locked(conn: &PgConnection, post_id: PostId, new_locked: bool) -> Result<Post, Error>; fn update_locked(pool: &'a DbPool, post_id: PostId, new_locked: bool) -> TokioDieselFuture<'a, Post>;
fn update_stickied( fn update_stickied(
conn: &PgConnection, pool: &'a DbPool,
post_id: PostId, post_id: PostId,
new_stickied: bool, new_stickied: bool,
) -> Result<Post, Error>; ) -> TokioDieselFuture<'a, Post>;
fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool; fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool;
} }
impl Post_ for Post { impl<'a> Post_<'a> for Post {
fn list_for_community( fn list_for_community(
conn: &PgConnection, pool: &'a DbPool,
the_community_id: CommunityId, the_community_id: CommunityId,
) -> Result<Vec<Self>, Error> { ) -> TokioDieselFuture<'a, Vec<Self>> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
post post
.filter(community_id.eq(the_community_id)) .filter(community_id.eq(the_community_id))
.then_order_by(published.desc()) .then_order_by(published.desc())
.then_order_by(stickied.desc()) .then_order_by(stickied.desc())
.limit(20) .limit(20)
.load::<Self>(conn) .load_async(pool)
} }
fn update_ap_id(conn: &PgConnection, post_id: PostId, apub_id: DbUrl) -> Result<Self, Error> { fn update_ap_id(pool: &'a DbPool, post_id: PostId, apub_id: DbUrl) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set(ap_id.eq(apub_id)) .set(ap_id.eq(apub_id))
.get_result::<Self>(conn) .get_result_async(pool)
} }
fn permadelete_for_creator( fn permadelete_for_creator(
conn: &PgConnection, pool: &'a DbPool,
for_creator_id: PersonId, for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> { ) -> TokioDieselFuture<'a, Vec<Self>> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
let perma_deleted = "*Permananently Deleted*"; let perma_deleted = "*Permananently Deleted*";
@ -111,37 +111,37 @@ impl Post_ for Post {
deleted.eq(true), deleted.eq(true),
updated.eq(naive_now()), updated.eq(naive_now()),
)) ))
.get_results::<Self>(conn) .get_results_async(pool)
} }
fn update_deleted( fn update_deleted(
conn: &PgConnection, pool: &'a DbPool,
post_id: PostId, post_id: PostId,
new_deleted: bool, new_deleted: bool,
) -> Result<Self, Error> { ) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set((deleted.eq(new_deleted), updated.eq(naive_now()))) .set((deleted.eq(new_deleted), updated.eq(naive_now())))
.get_result::<Self>(conn) .get_result_async(pool)
} }
fn update_removed( fn update_removed(
conn: &PgConnection, pool: &'a DbPool,
post_id: PostId, post_id: PostId,
new_removed: bool, new_removed: bool,
) -> Result<Self, Error> { ) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set((removed.eq(new_removed), updated.eq(naive_now()))) .set((removed.eq(new_removed), updated.eq(naive_now())))
.get_result::<Self>(conn) .get_result_async(pool)
} }
fn update_removed_for_creator( fn update_removed_for_creator(
conn: &PgConnection, pool: &'a DbPool,
for_creator_id: PersonId, for_creator_id: PersonId,
for_community_id: Option<CommunityId>, for_community_id: Option<CommunityId>,
new_removed: bool, new_removed: bool,
) -> Result<Vec<Self>, Error> { ) -> TokioDieselFuture<'a, Vec<Self>> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
let mut update = diesel::update(post).into_boxed(); let mut update = diesel::update(post).into_boxed();
@ -153,25 +153,25 @@ impl Post_ for Post {
update update
.set((removed.eq(new_removed), updated.eq(naive_now()))) .set((removed.eq(new_removed), updated.eq(naive_now())))
.get_results::<Self>(conn) .get_results_async(pool)
} }
fn update_locked(conn: &PgConnection, post_id: PostId, new_locked: bool) -> Result<Self, Error> { fn update_locked(pool: &'a DbPool, post_id: PostId, new_locked: bool) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set(locked.eq(new_locked)) .set(locked.eq(new_locked))
.get_result::<Self>(conn) .get_result_async(pool)
} }
fn update_stickied( fn update_stickied(
conn: &PgConnection, pool: &'a DbPool,
post_id: PostId, post_id: PostId,
new_stickied: bool, new_stickied: bool,
) -> Result<Self, Error> { ) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set(stickied.eq(new_stickied)) .set(stickied.eq(new_stickied))
.get_result::<Self>(conn) .get_result_async(pool)
} }
fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool { fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool {
@ -179,84 +179,84 @@ impl Post_ for Post {
} }
} }
impl ApubObject<PostForm> for Post { impl<'a> ApubObject<'a, PostForm> for Post {
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> { fn read_from_apub_id(pool: &'a DbPool, object_id: &'a DbUrl) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
post.filter(ap_id.eq(object_id)).first::<Self>(conn) post.filter(ap_id.eq(object_id)).first_async(pool)
} }
fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> { fn upsert(pool: &'a DbPool, post_form: &'a PostForm) -> TokioDieselFuture<'a, Post> {
use lemmy_db_schema::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
insert_into(post) insert_into(post)
.values(post_form) .values(post_form)
.on_conflict(ap_id) .on_conflict(ap_id)
.do_update() .do_update()
.set(post_form) .set(post_form)
.get_result::<Self>(conn) .get_result_async(pool)
} }
} }
impl Likeable<PostLikeForm, PostId> for PostLike { impl<'a> Likeable<'a, PostLikeForm, PostId> for PostLike {
fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> { fn like(pool: &'a DbPool, post_like_form: &'a PostLikeForm) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post_like::dsl::*; use lemmy_db_schema::schema::post_like::dsl::*;
insert_into(post_like) insert_into(post_like)
.values(post_like_form) .values(post_like_form)
.on_conflict((post_id, person_id)) .on_conflict((post_id, person_id))
.do_update() .do_update()
.set(post_like_form) .set(post_like_form)
.get_result::<Self>(conn) .get_result_async(pool)
} }
fn remove(conn: &PgConnection, person_id: PersonId, post_id: PostId) -> Result<usize, Error> { fn remove(pool: &'a DbPool, person_id: PersonId, post_id: PostId) -> TokioDieselFuture<'a, usize> {
use lemmy_db_schema::schema::post_like::dsl; use lemmy_db_schema::schema::post_like::dsl;
diesel::delete( diesel::delete(
dsl::post_like dsl::post_like
.filter(dsl::post_id.eq(post_id)) .filter(dsl::post_id.eq(post_id))
.filter(dsl::person_id.eq(person_id)), .filter(dsl::person_id.eq(person_id)),
) )
.execute(conn) .execute_async(pool)
} }
} }
impl Saveable<PostSavedForm> for PostSaved { impl<'a> Saveable<'a, PostSavedForm> for PostSaved {
fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> { fn save(pool: &'a DbPool, post_saved_form: &'a PostSavedForm) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post_saved::dsl::*; use lemmy_db_schema::schema::post_saved::dsl::*;
insert_into(post_saved) insert_into(post_saved)
.values(post_saved_form) .values(post_saved_form)
.on_conflict((post_id, person_id)) .on_conflict((post_id, person_id))
.do_update() .do_update()
.set(post_saved_form) .set(post_saved_form)
.get_result::<Self>(conn) .get_result_async(pool)
} }
fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> { fn unsave(pool: &'a DbPool, post_saved_form: &PostSavedForm) -> TokioDieselFuture<'a, usize> {
use lemmy_db_schema::schema::post_saved::dsl::*; use lemmy_db_schema::schema::post_saved::dsl::*;
diesel::delete( diesel::delete(
post_saved post_saved
.filter(post_id.eq(post_saved_form.post_id)) .filter(post_id.eq(post_saved_form.post_id))
.filter(person_id.eq(post_saved_form.person_id)), .filter(person_id.eq(post_saved_form.person_id)),
) )
.execute(conn) .execute_async(pool)
} }
} }
impl Readable<PostReadForm> for PostRead { impl<'a> Readable<'a, PostReadForm> for PostRead {
fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> { fn mark_as_read(pool: &'a DbPool, post_read_form: &'a PostReadForm) -> TokioDieselFuture<'a, Self> {
use lemmy_db_schema::schema::post_read::dsl::*; use lemmy_db_schema::schema::post_read::dsl::*;
insert_into(post_read) insert_into(post_read)
.values(post_read_form) .values(post_read_form)
.on_conflict((post_id, person_id)) .on_conflict((post_id, person_id))
.do_update() .do_update()
.set(post_read_form) .set(post_read_form)
.get_result::<Self>(conn) .get_result_async(pool)
} }
fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> { fn mark_as_unread(pool: &'a DbPool, post_read_form: &'a PostReadForm) -> TokioDieselFuture<'a, usize> {
use lemmy_db_schema::schema::post_read::dsl::*; use lemmy_db_schema::schema::post_read::dsl::*;
diesel::delete( diesel::delete(
post_read post_read
.filter(post_id.eq(post_read_form.post_id)) .filter(post_id.eq(post_read_form.post_id))
.filter(person_id.eq(post_read_form.person_id)), .filter(person_id.eq(post_read_form.person_id)),
) )
.execute(conn) .execute_async(pool)
} }
} }
@ -276,24 +276,22 @@ impl DeleteableOrRemoveable for Post {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{establish_unpooled_connection, source::post::*}; use crate::{setup_connection_pool_for_tests, source::post::*};
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{
community::{Community, CommunityForm}, community::{Community, CommunityForm},
person::*, person::*,
}; };
use serial_test::serial;
#[test] #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[serial] async fn test_crud() {
fn test_crud() { let pool = setup_connection_pool_for_tests();
let conn = establish_unpooled_connection();
let new_person = PersonForm { let new_person = PersonForm {
name: "jim".into(), name: "jim".into(),
..PersonForm::default() ..PersonForm::default()
}; };
let inserted_person = Person::create(&conn, &new_person).unwrap(); let inserted_person = Person::create(&pool, &new_person).await.unwrap();
let new_community = CommunityForm { let new_community = CommunityForm {
name: "test community_3".to_string(), name: "test community_3".to_string(),
@ -301,7 +299,7 @@ mod tests {
..CommunityForm::default() ..CommunityForm::default()
}; };
let inserted_community = Community::create(&conn, &new_community).unwrap(); let inserted_community = Community::create(&pool, &new_community).await.unwrap();
let new_post = PostForm { let new_post = PostForm {
name: "A test post".into(), name: "A test post".into(),
@ -310,7 +308,7 @@ mod tests {
..PostForm::default() ..PostForm::default()
}; };
let inserted_post = Post::create(&conn, &new_post).unwrap(); let inserted_post = Post::create(&pool, &new_post).await.unwrap();
let expected_post = Post { let expected_post = Post {
id: inserted_post.id, id: inserted_post.id,
@ -341,7 +339,7 @@ mod tests {
score: 1, score: 1,
}; };
let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap(); let inserted_post_like = PostLike::like(&pool, &post_like_form).await.unwrap();
let expected_post_like = PostLike { let expected_post_like = PostLike {
id: inserted_post_like.id, id: inserted_post_like.id,
@ -357,7 +355,7 @@ mod tests {
person_id: inserted_person.id, person_id: inserted_person.id,
}; };
let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap(); let inserted_post_saved = PostSaved::save(&pool, &post_saved_form).await.unwrap();
let expected_post_saved = PostSaved { let expected_post_saved = PostSaved {
id: inserted_post_saved.id, id: inserted_post_saved.id,
@ -372,7 +370,7 @@ mod tests {
person_id: inserted_person.id, person_id: inserted_person.id,
}; };
let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap(); let inserted_post_read = PostRead::mark_as_read(&pool, &post_read_form).await.unwrap();
let expected_post_read = PostRead { let expected_post_read = PostRead {
id: inserted_post_read.id, id: inserted_post_read.id,
@ -381,14 +379,14 @@ mod tests {
published: inserted_post_read.published, published: inserted_post_read.published,
}; };
let read_post = Post::read(&conn, inserted_post.id).unwrap(); let read_post = Post::read(&pool, inserted_post.id).await.unwrap();
let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap(); let updated_post = Post::update(&pool, inserted_post.id, &new_post).await.unwrap();
let like_removed = PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap(); let like_removed = PostLike::remove(&pool, inserted_person.id, inserted_post.id).await.unwrap();
let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap(); let saved_removed = PostSaved::unsave(&pool, &post_saved_form).await.unwrap();
let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap(); let read_removed = PostRead::mark_as_unread(&pool, &post_read_form).await.unwrap();
let num_deleted = Post::delete(&conn, inserted_post.id).unwrap(); let num_deleted = Post::delete(&pool, inserted_post.id).await.unwrap();
Community::delete(&conn, inserted_community.id).unwrap(); Community::delete(&pool, inserted_community.id).await.unwrap();
Person::delete(&conn, inserted_person.id).unwrap(); Person::delete(&pool, inserted_person.id).await.unwrap();
assert_eq!(expected_post, read_post); assert_eq!(expected_post, read_post);
assert_eq!(expected_post, inserted_post); assert_eq!(expected_post, inserted_post);

View File

@ -3,15 +3,11 @@ extern crate diesel_migrations;
use actix::prelude::*; use actix::prelude::*;
use actix_web::{web::Data, *}; use actix_web::{web::Data, *};
use diesel::{
r2d2::{ConnectionManager, Pool},
PgConnection,
};
use lemmy_api::match_websocket_operation; use lemmy_api::match_websocket_operation;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_api_crud::match_websocket_operation_crud; use lemmy_api_crud::match_websocket_operation_crud;
use lemmy_apub::activity_queue::create_activity_queue; use lemmy_apub::activity_queue::create_activity_queue;
use lemmy_db_queries::get_database_url_from_env; use lemmy_db_queries::setup_connection_pool;
use lemmy_routes::{feeds, images, nodeinfo, webfinger}; use lemmy_routes::{feeds, images, nodeinfo, webfinger};
use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks}; use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks};
use lemmy_utils::{ use lemmy_utils::{
@ -32,18 +28,11 @@ async fn main() -> Result<(), LemmyError> {
let settings = Settings::get(); let settings = Settings::get();
// Set up the r2d2 connection pool // Set up the r2d2 connection pool
let db_url = match get_database_url_from_env() { let pool = setup_connection_pool();
Ok(url) => url,
Err(_) => settings.get_database_url(),
};
let manager = ConnectionManager::<PgConnection>::new(&db_url);
let pool = Pool::builder()
.max_size(settings.database().pool_size())
.build(manager)
.unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
// Run the migrations from code // Run the migrations from code
blocking(&pool, move |conn| { blocking(&pool, move |conn| {
// TODO this is already done from the pool
embedded_migrations::run(conn)?; embedded_migrations::run(conn)?;
run_advanced_migrations(conn)?; run_advanced_migrations(conn)?;
Ok(()) as Result<(), LemmyError> Ok(()) as Result<(), LemmyError>