Add macOS 14+ built-in label

This commit is contained in:
Martin Angers 2024-04-03 11:13:18 -04:00
parent c28bd8fc3a
commit d2fd3694b8
2 changed files with 76 additions and 0 deletions

View file

@ -0,0 +1,56 @@
package tables
import (
"database/sql"
"fmt"
"github.com/VividCortex/mysqlerr"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/go-sql-driver/mysql"
)
func init() {
MigrationClient.AddMigration(Up_20240403104633, Down_20240403104633)
}
func Up_20240403104633(tx *sql.Tx) error {
const stmt = `
INSERT INTO labels (
name,
description,
query,
platform,
label_type,
label_membership_type
) VALUES (?, ?, ?, ?, ?)
`
const labelName = "macOS 14+ (Sonoma+)"
_, err := tx.Exec(
stmt,
labelName,
"macOS hosts with version 14 and above",
`select 1 from os_version where platform = 'darwin' and major >= 14;`,
"darwin",
fleet.LabelTypeBuiltIn,
fleet.LabelMembershipTypeDynamic,
)
if err != nil {
if driverErr, ok := err.(*mysql.MySQLError); ok {
if driverErr.Number == mysqlerr.ER_DUP_ENTRY {
// TODO(mna): how do we feel about this approach to ensure the new
// Fleet-reserved name is unique? All label names need to be unique
// across built-in and regular. (I don't think we've done anything
// special before, but this seems a bit nicer/clearer as to why the
// migration may have failed and how to fix it)
return fmt.Errorf("a label with the name %q already exists, please rename it before applying this migration: %w", labelName, err)
}
}
return err
}
return nil
}
func Down_20240403104633(tx *sql.Tx) error {
return nil
}

View file

@ -0,0 +1,20 @@
package tables
import "testing"
func TestUp_20240403104633(t *testing.T) {
db := applyUpToPrev(t)
//
// Insert data to test the migration
//
// ...
// Apply current migration.
applyNext(t, db)
//
// Check data, insert new entries, e.g. to verify migration is safe.
//
// ...
}