Skip to content

Commit 4b5b548

Browse files
jyn514Joshua Nelson
authored and
Joshua Nelson
committed
Connection -> Client
1 parent d8a2275 commit 4b5b548

17 files changed

+70
-97
lines changed

src/db/add_package.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
utils::MetadataPackage,
1313
};
1414
use log::{debug, info};
15-
use postgres::Client as Connection;
15+
use postgres::Client;
1616
use regex::Regex;
1717
use serde_json::Value;
1818
use slug::slugify;
@@ -25,7 +25,7 @@ use slug::slugify;
2525
/// not the files generated by rustdoc.
2626
#[allow(clippy::too_many_arguments)]
2727
pub(crate) fn add_package_into_database(
28-
conn: &mut Connection,
28+
conn: &mut Client,
2929
metadata_pkg: &MetadataPackage,
3030
source_dir: &Path,
3131
res: &BuildResult,
@@ -132,7 +132,7 @@ pub(crate) fn add_package_into_database(
132132

133133
/// Adds a build into database
134134
pub(crate) fn add_build_into_database(
135-
conn: &mut Connection,
135+
conn: &mut Client,
136136
release_id: i32,
137137
res: &BuildResult,
138138
) -> Result<i32> {
@@ -154,7 +154,7 @@ pub(crate) fn add_build_into_database(
154154
Ok(rows[0].get(0))
155155
}
156156

157-
fn initialize_package_in_database(conn: &mut Connection, pkg: &MetadataPackage) -> Result<i32> {
157+
fn initialize_package_in_database(conn: &mut Client, pkg: &MetadataPackage) -> Result<i32> {
158158
let mut rows = conn.query("SELECT id FROM crates WHERE name = $1", &[&pkg.name])?;
159159
// insert crate into database if it is not exists
160160
if rows.is_empty() {
@@ -250,7 +250,7 @@ fn read_rust_doc(file_path: &Path) -> Result<Option<String>> {
250250

251251
/// Adds keywords into database
252252
fn add_keywords_into_database(
253-
conn: &mut Connection,
253+
conn: &mut Client,
254254
pkg: &MetadataPackage,
255255
release_id: i32,
256256
) -> Result<()> {
@@ -281,7 +281,7 @@ fn add_keywords_into_database(
281281

282282
/// Adds authors into database
283283
fn add_authors_into_database(
284-
conn: &mut Connection,
284+
conn: &mut Client,
285285
pkg: &MetadataPackage,
286286
release_id: i32,
287287
) -> Result<()> {
@@ -326,7 +326,7 @@ fn add_authors_into_database(
326326
}
327327

328328
pub fn update_crate_data_in_database(
329-
conn: &mut Connection,
329+
conn: &mut Client,
330330
name: &str,
331331
registry_data: &CrateData,
332332
) -> Result<()> {
@@ -340,7 +340,7 @@ pub fn update_crate_data_in_database(
340340

341341
/// Adds owners into database
342342
fn update_owners_in_database(
343-
conn: &mut Connection,
343+
conn: &mut Client,
344344
owners: &[CrateOwner],
345345
crate_id: i32,
346346
) -> Result<()> {
@@ -407,11 +407,7 @@ fn update_owners_in_database(
407407
}
408408

409409
/// Add the compression algorithms used for this crate to the database
410-
fn add_compression_into_database<I>(
411-
conn: &mut Connection,
412-
algorithms: I,
413-
release_id: i32,
414-
) -> Result<()>
410+
fn add_compression_into_database<I>(conn: &mut Client, algorithms: I, release_id: i32) -> Result<()>
415411
where
416412
I: Iterator<Item = CompressionAlgorithm>,
417413
{

src/db/blacklist.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use failure::{Error, Fail};
2-
use postgres::Client as Connection;
2+
use postgres::Client;
33

44
#[derive(Debug, Fail)]
55
enum BlacklistError {
@@ -11,7 +11,7 @@ enum BlacklistError {
1111
}
1212

1313
/// Returns whether the given name is blacklisted.
14-
pub fn is_blacklisted(conn: &mut Connection, name: &str) -> Result<bool, Error> {
14+
pub fn is_blacklisted(conn: &mut Client, name: &str) -> Result<bool, Error> {
1515
let rows = conn.query(
1616
"SELECT COUNT(*) FROM blacklisted_crates WHERE crate_name = $1;",
1717
&[&name],
@@ -22,7 +22,7 @@ pub fn is_blacklisted(conn: &mut Connection, name: &str) -> Result<bool, Error>
2222
}
2323

2424
/// Returns the crate names on the blacklist, sorted ascending.
25-
pub fn list_crates(conn: &mut Connection) -> Result<Vec<String>, Error> {
25+
pub fn list_crates(conn: &mut Client) -> Result<Vec<String>, Error> {
2626
let rows = conn.query(
2727
"SELECT crate_name FROM blacklisted_crates ORDER BY crate_name asc;",
2828
&[],
@@ -32,7 +32,7 @@ pub fn list_crates(conn: &mut Connection) -> Result<Vec<String>, Error> {
3232
}
3333

3434
/// Adds a crate to the blacklist.
35-
pub fn add_crate(conn: &mut Connection, name: &str) -> Result<(), Error> {
35+
pub fn add_crate(conn: &mut Client, name: &str) -> Result<(), Error> {
3636
if is_blacklisted(conn, name)? {
3737
return Err(BlacklistError::CrateAlreadyOnBlacklist(name.into()).into());
3838
}
@@ -46,7 +46,7 @@ pub fn add_crate(conn: &mut Connection, name: &str) -> Result<(), Error> {
4646
}
4747

4848
/// Removes a crate from the blacklist.
49-
pub fn remove_crate(conn: &mut Connection, name: &str) -> Result<(), Error> {
49+
pub fn remove_crate(conn: &mut Client, name: &str) -> Result<(), Error> {
5050
if !is_blacklisted(conn, name)? {
5151
return Err(BlacklistError::CrateNotOnBlacklist(name.into()).into());
5252
}

src/db/delete.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::Storage;
22
use failure::{Error, Fail};
3-
use postgres::Client as Connection;
3+
use postgres::Client;
44

55
/// List of directories in docs.rs's underlying storage (either the database or S3) containing a
66
/// subdirectory named after the crate. Those subdirectories will be deleted.
@@ -12,7 +12,7 @@ enum CrateDeletionError {
1212
MissingCrate(String),
1313
}
1414

15-
pub fn delete_crate(conn: &mut Connection, storage: &Storage, name: &str) -> Result<(), Error> {
15+
pub fn delete_crate(conn: &mut Client, storage: &Storage, name: &str) -> Result<(), Error> {
1616
let crate_id = get_id(conn, name)?;
1717
delete_crate_from_database(conn, name, crate_id)?;
1818

@@ -24,7 +24,7 @@ pub fn delete_crate(conn: &mut Connection, storage: &Storage, name: &str) -> Res
2424
}
2525

2626
pub fn delete_version(
27-
conn: &mut Connection,
27+
conn: &mut Client,
2828
storage: &Storage,
2929
name: &str,
3030
version: &str,
@@ -38,7 +38,7 @@ pub fn delete_version(
3838
Ok(())
3939
}
4040

41-
fn get_id(conn: &mut Connection, name: &str) -> Result<i32, Error> {
41+
fn get_id(conn: &mut Client, name: &str) -> Result<i32, Error> {
4242
let crate_id_res = conn.query("SELECT id FROM crates WHERE name = $1", &[&name])?;
4343
if let Some(row) = crate_id_res.into_iter().next() {
4444
Ok(row.get("id"))
@@ -56,11 +56,7 @@ const METADATA: &[(&str, &str)] = &[
5656
("compression_rels", "release"),
5757
];
5858

59-
fn delete_version_from_database(
60-
conn: &mut Connection,
61-
name: &str,
62-
version: &str,
63-
) -> Result<(), Error> {
59+
fn delete_version_from_database(conn: &mut Client, name: &str, version: &str) -> Result<(), Error> {
6460
let crate_id = get_id(conn, name)?;
6561
let mut transaction = conn.transaction()?;
6662
for &(table, column) in METADATA {
@@ -92,11 +88,7 @@ fn delete_version_from_database(
9288
transaction.commit().map_err(Into::into)
9389
}
9490

95-
fn delete_crate_from_database(
96-
conn: &mut Connection,
97-
name: &str,
98-
crate_id: i32,
99-
) -> Result<(), Error> {
91+
fn delete_crate_from_database(conn: &mut Client, name: &str, crate_id: i32) -> Result<(), Error> {
10092
let mut transaction = conn.transaction()?;
10193

10294
transaction.execute(
@@ -128,15 +120,15 @@ mod tests {
128120
use super::*;
129121
use crate::test::{assert_success, wrapper};
130122
use failure::Error;
131-
use postgres::Client as Connection;
123+
use postgres::Client;
132124

133-
fn crate_exists(conn: &mut Connection, name: &str) -> Result<bool, Error> {
125+
fn crate_exists(conn: &mut Client, name: &str) -> Result<bool, Error> {
134126
Ok(!conn
135127
.query("SELECT * FROM crates WHERE name = $1;", &[&name])?
136128
.is_empty())
137129
}
138130

139-
fn release_exists(conn: &mut Connection, id: i32) -> Result<bool, Error> {
131+
fn release_exists(conn: &mut Client, id: i32) -> Result<bool, Error> {
140132
Ok(!conn
141133
.query("SELECT * FROM releases WHERE id = $1;", &[&id])?
142134
.is_empty())
@@ -187,7 +179,7 @@ mod tests {
187179
#[test]
188180
fn test_delete_version() {
189181
wrapper(|env| {
190-
fn authors(conn: &mut Connection, crate_id: i32) -> Result<Vec<String>, Error> {
182+
fn authors(conn: &mut Client, crate_id: i32) -> Result<Vec<String>, Error> {
191183
Ok(conn
192184
.query(
193185
"SELECT name FROM authors

src/db/migrate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::error::Result as CratesfyiResult;
44
use log::info;
5-
use postgres::{Client as Connection, Error as PostgresError, Transaction};
5+
use postgres::{Client, Error as PostgresError, Transaction};
66
use schemamama::{Migration, Migrator, Version};
77
use schemamama_postgres::{PostgresAdapter, PostgresMigration};
88

@@ -50,7 +50,7 @@ macro_rules! migration {
5050
}};
5151
}
5252

53-
pub fn migrate(version: Option<Version>, conn: &mut Connection) -> CratesfyiResult<()> {
53+
pub fn migrate(version: Option<Version>, conn: &mut Client) -> CratesfyiResult<()> {
5454
conn.execute(
5555
"CREATE TABLE IF NOT EXISTS database_versions (version BIGINT PRIMARY KEY);",
5656
&[],

src/db/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub(crate) use self::add_package::{add_build_into_database, add_package_into_dat
55
pub use self::delete::{delete_crate, delete_version};
66
pub use self::file::add_path_into_database;
77
pub use self::migrate::migrate;
8-
pub(crate) use self::pool::PoolConnection;
8+
pub(crate) use self::pool::PoolClient;
99
pub use self::pool::{Pool, PoolError};
1010

1111
mod add_package;

src/db/pool.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::Config;
2-
use postgres::{Client as Connection, NoTls};
2+
use postgres::{Client, NoTls};
33
use r2d2_postgres::PostgresConnectionManager;
44

5-
pub(crate) type PoolConnection = r2d2::PooledConnection<PostgresConnectionManager<NoTls>>;
5+
pub(crate) type PoolClient = r2d2::PooledConnection<PostgresConnectionManager<NoTls>>;
66

77
const DEFAULT_SCHEMA: &str = "public";
88

@@ -39,12 +39,12 @@ impl Pool {
3939
Ok(Pool { pool })
4040
}
4141

42-
pub fn get(&self) -> Result<PoolConnection, PoolError> {
42+
pub fn get(&self) -> Result<PoolClient, PoolError> {
4343
match self.pool.get() {
4444
Ok(conn) => Ok(conn),
4545
Err(err) => {
4646
crate::web::metrics::FAILED_DB_CONNECTIONS.inc();
47-
Err(PoolError::ConnectionError(err))
47+
Err(PoolError::ClientError(err))
4848
}
4949
}
5050
}
@@ -71,8 +71,8 @@ impl SetSchema {
7171
}
7272
}
7373

74-
impl r2d2::CustomizeConnection<Connection, postgres::Error> for SetSchema {
75-
fn on_acquire(&self, conn: &mut Connection) -> Result<(), postgres::Error> {
74+
impl r2d2::CustomizeConnection<Client, postgres::Error> for SetSchema {
75+
fn on_acquire(&self, conn: &mut Client) -> Result<(), postgres::Error> {
7676
if self.schema != DEFAULT_SCHEMA {
7777
conn.execute(
7878
format!("SET search_path TO {}, {};", self.schema, DEFAULT_SCHEMA).as_str(),
@@ -92,5 +92,5 @@ pub enum PoolError {
9292
PoolCreationFailed(#[fail(cause)] r2d2::Error),
9393

9494
#[fail(display = "failed to get a database connection")]
95-
ConnectionError(#[fail(cause)] r2d2::Error),
95+
ClientError(#[fail(cause)] r2d2::Error),
9696
}

src/docbuilder/limits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::error::Result;
2-
use postgres::Client as Connection;
2+
use postgres::Client;
33
use serde::Serialize;
44
use std::time::Duration;
55

@@ -25,7 +25,7 @@ impl Default for Limits {
2525
}
2626

2727
impl Limits {
28-
pub(crate) fn for_crate(conn: &mut Connection, name: &str) -> Result<Self> {
28+
pub(crate) fn for_crate(conn: &mut Client, name: &str) -> Result<Self> {
2929
let mut limits = Self::default();
3030

3131
let res = conn.query(

src/storage/database.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,18 @@ impl DatabaseBackend {
6565
}
6666
}
6767

68-
pub(super) fn start_connection(&self) -> Result<DatabaseConnection, Error> {
69-
Ok(DatabaseConnection {
68+
pub(super) fn start_connection(&self) -> Result<DatabaseClient, Error> {
69+
Ok(DatabaseClient {
7070
conn: self.pool.get()?,
7171
})
7272
}
7373
}
7474

75-
pub(super) struct DatabaseConnection {
76-
conn: crate::db::PoolConnection,
75+
pub(super) struct DatabaseClient {
76+
conn: crate::db::PoolClient,
7777
}
7878

79-
impl DatabaseConnection {
79+
impl DatabaseClient {
8080
pub(super) fn start_storage_transaction(
8181
&mut self,
8282
) -> Result<DatabaseStorageTransaction<'_>, Error> {

src/test/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
mod fakes;
22

3-
use crate::db::{Pool, PoolConnection};
3+
use crate::db::{Pool, PoolClient};
44
use crate::storage::Storage;
55
use crate::web::Server;
66
use crate::BuildQueue;
77
use crate::Config;
88
use failure::Error;
99
use log::error;
1010
use once_cell::unsync::OnceCell;
11-
use postgres::Client as Connection;
11+
use postgres::Client;
1212
use reqwest::{
1313
blocking::{Client, RequestBuilder},
1414
Method,
@@ -229,7 +229,7 @@ impl TestDatabase {
229229
// test to create a fresh instance of the database to run within.
230230
let schema = format!("docs_rs_test_schema_{}", rand::random::<u64>());
231231

232-
let mut conn = Connection::connect(config.database_url.as_str(), postgres::TlsMode::None)?;
232+
let mut conn = Client::connect(config.database_url.as_str(), postgres::TlsMode::None)?;
233233
conn.batch_execute(&format!(
234234
"
235235
CREATE SCHEMA {0};
@@ -272,7 +272,7 @@ impl TestDatabase {
272272
self.pool.clone()
273273
}
274274

275-
pub(crate) fn conn(&self) -> PoolConnection {
275+
pub(crate) fn conn(&self) -> PoolClient {
276276
self.pool
277277
.get()
278278
.expect("failed to get a connection out of the pool")

src/utils/github_updater.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{db::Pool, Config};
33
use chrono::{DateTime, Utc};
44
use failure::err_msg;
55
use log::{debug, warn};
6-
use postgres::Client as Connection;
6+
use postgres::Client;
77
use regex::Regex;
88
use reqwest::header::{HeaderValue, ACCEPT, AUTHORIZATION, USER_AGENT};
99
use serde::Deserialize;
@@ -120,12 +120,7 @@ impl GithubUpdater {
120120
Ok(response.resources.core.remaining == 0)
121121
}
122122

123-
fn update_crate(
124-
&self,
125-
conn: &mut Connection,
126-
crate_id: i32,
127-
repository_url: &str,
128-
) -> Result<()> {
123+
fn update_crate(&self, conn: &mut Client, crate_id: i32, repository_url: &str) -> Result<()> {
129124
let path =
130125
get_github_path(repository_url).ok_or_else(|| err_msg("Failed to get github path"))?;
131126
let fields = self.get_github_fields(&path)?;

0 commit comments

Comments
 (0)