Skip to content

Commit 3a9b31d

Browse files
Merge #901
901: Port `bin/transfer-crates` over to Diesel r=carols10cents I noticed a bug in this script as part of porting it over. It was previously updating the `crate_owners` table without filtering on `owner_kind`, which would be very bad if there were a team with the same ID. I've fixed that bug as part of this port.
2 parents bbfd444 + 4de6feb commit 3a9b31d

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

src/bin/transfer-crates.rs

+33-29
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,28 @@
66
#![deny(warnings)]
77

88
extern crate cargo_registry;
9-
extern crate postgres;
10-
extern crate time;
9+
extern crate diesel;
1110
extern crate semver;
1211

12+
use diesel::prelude::*;
13+
use diesel::pg::PgConnection;
1314
use std::env;
1415
use std::io;
1516
use std::io::prelude::*;
1617

1718
use cargo_registry::{Crate, User};
1819
use cargo_registry::owner::OwnerKind;
19-
use cargo_registry::Model;
20+
use cargo_registry::schema::*;
2021

21-
#[allow(dead_code)]
2222
fn main() {
23-
let conn = cargo_registry::db::connect_now_old();
24-
{
25-
let tx = conn.transaction().unwrap();
26-
transfer(&tx);
27-
tx.set_commit();
28-
tx.finish().unwrap();
29-
}
23+
let conn = cargo_registry::db::connect_now().unwrap();
24+
conn.transaction::<_, diesel::result::Error, _>(|| {
25+
transfer(&conn);
26+
Ok(())
27+
}).unwrap()
3028
}
3129

32-
fn transfer(tx: &postgres::transaction::Transaction) {
30+
fn transfer(conn: &PgConnection) {
3331
let from = match env::args().nth(1) {
3432
None => {
3533
println!("needs a from-user argument");
@@ -45,8 +43,14 @@ fn transfer(tx: &postgres::transaction::Transaction) {
4543
Some(s) => s,
4644
};
4745

48-
let from = User::find_by_login(tx, &from).unwrap();
49-
let to = User::find_by_login(tx, &to).unwrap();
46+
let from = users::table
47+
.filter(users::gh_login.eq(from))
48+
.first::<User>(conn)
49+
.unwrap();
50+
let to = users::table
51+
.filter(users::gh_login.eq(to))
52+
.first::<User>(conn)
53+
.unwrap();
5054

5155
if from.gh_id != to.gh_id {
5256
println!("====================================================");
@@ -67,27 +71,27 @@ fn transfer(tx: &postgres::transaction::Transaction) {
6771
);
6872
get_confirm("continue");
6973

70-
let stmt = tx.prepare(
71-
"SELECT * FROM crate_owners
72-
WHERE owner_id = $1
73-
AND owner_kind = $2",
74-
).unwrap();
75-
let rows = stmt.query(&[&from.id, &(OwnerKind::User as i32)]).unwrap();
74+
let crate_owners = crate_owners::table
75+
.filter(crate_owners::owner_id.eq(from.id))
76+
.filter(crate_owners::owner_kind.eq(OwnerKind::User as i32));
77+
let crates = Crate::all()
78+
.filter(crates::id.eq_any(
79+
crate_owners.select(crate_owners::crate_id),
80+
))
81+
.load::<Crate>(conn)
82+
.unwrap();
7683

77-
for row in rows.iter() {
78-
let krate = Crate::find(tx, row.get("crate_id")).unwrap();
79-
println!("transferring {}", krate.name);
80-
let owners = krate.owners_old(tx).unwrap();
84+
for krate in crates {
85+
let owners = krate.owners(conn).unwrap();
8186
if owners.len() != 1 {
8287
println!("warning: not exactly one owner for {}", krate.name);
8388
}
8489
}
8590

86-
tx.execute(
87-
"UPDATE crate_owners SET owner_id = $1
88-
WHERE owner_id = $2",
89-
&[&to.id, &from.id],
90-
).unwrap();
91+
diesel::update(crate_owners)
92+
.set(crate_owners::owner_id.eq(to.id))
93+
.execute(conn)
94+
.unwrap();
9195

9296
get_confirm("commit?");
9397
}

0 commit comments

Comments
 (0)