diff --git a/Cargo.lock b/Cargo.lock index 42c2f17eb9b..4b2d987838b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,7 +72,6 @@ name = "cargo-registry" version = "0.1.0" dependencies = [ "bufstream 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "cargo-registry-migrate 0.1.0", "cargo-registry-s3 0.1.0", "chrono 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "civet 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -114,13 +113,6 @@ dependencies = [ "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "cargo-registry-migrate" -version = "0.1.0" -dependencies = [ - "postgres 0.13.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "cargo_metadata" version = "0.1.2" @@ -699,19 +691,6 @@ name = "pkg-config" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "postgres" -version = "0.13.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "bufstream 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "fallible-iterator 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", - "postgres-protocol 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "postgres-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "postgres" version = "0.14.0" @@ -737,17 +716,6 @@ dependencies = [ "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "postgres-shared" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "fallible-iterator 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)", - "postgres-protocol 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "postgres-shared" version = "0.2.0" @@ -1152,10 +1120,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = "cb325642290f28ee14d8c6201159949a872f220c62af6e110a56ea914fbe42fc" "checksum phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = "07e24b0ca9643bdecd0632f2b3da6b1b89bbb0030e0b992afc1113b23a7bc2f2" "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" -"checksum postgres 0.13.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ed6564a52406dbc555719ba186f5309c790bc5fd44a656bb787d1c49fbc6ac" "checksum postgres 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aadd34ecc7aa81eae593f8299bb57e066277cda3d63713b2352245060713466d" "checksum postgres-protocol 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a8210267a051af88e19a35defb2a26ecd2f84a4f3cba66c28a849a8caa4471be" -"checksum postgres-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ba919eba78ade418bdee00d325caccb0946ab0ccfeaeb364c0fe157d2a81c382" "checksum postgres-shared 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "857ef343933a7d81e365a334f505a07db16ab7be64d9d7cd82c78d2f486777e4" "checksum pq-sys 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6f386bd842d8571f4df788f49e764bab85d30b3320b2ca98a2a24cfa8f65b903" "checksum quine-mc_cluskey 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "07589615d719a60c8dd8a4622e7946465dfef20d1a428f969e3443e7386d5f45" diff --git a/Cargo.toml b/Cargo.toml index 5dcb3e48603..6eb8fc7746c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ path = "src/tests/all.rs" [dependencies] cargo-registry-s3 = { path = "src/s3", version = "0.1.0" } -cargo-registry-migrate = { path = "src/migrate", version = "0.1.0" } rand = "0.3" time = "0.1" git2 = "0.6.4" diff --git a/Procfile b/Procfile index b4fa48bf1c3..0dbfa4cc6b0 100644 --- a/Procfile +++ b/Procfile @@ -1,2 +1,2 @@ -web: ./target/release/migrate && bin/diesel migration run && bin/start-nginx ./target/release/server +web: bin/diesel migration run && bin/start-nginx ./target/release/server worker: ./target/release/update-downloads daemon 300 diff --git a/src/bin/migrate.rs b/src/bin/migrate.rs deleted file mode 100644 index 2bfca78c783..00000000000 --- a/src/bin/migrate.rs +++ /dev/null @@ -1,46 +0,0 @@ -#![deny(warnings)] - -#[macro_use] -extern crate diesel; -extern crate dotenv; - -use diesel::*; -use diesel::migrations::setup_database; -use diesel::pg::PgConnection; -use dotenv::dotenv; -use std::env; - -table! { - information_schema.tables (table_name) { - table_name -> Text, - } -} - -fn main() { - let _ = dotenv(); - // This code will eventually break, as it relies on internal details of Diesel. - // If this function stops working, it's probably better to just delete it. - let conn = PgConnection::establish(&env::var("DATABASE_URL").unwrap()).unwrap(); - - if !table_exists("__diesel_schema_migrations", &conn) { - setup_database(&conn).unwrap(); - } - - if table_exists("schema_migrations", &conn) { - conn.execute("INSERT INTO __diesel_schema_migrations ( - SELECT version::text AS version, CURRENT_TIMESTAMP as run_on - FROM schema_migrations - ) ON CONFLICT DO NOTHING").unwrap(); - } - - println!("The `migrate` binary is no longer used. Use `diesel migration run` \ - and `diesel migration revert` instead."); -} - -fn table_exists(target: &str, conn: &PgConnection) -> bool { - use self::tables::dsl::*; - use diesel::expression::dsl::exists; - - let table_query = tables.filter(table_name.eq(target)); - select(exists(table_query)).get_result(conn).unwrap() -} diff --git a/src/migrate/Cargo.toml b/src/migrate/Cargo.toml deleted file mode 100644 index 55f4d3e2088..00000000000 --- a/src/migrate/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[project] -name = "cargo-registry-migrate" -authors = ["Alex Crichton "] -version = "0.1.0" -license = "MIT/Apache-2.0" -repository = "https://github.com/rust-lang/crates.io" - -[lib] -name = "migrate" -path = "lib.rs" - -[dependencies] -postgres = "0.13" diff --git a/src/migrate/lib.rs b/src/migrate/lib.rs deleted file mode 100644 index 63fbcc93f02..00000000000 --- a/src/migrate/lib.rs +++ /dev/null @@ -1,190 +0,0 @@ -#![deny(warnings)] - -extern crate postgres; - -use std::collections::HashSet; - -use postgres::transaction::Transaction; -use postgres::Result as PgResult; - -struct A<'a, 'b: 'a> { - t: &'a Transaction<'b>, -} - -type Step = Box PgResult<()> + 'static>; - -pub struct Migration { - version: i64, - up: Step, - down: Step, -} - -pub struct Manager<'a> { - tx: Transaction<'a>, - versions: HashSet, -} - -impl Migration { - fn mk(version: i64, up: Step, down: Step) -> Migration { - Migration { version: version, up: up, down: down } - } - - pub fn new(version: i64, mut up: F1, mut down: F2) -> Migration - where F1: FnMut(&Transaction) -> PgResult<()> + 'static, - F2: FnMut(&Transaction) -> PgResult<()> + 'static - { - Migration::mk(version, - Box::new(move |a| up(a.t)), - Box::new(move |a| down(a.t))) - } - - pub fn run(version: i64, up: &str, down: &str) -> Migration { - Migration::mk(version, - run(up.to_string()), - run(down.to_string())) - } - - pub fn add_table(version: i64, table: &str, rest: &str) -> Migration { - let add_sql = format!("CREATE TABLE {} ({})", table, rest); - let rm_sql = format!("DROP TABLE {}", table); - Migration::mk(version, run(add_sql), run(rm_sql)) - } - - pub fn add_column(version: i64, table: &str, column: &str, - type_and_constraints: &str) -> Migration { - let add_sql = format!("ALTER TABLE {} ADD COLUMN {} {}", - table, column, type_and_constraints); - let rm_sql = format!("ALTER TABLE {} DROP COLUMN {}", table, column); - Migration::mk(version, run(add_sql), run(rm_sql)) - } - - pub fn version(&self) -> i64 { self.version } -} - -fn run(sql: String) -> Step { - Box::new(move |a: A| { - let tx = a.t; - tx.execute(&sql, &[]).map(|_| ()).map_err(|e| { - println!("failed to run `{}`", sql); - e - }) - }) -} - -impl<'a> Manager<'a> { - pub fn new(tx: Transaction) -> PgResult { - let mut mgr = Manager { tx: tx, versions: HashSet::new() }; - mgr.load()?; - Ok(mgr) - } - - fn load(&mut self) -> PgResult<()> { - self.tx.execute("CREATE TABLE IF NOT EXISTS schema_migrations ( - id SERIAL PRIMARY KEY, - version INT8 NOT NULL UNIQUE - )", &[])?; - - let stmt = self.tx.prepare("SELECT version FROM \ - schema_migrations")?; - for row in stmt.query(&[])?.iter() { - assert!(self.versions.insert(row.get("version"))); - } - Ok(()) - } - - pub fn contains(&self, version: i64) -> bool { - self.versions.contains(&version) - } - - pub fn apply(&mut self, mut migration: Migration) -> PgResult<()> { - if !self.versions.insert(migration.version) { return Ok(()) } - println!("applying {}", migration.version); - (migration.up)(A { t: &self.tx })?; - let stmt = self.tx.prepare("INSERT into schema_migrations - (version) VALUES ($1)")?; - stmt.execute(&[&migration.version])?; - Ok(()) - } - - pub fn rollback(&mut self, mut migration: Migration) -> PgResult<()> { - if !self.versions.remove(&migration.version) { return Ok(()) } - println!("rollback {}", migration.version); - (migration.down)(A { t: &self.tx })?; - let stmt = self.tx.prepare("DELETE FROM schema_migrations - WHERE version = $1")?; - stmt.execute(&[&migration.version])?; - Ok(()) - } - - pub fn set_commit(&mut self) { self.tx.set_commit() } - - pub fn finish(self) -> PgResult<()> { self.tx.finish() } -} - -#[cfg(test)] -mod tests { - use std::os; - use postgres::{PostgresConnection, NoSsl}; - use super::{Manager, Migration}; - - fn conn() -> PostgresConnection { - let url = os::getenv("MIGRATE_TEST_DATABASE_URL").unwrap(); - PostgresConnection::connect(&url, &NoSsl).unwrap() - } - - #[test] - fn no_reapply() { - let c = conn(); - let c = c.transaction().unwrap(); - let mut called = false; - { - let mut mgr = Manager::new(c.transaction().unwrap()).unwrap(); - mgr.apply(Migration::new(1, |_| { - called = true; Ok(()) - }, |_| panic!())).unwrap(); - mgr.set_commit(); - } - assert!(called); - called = false; - { - let mut mgr = Manager::new(c.transaction().unwrap()).unwrap(); - mgr.apply(Migration::new(1, |_| { - called = true; Ok(()) - }, |_| panic!())).unwrap(); - mgr.set_commit(); - } - assert!(!called); - } - - #[test] - fn rollback_then_apply() { - let c = conn(); - let c = c.transaction().unwrap(); - let mut called = false; - { - let mut mgr = Manager::new(c.transaction().unwrap()).unwrap(); - mgr.rollback(Migration::new(1, |_| panic!(), |_| { - called = true; Ok(()) - })).unwrap(); - mgr.set_commit(); - } - assert!(!called); - { - let mut mgr = Manager::new(c.transaction().unwrap()).unwrap(); - mgr.apply(Migration::new(1, |_| { - called = true; Ok(()) - }, |_| panic!())).unwrap(); - mgr.set_commit(); - } - assert!(called); - called = false; - { - let mut mgr = Manager::new(c.transaction().unwrap()).unwrap(); - mgr.rollback(Migration::new(1, |_| panic!(), |_| { - called = true; Ok(()) - })).unwrap(); - mgr.set_commit(); - } - assert!(called); - } -}