Skip to content

Commit 13e1ce9

Browse files
Merge pull request #385 from Mark-Simulacrum/migration-fixes
Migration fixes
2 parents 8b53e64 + e7b6626 commit 13e1ce9

File tree

2 files changed

+58
-38
lines changed

2 files changed

+58
-38
lines changed

src/db/file.rs

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,53 @@ pub struct Blob {
6363
}
6464

6565
pub fn get_path(conn: &Connection, path: &str) -> Option<Blob> {
66-
let rows = conn.query("SELECT path, mime, date_updated, content
67-
FROM files
68-
WHERE path = $1", &[&path]).unwrap();
66+
if let Some(client) = s3_client() {
67+
let res = client.get_object(GetObjectRequest {
68+
bucket: "rust-docs-rs".into(),
69+
key: path.into(),
70+
..Default::default()
71+
}).sync();
6972

70-
if rows.len() == 0 {
71-
None
72-
} else {
73-
let row = rows.get(0);
74-
let mut content = row.get(3);
75-
if content == b"in-s3" {
76-
let client = s3_client();
77-
content = client.and_then(|c| c.get_object(GetObjectRequest {
78-
bucket: "rust-docs-rs".into(),
79-
key: path.into(),
80-
..Default::default()
81-
}).sync().ok()).and_then(|r| r.body).map(|b| {
82-
let mut b = b.into_blocking_read();
83-
let mut content = Vec::new();
84-
b.read_to_end(&mut content).unwrap();
85-
content
86-
}).unwrap();
73+
let res = match res {
74+
Ok(r) => r,
75+
Err(err) => {
76+
debug!("error fetching {}: {:?}", path, err);
77+
return None;
78+
}
8779
};
8880

81+
let mut b = res.body.unwrap().into_blocking_read();
82+
let mut content = Vec::new();
83+
b.read_to_end(&mut content).unwrap();
84+
85+
let last_modified = res.last_modified.unwrap();
86+
let last_modified = time::strptime(&last_modified, "%a, %d %b %Y %H:%M:%S %Z")
87+
.unwrap_or_else(|e| panic!("failed to parse {:?} as timespec: {:?}", last_modified, e))
88+
.to_timespec();
89+
8990
Some(Blob {
90-
path: row.get(0),
91-
mime: row.get(1),
92-
date_updated: row.get(2),
91+
path: path.into(),
92+
mime: res.content_type.unwrap(),
93+
date_updated: last_modified,
9394
content,
9495
})
96+
} else {
97+
let rows = conn.query("SELECT path, mime, date_updated, content
98+
FROM files
99+
WHERE path = $1", &[&path]).unwrap();
100+
101+
if rows.len() == 0 {
102+
None
103+
} else {
104+
let row = rows.get(0);
105+
106+
Some(Blob {
107+
path: row.get(0),
108+
mime: row.get(1),
109+
date_updated: row.get(2),
110+
content: row.get(3),
111+
})
112+
}
95113
}
96114
}
97115

@@ -201,18 +219,21 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
201219
)
202220
};
203221

204-
// check if file already exists in database
205-
let rows = try!(conn.query("SELECT COUNT(*) FROM files WHERE path = $1", &[&path]));
222+
// If AWS credentials are configured, don't insert/update the database
223+
if client.is_none() {
224+
// check if file already exists in database
225+
let rows = try!(conn.query("SELECT COUNT(*) FROM files WHERE path = $1", &[&path]));
206226

207-
let content = content.unwrap_or_else(|| "in-s3".to_owned().into());
227+
let content = content.expect("content never None if client is None");
208228

209-
if rows.get(0).get::<usize, i64>(0) == 0 {
210-
try!(trans.query("INSERT INTO files (path, mime, content) VALUES ($1, $2, $3)",
211-
&[&path, &mime, &content]));
212-
} else {
213-
try!(trans.query("UPDATE files SET mime = $2, content = $3, date_updated = NOW() \
214-
WHERE path = $1",
215-
&[&path, &mime, &content]));
229+
if rows.get(0).get::<usize, i64>(0) == 0 {
230+
try!(trans.query("INSERT INTO files (path, mime, content) VALUES ($1, $2, $3)",
231+
&[&path, &mime, &content]));
232+
} else {
233+
try!(trans.query("UPDATE files SET mime = $2, content = $3, date_updated = NOW() \
234+
WHERE path = $1",
235+
&[&path, &mime, &content]));
236+
}
216237
}
217238
}
218239

@@ -269,8 +290,7 @@ pub fn move_to_s3(conn: &Connection, n: usize) -> Result<usize> {
269290
use ::futures::future::Future;
270291
match rt.block_on(::futures::future::join_all(futures)) {
271292
Ok(paths) => {
272-
let statement = trans.prepare("UPDATE files SET content = E'in-s3' WHERE path = $1")
273-
.unwrap();
293+
let statement = trans.prepare("DELETE FROM files WHERE path = $1").unwrap();
274294
for path in paths {
275295
statement.execute(&[&path]).unwrap();
276296
}

src/docbuilder/options.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Default for DocBuilderOptions {
3737
crates_io_index_path: crates_io_index_path,
3838

3939
chroot_user: "cratesfyi".to_string(),
40-
container_name: "cratesfyi-container".to_string(),
40+
container_name: env::var("CRATESFYI_CONTAINER_NAME").expect("CRATESFYI_CONTAINER_NAME"),
4141

4242
keep_build_directory: false,
4343
skip_if_exists: false,
@@ -104,9 +104,9 @@ impl DocBuilderOptions {
104104

105105

106106
fn generate_paths(prefix: PathBuf) -> (PathBuf, PathBuf, PathBuf, PathBuf) {
107-
107+
let name = env::var_os("CRATESFYI_CONTAINER_NAME").expect("CRATESFYI_CONTAINER_NAME");
108108
let destination = PathBuf::from(&prefix).join("documentations");
109-
let chroot_path = PathBuf::from(&prefix).join("cratesfyi-container/rootfs");
109+
let chroot_path = PathBuf::from(&prefix).join(&name).join("rootfs");
110110
let crates_io_index_path = PathBuf::from(&prefix).join("crates.io-index");
111111

112112
(prefix, destination, chroot_path, crates_io_index_path)

0 commit comments

Comments
 (0)