mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 13:37:30 +00:00
> Closes #33581 <!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves # # 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. - [x] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually ## Database migrations - [x] Checked table schema to confirm autoupdate - [x] Checked schema for all modified table for columns that will auto-update timestamps during migration. - [x] Ensured the correct collation is explicitly set for character columns (`COLLATE utf8mb4_unicode_ci`). --------- Co-authored-by: RachelElysia <rachel@fleetdm.com>
56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package tables
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
)
|
|
|
|
func init() {
|
|
MigrationClient.AddMigration(Up_20251003094629, Down_20251003094629)
|
|
}
|
|
|
|
func Up_20251003094629(tx *sql.Tx) error {
|
|
_, err := tx.Exec(`ALTER TABLE software_titles ADD COLUMN application_id VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL`)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to add software_titles.application_id column: %w", err)
|
|
}
|
|
|
|
_, err = tx.Exec(`ALTER TABLE software ADD COLUMN application_id VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL`)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to add software.application_id column: %w", err)
|
|
}
|
|
|
|
if _, err := tx.Exec(`
|
|
ALTER TABLE software_titles
|
|
DROP INDEX idx_unique_sw_titles
|
|
`); err != nil {
|
|
return fmt.Errorf("failed to drop unique index: %w", err)
|
|
}
|
|
|
|
// Drop the column to update the definition
|
|
_, err = tx.Exec(`ALTER TABLE software_titles DROP COLUMN unique_identifier`)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to drop software_titles.unique_identifier column: %w", err)
|
|
}
|
|
|
|
_, err = tx.Exec(`
|
|
ALTER TABLE software_titles
|
|
ADD COLUMN unique_identifier VARCHAR(255) GENERATED ALWAYS AS (COALESCE(bundle_identifier, application_id, name)) VIRTUAL;
|
|
`)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to add generated column unique_identifier: %w", err)
|
|
}
|
|
|
|
if _, err := tx.Exec(`
|
|
ALTER TABLE software_titles
|
|
ADD UNIQUE INDEX idx_unique_sw_titles (unique_identifier, source, extension_for);
|
|
`); err != nil {
|
|
return fmt.Errorf("failed to add unique index: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func Down_20251003094629(tx *sql.Tx) error {
|
|
return nil
|
|
}
|