Skip to content

Commit 1929eea

Browse files
committed
create crates-admin migrate to sync categories and migrate the db
The new command will be used in production to run migrations instead of installing the Diesel CLI and running migrations through it. In addition to that, synchronizing categories has been moved to the `crates-admin migrate` command instead of being executed every time the application starts. The main advantage of this is, booting the server will not *require* a fully working database connection anymore. A connection is still needed, but this removes all the blockers for removing that limit.
1 parent 7ec19cd commit 1929eea

File tree

8 files changed

+29
-11
lines changed

8 files changed

+29
-11
lines changed

.buildpacks

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
https://github.com/emk/heroku-buildpack-rust#cfa0f06
22
https://github.com/heroku/heroku-buildpack-nodejs#v176
33
https://github.com/heroku/heroku-buildpack-nginx#53b03b0
4-
https://github.com/sgrif/heroku-buildpack-diesel#f605edd

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ derive_deref = "1.1.1"
5252
dialoguer = "0.8"
5353
diesel = { version = "1.4.0", features = ["postgres", "serde_json", "chrono", "r2d2"] }
5454
diesel_full_text_search = "1.0.0"
55+
diesel_migrations = { version = "1.3.0", features = ["postgres"] }
5556
dotenv = "0.15"
5657
flate2 = "1.0"
5758
futures-channel = { version = "0.3.1", default-features = false }
@@ -88,7 +89,6 @@ url = "2.1"
8889
[dev-dependencies]
8990
claim = "0.5"
9091
conduit-test = "0.9.0-alpha.4"
91-
diesel_migrations = { version = "1.3.0", features = ["postgres"] }
9292
hyper-tls = "0.5"
9393
lazy_static = "1.0"
9494
tokio = "1"

Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
release: bin/diesel migration run
1+
release: ./target/release/crates-admin migrate
22
web: ./target/release/server
33
background_worker: ./target/release/background-worker

src/admin/migrate.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use anyhow::Error;
2+
3+
static CATEGORIES_TOML: &'static str = include_str!("../boot/categories.toml");
4+
diesel_migrations::embed_migrations!("./migrations");
5+
6+
#[derive(clap::Clap, Debug, Copy, Clone)]
7+
#[clap(name = "migrate", about = "Migrate the database.")]
8+
pub struct Opts;
9+
10+
pub fn run(_opts: Opts) -> Result<(), Error> {
11+
println!("==> migrating the database");
12+
let conn = crate::db::connect_now()?;
13+
embedded_migrations::run_with_output(&conn, &mut std::io::stdout())?;
14+
15+
println!("==> synchronizing crate categories");
16+
crate::boot::categories::sync(CATEGORIES_TOML).unwrap();
17+
18+
Ok(())
19+
}

src/admin/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod delete_crate;
22
pub mod delete_version;
33
pub mod dialoguer;
4+
pub mod migrate;
45
pub mod on_call;
56
pub mod populate;
67
pub mod render_readmes;

src/bin/crates-admin.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![warn(clippy::all, rust_2018_idioms)]
22

33
use cargo_registry::admin::{
4-
delete_crate, delete_version, populate, render_readmes, test_pagerduty, transfer_crates,
5-
verify_token,
4+
delete_crate, delete_version, migrate, populate, render_readmes, test_pagerduty,
5+
transfer_crates, verify_token,
66
};
77

88
use clap::Clap;
@@ -23,6 +23,7 @@ enum SubCommand {
2323
TestPagerduty(test_pagerduty::Opts),
2424
TransferCrates(transfer_crates::Opts),
2525
VerifyToken(verify_token::Opts),
26+
Migrate(migrate::Opts),
2627
}
2728

2829
fn main() {
@@ -36,5 +37,6 @@ fn main() {
3637
SubCommand::TestPagerduty(opts) => test_pagerduty::run(opts).unwrap(),
3738
SubCommand::TransferCrates(opts) => transfer_crates::run(opts),
3839
SubCommand::VerifyToken(opts) => verify_token::run(opts).unwrap(),
40+
SubCommand::Migrate(opts) => migrate::run(opts).unwrap(),
3941
}
4042
}

src/bin/server.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![warn(clippy::all, rust_2018_idioms)]
22
#![allow(unknown_lints)]
33

4-
use cargo_registry::{boot, App, Env};
4+
use cargo_registry::{App, Env};
55
use std::{borrow::Cow, fs::File, process::Command, sync::Arc, time::Duration};
66

77
use conduit_hyper::Service;
@@ -43,11 +43,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4343

4444
let handler = cargo_registry::build_handler(app.clone());
4545

46-
// On every server restart, ensure the categories available in the database match
47-
// the information in *src/categories.toml*.
48-
let categories_toml = include_str!("../boot/categories.toml");
49-
boot::categories::sync(categories_toml).unwrap();
50-
5146
let heroku = dotenv::var("HEROKU").is_ok();
5247
let fastboot = dotenv::var("USE_FASTBOOT").is_ok();
5348
let dev_docker = dotenv::var("DEV_DOCKER").is_ok();

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ extern crate derive_deref;
1515
#[macro_use]
1616
extern crate diesel;
1717
#[macro_use]
18+
extern crate diesel_migrations;
19+
#[macro_use]
1820
extern crate serde;
1921
#[macro_use]
2022
extern crate serde_json;

0 commit comments

Comments
 (0)