fix: Auto-create bot database when not configured

Modified get_bot_pool() to automatically create the database for a bot
if it doesn't exist, instead of failing with "No database configured" error.

This fixes the issue where bots created after the initial sync don't have
a database_name set in the bots table, causing table creation to fail.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Rodrigo Rodriguez 2026-02-15 14:57:22 +00:00
parent 0c9665dd8b
commit e9a428ab1c

View file

@ -105,9 +105,25 @@ impl BotDatabaseManager {
}
}
// Get database name for this bot
let db_name = self.get_bot_database_name(bot_id)?
.ok_or_else(|| format!("No database configured for bot {}", bot_id))?;
// Get bot info (including name) from database
let mut conn = self.main_pool.get()?;
let bot_info: Option<BotDatabaseInfo> = sql_query(
"SELECT id, name, database_name FROM bots WHERE id = $1 AND is_active = true",
)
.bind::<diesel::sql_types::Uuid, _>(bot_id)
.get_result(&mut conn)
.optional()?;
let bot_info = bot_info.ok_or_else(|| format!("Bot {} not found or not active", bot_id))?;
// Ensure bot has a database, create if needed
let db_name = if let Some(name) = bot_info.database_name {
name
} else {
// Bot doesn't have a database configured, create it now
info!("Bot {} ({}) has no database, creating now", bot_info.name, bot_id);
self.ensure_bot_has_database(bot_id, &bot_info.name)?
};
// Create new pool
let pool = self.create_pool_for_database(&db_name)?;