fleet/server/datastore/mysql/migrations/tables/20250827113140_AddIndexDistributedQueryCampaignTargets.go
Victor Lyuboslavsky b3216a1727
Add CleanupCompletedCampaignTargets to cleanup old campaign targets. (#32385)
Fixes #31432 

- Added campaign target cleanup: Deletes targets from campaigns
completed >24h ago. Uses 10% or 50k min per run, processes in 10k
batches. Added DB index, integrated into hourly cron, includes tests.

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

## Testing

- [x] Added/updated automated tests
- [x] Where appropriate, [automated tests simulate multiple hosts and
test for host
isolation](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/reference/patterns-backend.md#unit-testing)
(updates to one hosts's records do not affect another)

- [x] QA'd all new/changed functionality manually


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- New Features
- Automatic cleanup of live query campaign targets 24 hours after
campaign completion to reduce clutter and storage usage.

- Chores
- Added a database index to speed up live query target operations for
improved performance at scale.
- Enhanced scheduled maintenance to log cleanup counts and execution
time for better observability.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-08-28 11:04:05 -05:00

23 lines
570 B
Go

package tables
import (
"database/sql"
)
func init() {
MigrationClient.AddMigration(Up_20250827113140, Down_20250827113140)
}
func Up_20250827113140(tx *sql.Tx) error {
// Add index on distributed_query_campaign_id column to improve query performance
// This is the most common WHERE clause used when querying this table
_, err := tx.Exec(`
ALTER TABLE distributed_query_campaign_targets
ADD INDEX idx_distributed_query_campaign_targets_campaign_id (distributed_query_campaign_id)
`)
return err
}
func Down_20250827113140(_ *sql.Tx) error {
return nil
}