Skip to content

Commit b2315e4

Browse files
committed
Keep track of a package's maximum version
1 parent 49a235e commit b2315e4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/bin/migrate.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use std::os;
77
use migrate::Migration;
88
use postgres::{PostgresTransaction, PostgresResult};
99

10+
use cargo_registry::package::Package;
11+
1012
fn main() {
1113
let db_config = r2d2::Config {
1214
pool_size: 1,
@@ -137,5 +139,29 @@ fn migrations() -> Vec<Migration> {
137139
DROP DEFAULT", []));
138140
Ok(())
139141
}, proc(_) Ok(())),
142+
Migration::add_column(20140926130044, "packages", "max_version",
143+
"VARCHAR"),
144+
Migration::new(20140926130045, proc(tx) {
145+
let stmt = try!(tx.prepare("SELECT * FROM packages"));
146+
for row in try!(stmt.query(&[])) {
147+
let pkg = Package::from_row(&row);
148+
let versions = pkg.versions(tx).unwrap();
149+
let v = versions.iter().max_by(|v| &v.num).unwrap();
150+
let max = v.num.to_string();
151+
try!(tx.execute("UPDATE packages SET max_version = $1 \
152+
WHERE id = $2",
153+
&[&max, &pkg.id]));
154+
}
155+
Ok(())
156+
}, proc(_) Ok(())),
157+
Migration::new(20140926130046, proc(tx) {
158+
try!(tx.execute("ALTER TABLE versions ALTER COLUMN downloads \
159+
SET NOT NULL", []));
160+
Ok(())
161+
}, proc(tx) {
162+
try!(tx.execute("ALTER TABLE versions ALTER COLUMN downloads \
163+
DROP NOT NULL", []));
164+
Ok(())
165+
}),
140166
]
141167
}

0 commit comments

Comments
 (0)