-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
server/datastore/mysql/migrations/tables/20241216185214_InternalHostScriptResults.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package tables | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
) | ||
|
||
func init() { | ||
MigrationClient.AddMigration(Up_20241216185214, Down_20241216185214) | ||
} | ||
|
||
func Up_20241216185214(tx *sql.Tx) error { | ||
// alter the host_script_results table to add an is_internal flag (for scripts that still execute | ||
// when scripts are disabled globally) | ||
_, err := tx.Exec(`ALTER TABLE host_script_results ADD COLUMN is_internal BOOLEAN DEFAULT FALSE`) | ||
if err != nil { | ||
return fmt.Errorf("add is_internal to host_script_results: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Down_20241216185214(tx *sql.Tx) error { | ||
return nil | ||
} |
72 changes: 72 additions & 0 deletions
72
server/datastore/mysql/migrations/tables/20241216185214_InternalHostScriptResults_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package tables | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUp_20241216185214(t *testing.T) { | ||
db := applyUpToPrev(t) | ||
|
||
const ( | ||
insertScriptResultStmt = `INSERT INTO host_script_results ( | ||
host_id, execution_id, script_content_id, script_id, output | ||
) VALUES (?, ?, ?, ?, '')` | ||
|
||
insertScriptStmt = `INSERT INTO scripts ( | ||
team_id, global_or_team_id, name, script_content_id | ||
) VALUES (?, ?, ?, ?)` | ||
|
||
loadResultStmt = `SELECT | ||
id, host_id, execution_id, script_id, is_internal | ||
FROM host_script_results WHERE id = ?` | ||
) | ||
|
||
type script struct { | ||
id, globalOrTeamID int64 | ||
name, scriptContents string | ||
teamID *int64 | ||
} | ||
type scriptResult struct { | ||
id, hostID int64 | ||
executionID string | ||
scriptID *int64 | ||
isInternal bool | ||
} | ||
|
||
// create a global script | ||
globalScript := script{ | ||
globalOrTeamID: 0, | ||
teamID: nil, | ||
name: "global-script", | ||
scriptContents: "b", | ||
} | ||
|
||
scriptContentsID := execNoErrLastID(t, db, "INSERT INTO script_contents(contents, md5_checksum) VALUES (?, 'a')", globalScript.scriptContents) | ||
|
||
res, err := db.Exec(insertScriptStmt, globalScript.teamID, globalScript.globalOrTeamID, globalScript.name, scriptContentsID) | ||
require.NoError(t, err) | ||
globalScript.id, _ = res.LastInsertId() | ||
|
||
// create a host script result for that global script | ||
globalScriptResult := scriptResult{ | ||
hostID: 123, | ||
executionID: uuid.New().String(), | ||
scriptID: &globalScript.id, | ||
} | ||
res, err = db.Exec(insertScriptResultStmt, globalScriptResult.hostID, globalScriptResult.executionID, scriptContentsID, globalScriptResult.scriptID) | ||
require.NoError(t, err) | ||
globalScriptResult.id, _ = res.LastInsertId() | ||
|
||
// Apply current migration. | ||
applyNext(t, db) | ||
|
||
// the global host script result should be set as internal | ||
var result scriptResult | ||
err = db.QueryRow(loadResultStmt, globalScriptResult.id).Scan(&result.id, &result.hostID, &result.executionID, &result.scriptID, &result.isInternal) | ||
require.NoError(t, err) | ||
require.False(t, result.isInternal) | ||
require.Equal(t, globalScriptResult, result) | ||
} |