fleet/server/datastore/mysql/migrations/tables/20220704101843_AddSoftwareIDInSoftwareCVE.go
Scott Gress 59f96651b6
Update to Go 1.24.1 (#27506)
For #26713 

# Details

This PR updates Fleet and its related tools and binaries to use Go
version 1.24.1.

Scanning through the changelog, I didn't see anything relevant to Fleet
that requires action. The only possible breaking change I spotted was:

> As [announced](https://tip.golang.org/doc/go1.23#linux) in the Go 1.23
release notes, Go 1.24 requires Linux kernel version 3.2 or later.

Linux kernel 3.2 was released in January of 2012, so I think we can
commit to dropping support for earlier kernel versions.

The new [tools directive](https://tip.golang.org/doc/go1.24#tools) is
interesting as it means we can move away from using `tools.go` files,
but it's not a required update.

# Checklist for submitter

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

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [X] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
- [x] Manual QA for all new/changed functionality
- For Orbit and Fleet Desktop changes:
- [X] Make sure fleetd is compatible with the latest released version of
Fleet
   - [x] Orbit runs on macOS  , Linux   and Windows. 
- [x] Manual QA must be performed in the three main OSs, macOS ,
Windows and Linux .
2025-03-31 11:14:09 -05:00

79 lines
1.7 KiB
Go

package tables
import (
"database/sql"
"fmt"
"github.com/pkg/errors"
)
func init() {
MigrationClient.AddMigration(Up_20220704101843, Down_20220704101843)
}
func Up_20220704101843(tx *sql.Tx) error {
if !columnExists(tx, "software_cve", "software_id") {
_, err := tx.Exec(`
ALTER TABLE software_cve ADD COLUMN software_id bigint(20) UNSIGNED NULL, ALGORITHM=INPLACE, LOCK=NONE;
`)
if err != nil {
return errors.Wrapf(err, "adding software_id to software_cve")
}
}
var minVal int
var maxVal int
const selectStmt = `
SELECT COALESCE(MIN(cve.id), 0) AS min_id, COALESCE(MAX(cve.id), 0) as max_id
FROM software_cve AS cve
WHERE cve.software_id IS NULL;`
if err := tx.QueryRow(selectStmt).Scan(&minVal, &maxVal); err != nil {
return errors.Wrap(err, "selecting min,max id")
}
// Update in batches
const batchSize = 500
const updateStmt = `
UPDATE software_cve AS cve
INNER JOIN software_cpe AS cpe ON cve.cpe_id = cpe.id
SET cve.software_id = cpe.software_id
WHERE cve.software_id IS NULL AND cve.id >= ? AND cve.id < ?;`
if minVal != 0 || maxVal != 0 {
fmt.Printf("Updating aprox %d records... \n", maxVal-minVal)
}
start := minVal
for {
end := start + batchSize
if end >= maxVal {
end = maxVal + 1
}
_, err := tx.Exec(updateStmt, start, end)
if err != nil {
return errors.Wrapf(err, "updating software_cve")
}
start += batchSize
if start >= maxVal {
break
}
}
const indexStmt = `
ALTER TABLE software_cve ADD INDEX software_cve_software_id (software_id), ALGORITHM=INPLACE, LOCK=NONE;`
_, err := tx.Exec(indexStmt)
if err != nil {
return errors.Wrapf(err, "adding index to software_id on software_cve table")
}
return nil
}
func Down_20220704101843(tx *sql.Tx) error {
return nil
}