fleet/server/datastore/mysql/migrations/tables/20220704101843_AddSoftwareIDInSoftwareCVE.go
Lucas Manuel Rodriguez 28744bf57e
Consistently log migrations (#8154)
* Consistently log migrations

* Fix name and update goose version
2022-10-11 15:20:12 -03: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 min int
var max 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(&min, &max); 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 min != 0 || max != 0 {
fmt.Printf("Updating aprox %d records... \n", max-min)
}
start := min
for {
end := start + batchSize
if end >= max {
end = max + 1
}
_, err := tx.Exec(updateStmt, start, end)
if err != nil {
return errors.Wrapf(err, "updating software_cve")
}
start += batchSize
if start >= max {
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
}