diff --git a/src/commands/admin/update.ts b/src/commands/admin/update.ts index d33412b..779334c 100644 --- a/src/commands/admin/update.ts +++ b/src/commands/admin/update.ts @@ -70,44 +70,34 @@ export const update = createCommand({ if (confirmation.customId === "confirm_update") { await confirmation.update({ - embeds: [createWarningEmbed("Applying updates and restarting...", "Update In Progress")], + embeds: [createWarningEmbed("Applying updates and restarting...\nThe bot will run database migrations on next startup.", "Update In Progress")], components: [] }); - const { stdout } = await execAsync(`git reset --hard origin/${branch}`); - - // Run DB Migrations - await confirmation.editReply({ - embeds: [createWarningEmbed("Updating database schema...", "Running Migrations")], - components: [] - }); - - let migrationOutput = ""; - try { - const { stdout: dbOut } = await execAsync("bun run db:push:local"); - migrationOutput = dbOut; - } catch (dbErr: any) { - migrationOutput = `Migration Failed: ${dbErr.message}`; - console.error("Migration Error:", dbErr); - // We continue with restart even if migration fails? - // Probably safer to try, or should we abort? - // For now, let's log it and proceed, as code might depend on it but maybe it was a no-op partial fail. - } - - await interaction.followUp({ - flags: MessageFlags.Ephemeral, - embeds: [createSuccessEmbed(`Git Reset Output:\n\`\`\`\n${stdout}\n\`\`\`\nDB Migration Output:\n\`\`\`\n${migrationOutput}\n\`\`\`\nRestarting process...`, "Update Successful")] - }); - - // Write context for post-restart notification + // Write context BEFORE reset, because reset -> watcher restart await writeFile(".restart_context.json", JSON.stringify({ channelId: interaction.channelId, userId: interaction.user.id, - timestamp: Date.now() + timestamp: Date.now(), + runMigrations: true })); - // Trigger restart - await appendFile("src/index.ts", " "); + const { stdout } = await execAsync(`git reset --hard origin/${branch}`); + + // In case we are not running with a watcher, or if no files were changed (unlikely given log check), + // we might need to manually trigger restart. + // But if files changed, watcher kicks in here or slightly after. + // If we are here, we can try to force a touch or just exit. + + // Trigger restart just in case watcher didn't catch it or we are in a mode without watcher (though update implies source change) + try { + await appendFile("src/index.ts", " "); + } catch (err) { + console.error("Failed to touch triggers:", err); + } + + // The process should die now or soon. + // We do NOT run migrations here anymore. } else { await confirmation.update({ diff --git a/src/events/ready.ts b/src/events/ready.ts index 82fa04d..87c647c 100644 --- a/src/events/ready.ts +++ b/src/events/ready.ts @@ -20,10 +20,37 @@ const event: Event = { // Validate context freshness (e.g., ignore if older than 5 minutes) if (Date.now() - context.timestamp < 5 * 60 * 1000) { const channel = await c.channels.fetch(context.channelId); + if (channel && channel.isSendable()) { - await channel.send({ - embeds: [createSuccessEmbed("Bot is back online! Redeploy successful.", "System Online")] - }); + let migrationOutput = ""; + let success = true; + + if (context.runMigrations) { + try { + const { exec } = await import("child_process"); + const { promisify } = await import("util"); + const execAsync = promisify(exec); + + // Send intermediate update if possible, though ready event should be fast. + // Maybe just run it and report result. + const { stdout: dbOut } = await execAsync("bun run db:push:local"); + migrationOutput = dbOut; + } catch (dbErr: any) { + success = false; + migrationOutput = `Migration Failed: ${dbErr.message}`; + console.error("Migration Error:", dbErr); + } + } + + if (context.runMigrations) { + await channel.send({ + embeds: [createSuccessEmbed(`Bot is back online!\n\n**DB Migration Output:**\n\`\`\`\n${migrationOutput}\n\`\`\``, success ? "Update Successful" : "Update Completed with/Errors")] + }); + } else { + await channel.send({ + embeds: [createSuccessEmbed("Bot is back online! Redeploy successful.", "System Online")] + }); + } } }