Skip to content

Commit 18c5527

Browse files
committed
Make all crate name lookups case insensitive
1 parent 191f328 commit 18c5527

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/bin/migrate.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,19 @@ fn migrations() -> Vec<Migration> {
397397

398398
}, proc(_) Ok(())),
399399
Migration::add_column(20141120162357, "dependencies", "kind", "INTEGER"),
400+
Migration::new(20141121191309, proc(tx) {
401+
try!(tx.execute("ALTER TABLE crates DROP CONSTRAINT \
402+
packages_name_key", &[]));
403+
try!(tx.execute("CREATE UNIQUE INDEX index_crates_name \
404+
ON crates (lower(name))", &[]));
405+
Ok(())
406+
407+
}, proc(tx) {
408+
try!(tx.execute("DROP INDEX index_crates_name", &[]));
409+
try!(tx.execute("ALTER TABLE crates ADD CONSTRAINT packages_name_key \
410+
UNIQUE (name)", &[]));
411+
Ok(())
412+
}),
400413
];
401414
// NOTE: Generate a new id via `date +"%Y%m%d%H%M%S"`
402415

src/krate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl Crate {
7777

7878
pub fn find_by_name(conn: &Connection, name: &str) -> CargoResult<Crate> {
7979
let stmt = try!(conn.prepare("SELECT * FROM crates \
80-
WHERE name = $1 LIMIT 1"));
80+
WHERE lower(name) = lower($1) LIMIT 1"));
8181
match try!(stmt.query(&[&name as &ToSql])).next() {
8282
Some(row) => Ok(Model::from_row(&row)),
8383
None => Err(FromError::from_error(NotFound)),
@@ -114,7 +114,7 @@ impl Crate {
114114
keywords = $5,
115115
license = $6,
116116
repository = $7
117-
WHERE name = $8
117+
WHERE lower(name) = lower($8)
118118
RETURNING *"));
119119
let mut rows = try!(stmt.query(&[&documentation, &homepage,
120120
&description, &readme, &keywords,
@@ -666,7 +666,7 @@ pub fn download(req: &mut Request) -> CargoResult<Response> {
666666
FROM crates
667667
INNER JOIN versions ON
668668
crates.id = versions.crate_id
669-
WHERE crates.name = $1
669+
WHERE lower(crates.name) = lower($1)
670670
AND versions.num = $2
671671
LIMIT 1"));
672672
let mut rows = try!(stmt.query(&[&crate_name as &ToSql, &version as &ToSql]));

src/tests/krate.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,17 @@ fn new_krate_duplicate_version() {
321321
"{}", json.errors);
322322
}
323323

324+
#[test]
325+
fn new_crate_similar_name() {
326+
let (_b, app, middle) = ::app();
327+
let mut req = new_req(app, "foo", "1.0.0");
328+
::mock_user(&mut req, ::user("foo"));
329+
::mock_crate(&mut req, ::krate("Foo"));
330+
let json = bad_resp!(middle.call(&mut req));
331+
assert!(json.errors[0].detail.as_slice().contains("already uploaded"),
332+
"{}", json.errors);
333+
}
334+
324335
#[test]
325336
fn new_krate_git_upload() {
326337
let (_b, app, middle) = ::app();
@@ -428,6 +439,15 @@ fn download() {
428439
let mut resp = ok_resp!(middle.call(&mut req));
429440
let downloads = ::json::<Downloads>(&mut resp);
430441
assert_eq!(downloads.version_downloads.len(), 1);
442+
443+
req.with_path("/api/v1/crates/FOO/1.0.0/download");
444+
let resp = t_resp!(middle.call(&mut req));
445+
assert_eq!(resp.status.val0(), 302);
446+
447+
req.with_path("/api/v1/crates/FOO/1.0.0/downloads");
448+
let mut resp = ok_resp!(middle.call(&mut req));
449+
let downloads = ::json::<Downloads>(&mut resp);
450+
assert_eq!(downloads.version_downloads.len(), 1);
431451
}
432452

433453
#[test]

0 commit comments

Comments
 (0)