import { BaseCommand, flags } from '@adonisjs/core/ace' import { CommandOptions } from '@adonisjs/core/types/ace' import IndexMaintenance from '#services/IndexMaintenance' /** Rebuilds bloated indexes online (gc:scheduler also does this weekly). */ export default class DbReindex extends BaseCommand { public static commandName = 'db:reindex' public static description = 'Rebuilds B-tree indexes that are mostly empty space (online, REINDEX CONCURRENTLY).' public static options: CommandOptions = { startApp: true, } @flags.boolean({ description: 'Only show which indexes would be rebuilt' }) declare dryRun: boolean public async run() { const rebuilt = await IndexMaintenance.rebuildBloated({ dryRun: this.dryRun }) const saved = rebuilt.reduce((sum, r) => sum + r.before - r.after, 0) this.logger.info(`${rebuilt.length} indexes ${this.dryRun ? 'would be rebuilt' : `rebuilt, ${Math.round(saved / 1024 / 1024)} MB freed`}.`) } }