Skip to content

Commit a1a48f5

Browse files
committed
Sync categories on server startup
1 parent c0f5ee3 commit a1a48f5

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

src/bin/server.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ fn main() {
6262
let app = cargo_registry::App::new(&config);
6363
let app = cargo_registry::middleware(Arc::new(app));
6464

65+
cargo_registry::categories::sync().unwrap();
66+
6567
let port = if heroku {
6668
8888
6769
} else {
Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
// Sync available crate categories from `src/categories.txt`.
2-
// Only needs to be run for new databases or when `src/categories.txt`
3-
// has been updated.
4-
//
5-
// Usage:
6-
// cargo run --bin sync-categories
2+
// Runs when the server is started.
73

8-
#![deny(warnings)]
4+
use pg;
5+
use env;
6+
use util::errors::CargoResult;
97

10-
extern crate cargo_registry;
11-
extern crate postgres;
12-
13-
use cargo_registry::env;
14-
15-
fn main() {
16-
let conn = postgres::Connection::connect(&env("DATABASE_URL")[..],
17-
postgres::TlsMode::None).unwrap();
8+
pub fn sync() -> CargoResult<()> {
9+
let conn = pg::Connection::connect(&env("DATABASE_URL")[..],
10+
pg::TlsMode::None).unwrap();
1811
let tx = conn.transaction().unwrap();
19-
sync(&tx).unwrap();
20-
tx.set_commit();
21-
tx.finish().unwrap();
22-
}
2312

24-
fn sync(tx: &postgres::transaction::Transaction) -> postgres::Result<()> {
25-
let categories = include_str!("../categories.txt");
13+
let categories = include_str!("./categories.txt");
2614

2715
let slug_categories: Vec<_> = categories.lines().map(|c| {
2816
let mut parts = c.split(' ');
@@ -50,5 +38,7 @@ fn sync(tx: &postgres::transaction::Transaction) -> postgres::Result<()> {
5038
in_clause
5139
)[..]
5240
));
41+
tx.set_commit();
42+
tx.finish().unwrap();
5343
Ok(())
5444
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use conduit_middleware::MiddlewareBuilder;
5252
use util::{C, R, R404};
5353

5454
pub mod app;
55+
pub mod categories;
5556
pub mod category;
5657
pub mod config;
5758
pub mod db;

0 commit comments

Comments
 (0)