fleet/server/datastore/mysql/migrations/tables/20241004005000_AddPolicyIdToScriptExecutions.go
Ian Littman 5339794f97
Include the policy ID and name in the "script ran" activity of a script run queued by a policy failure (#22690)
#22692 

# 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`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files)
for more information.
- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)
- [x] Added/updated tests
- [x] If database migrations are included, checked table schema to
confirm autoupdate
- For database migrations:
- [x] Checked schema for all modified table for columns that will
auto-update timestamps during migration.
- [x] Confirmed that updating the timestamps is acceptable, and will not
cause unwanted side effects.
- [x] Ensured the correct collation is explicitly set for character
columns (`COLLATE utf8mb4_unicode_ci`).
- [x] Manual QA for all new/changed functionality
2024-10-08 15:45:31 -05:00

26 lines
565 B
Go

package tables
import (
"database/sql"
"fmt"
)
func init() {
MigrationClient.AddMigration(Up_20241004005000, Down_20241004005000)
}
func Up_20241004005000(tx *sql.Tx) error {
if _, err := tx.Exec(`
ALTER TABLE host_script_results
ADD COLUMN policy_id INT UNSIGNED DEFAULT NULL,
ADD FOREIGN KEY fk_script_result_policy_id (policy_id) REFERENCES policies (id) ON DELETE SET NULL
`); err != nil {
return fmt.Errorf("failed to add policy_id to host script results: %w", err)
}
return nil
}
func Down_20241004005000(tx *sql.Tx) error {
return nil
}