Skip to content

Commit b01a3d9

Browse files
committed
Make connection optionally injectable
1 parent b966cc5 commit b01a3d9

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/categories.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use diesel;
55
use diesel::prelude::*;
6+
use diesel::pg::PgConnection;
67
use toml;
78

89
use db;
@@ -99,11 +100,14 @@ struct NewCategory {
99100
}
100101

101102
pub fn sync(toml_str: &str) -> CargoResult<()> {
103+
let conn = db::connect_now().unwrap();
104+
sync_with_connection(toml_str, &conn)
105+
}
106+
107+
pub fn sync_with_connection(toml_str: &str, conn: &PgConnection) -> CargoResult<()> {
102108
use diesel::pg::upsert::*;
103109
use diesel::expression::dsl::all;
104110

105-
let conn = db::connect_now().unwrap();
106-
107111
let toml: toml::value::Table =
108112
toml::from_str(toml_str).expect("Could not parse categories toml");
109113

@@ -133,10 +137,10 @@ pub fn sync(toml_str: &str) -> CargoResult<()> {
133137
let slugs = diesel::insert(&to_insert)
134138
.into(categories::table)
135139
.returning(categories::slug)
136-
.get_results::<String>(&conn)?;
140+
.get_results::<String>(&*conn)?;
137141

138142
let to_delete = categories::table.filter(categories::slug.ne(all(slugs)));
139-
diesel::delete(to_delete).execute(&conn)?;
143+
diesel::delete(to_delete).execute(&*conn)?;
140144
Ok(())
141145
})
142146
}

0 commit comments

Comments
 (0)