Skip to content

Commit e8ce5da

Browse files
committed
Auto merge of #3562 - jtgeibel:fail-migrations-in-read-only, r=pietroalbini
Ensure the release succeeds when the primary database is read-only If the primary is in read-only mode then the leader database in `DATABASE_URL` may be unavailable. To avoid blocking configuration changes, the command exits immediately claiming success. r? `@pietroalbini`
2 parents d53724d + 4d269b5 commit e8ce5da

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/admin/migrate.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,33 @@ static CATEGORIES_TOML: &'static str = include_str!("../boot/categories.toml");
44
diesel_migrations::embed_migrations!("./migrations");
55

66
#[derive(clap::Clap, Debug, Copy, Clone)]
7-
#[clap(name = "migrate", about = "Migrate the database.")]
7+
#[clap(
8+
name = "migrate",
9+
about = "Verify config, migrate the database, and other release tasks."
10+
)]
811
pub struct Opts;
912

1013
pub fn run(_opts: Opts) -> Result<(), Error> {
14+
let config = crate::Config::default();
15+
16+
// TODO: Refactor logic so that we can also check things from App::new() here.
17+
// If the app will panic due to bad configuration, it is better to error in the release phase
18+
// to avoid launching dynos that will fail.
19+
20+
if config.db_primary_config.read_only_mode {
21+
// TODO: Check `any_pending_migrations()` with a read-only connection and error if true.
22+
// It looks like this requires changes upstream to make this pub in `migration_macros`.
23+
24+
println!("==> Skipping migrations and category sync (read-only mode)");
25+
26+
// The service is undergoing maintenance or mitigating an outage.
27+
// Exit with success to ensure configuration changes can be made.
28+
// Heroku will not launch new dynos if the release phase fails.
29+
return Ok(());
30+
}
31+
1132
println!("==> migrating the database");
33+
// The primary is online, access directly via `DATABASE_URL`.
1234
let conn = crate::db::connect_now()?;
1335
embedded_migrations::run_with_output(&conn, &mut std::io::stdout())?;
1436

src/bin/crates-admin.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ enum SubCommand {
2626
Migrate(migrate::Opts),
2727
}
2828

29-
fn main() {
29+
fn main() -> anyhow::Result<()> {
3030
let opts: Opts = Opts::parse();
3131

32-
match opts.command {
32+
Ok(match opts.command {
3333
SubCommand::DeleteCrate(opts) => delete_crate::run(opts),
3434
SubCommand::DeleteVersion(opts) => delete_version::run(opts),
3535
SubCommand::Populate(opts) => populate::run(opts),
3636
SubCommand::RenderReadmes(opts) => render_readmes::run(opts),
37-
SubCommand::TestPagerduty(opts) => test_pagerduty::run(opts).unwrap(),
37+
SubCommand::TestPagerduty(opts) => test_pagerduty::run(opts)?,
3838
SubCommand::TransferCrates(opts) => transfer_crates::run(opts),
3939
SubCommand::VerifyToken(opts) => verify_token::run(opts).unwrap(),
40-
SubCommand::Migrate(opts) => migrate::run(opts).unwrap(),
41-
}
40+
SubCommand::Migrate(opts) => migrate::run(opts)?,
41+
})
4242
}

0 commit comments

Comments
 (0)