fix(bootstrap): Run migrations with direct password before Vault is set up

The bootstrap order is vault -> tables -> directory. When tables
component was processed, it tried to call establish_pg_connection()
which requires Vault, but Vault setup only happens when processing
the vault component (which is earlier in the loop but the if-block
for tables runs for the tables component, not vault).

Fix: Run migrations directly with the generated password during the
tables component setup, before Vault is configured. This avoids the
dependency on Vault being set up.
This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-12-09 07:49:01 -03:00
parent 49d1d0cf2e
commit b204aebd50

View file

@ -5,7 +5,7 @@ use crate::shared::utils::{establish_pg_connection, init_secrets_manager};
use anyhow::Result;
use aws_config::BehaviorVersion;
use aws_sdk_s3::Client;
use diesel::RunQueryDsl;
use diesel::{Connection, RunQueryDsl};
use log::debug;
use log::{error, info, trace, warn};
use rand::distr::Alphanumeric;
@ -574,6 +574,23 @@ impl BootstrapManager {
}
}
// Run migrations using direct connection (Vault not set up yet)
info!("🔄 Running database migrations...");
let database_url =
format!("postgres://gbuser:{}@localhost:5432/botserver", db_password);
match diesel::PgConnection::establish(&database_url) {
Ok(mut conn) => {
if let Err(e) = self.apply_migrations(&mut conn) {
error!("Failed to apply migrations: {}", e);
} else {
info!("✓ Database migrations applied");
}
}
Err(e) => {
error!("Failed to connect to database for migrations: {}", e);
}
}
info!("🔧 Creating Directory configuration files...");
if let Err(e) = self.configure_services_in_directory(&db_password).await {
error!("Failed to create Directory config files: {}", e);
@ -636,11 +653,6 @@ impl BootstrapManager {
}
}
if component == "tables" {
let mut conn = establish_pg_connection().unwrap();
self.apply_migrations(&mut conn)?;
}
if component == "email" {
info!("🔧 Auto-configuring Email (Stalwart)...");
if let Err(e) = self.setup_email().await {