fix: address audit execution failures and unsafe type conversions
This commit is contained in:
@@ -2,6 +2,7 @@ package sqlexec
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -121,7 +122,7 @@ func TestLive_ExecuteScriptsOrder(t *testing.T) {
|
||||
if strings.Join(got, ",") != strings.Join(want, ",") {
|
||||
t.Errorf("execution order = %v, want %v", got, want)
|
||||
}
|
||||
if opts.Metadata["execution_total"] != 6 || opts.Metadata["execution_success"] != 6 || opts.Metadata["execution_failed"] != 0 {
|
||||
if opts.Metadata["execution_total"] != 7 || opts.Metadata["execution_success"] != 6 || opts.Metadata["execution_failed"] != 0 {
|
||||
t.Errorf("counts: %v", opts.Metadata)
|
||||
}
|
||||
}
|
||||
@@ -160,8 +161,8 @@ func TestLive_IgnoreErrorsContinues(t *testing.T) {
|
||||
{Name: "02_ok", Priority: 2, SQL: fmt.Sprintf("CREATE TABLE %s.after(id int);", schema)},
|
||||
}
|
||||
opts := liveOptions(connString, map[string]interface{}{"ignore_errors": true})
|
||||
if err := NewWriter(opts).WriteSchema(&models.Schema{Name: schema, Scripts: scripts}); err != nil {
|
||||
t.Fatalf("ignore_errors must not fail: %v", err)
|
||||
if err := NewWriter(opts).WriteSchema(&models.Schema{Name: schema, Scripts: scripts}); err == nil {
|
||||
t.Fatal("continuation must still return failure")
|
||||
}
|
||||
if opts.Metadata["execution_total"] != 2 || opts.Metadata["execution_success"] != 1 || opts.Metadata["execution_failed"] != 1 {
|
||||
t.Errorf("counts: %v", opts.Metadata)
|
||||
@@ -186,8 +187,8 @@ func TestLive_EmbedDirectiveErrorHandling(t *testing.T) {
|
||||
}
|
||||
|
||||
opts := liveOptions(connString, map[string]interface{}{"ignore_errors": true})
|
||||
if err := NewWriter(opts).WriteSchema(&models.Schema{Name: schema, Scripts: []*models.Script{bad}}); err != nil {
|
||||
t.Errorf("ignore_errors: %v", err)
|
||||
if err := NewWriter(opts).WriteSchema(&models.Schema{Name: schema, Scripts: []*models.Script{bad}}); err == nil {
|
||||
t.Error("embed preparation failures must propagate even when continuing")
|
||||
}
|
||||
if opts.Metadata["execution_failed"] != 1 {
|
||||
t.Errorf("counts: %v", opts.Metadata)
|
||||
@@ -217,7 +218,29 @@ func TestLive_WriteDatabaseMultiSchema(t *testing.T) {
|
||||
// A failure in one schema aborts and names that schema.
|
||||
db.Schemas[1].Scripts[0].SQL = "SELECT * FROM definitely_missing_table;"
|
||||
err := NewWriter(liveOptions(connString, nil)).WriteDatabase(db)
|
||||
if err == nil || !strings.Contains(err.Error(), "schema "+s2) {
|
||||
if err == nil || !strings.Contains(err.Error(), s2+".b") {
|
||||
t.Errorf("expected error naming schema %s, got %v", s2, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLive_MultiSchemaFailuresAggregate(t *testing.T) {
|
||||
connection := liveConn(t)
|
||||
opts := liveOptions(connection, nil)
|
||||
opts.ContinueOnError = true
|
||||
db := &models.Database{Schemas: []*models.Schema{
|
||||
{Name: "one", Scripts: []*models.Script{{Name: "bad", SQL: "SELECT 1 / 0"}, {Name: "empty"}}},
|
||||
{Name: "two", Scripts: []*models.Script{{Name: "ok", SQL: "SELECT 1"}}},
|
||||
}}
|
||||
err := NewWriter(opts).WriteDatabase(db)
|
||||
var failure *writers.ExecutionFailure
|
||||
if !errors.As(err, &failure) {
|
||||
t.Fatalf("expected structured failure: %v", err)
|
||||
}
|
||||
report := failure.Report
|
||||
if report.TotalStatements != 3 || report.FailedStatements != 1 || report.ExecutedStatements != 1 || report.SkippedStatements != 1 || report.Outcome != "script_managed" {
|
||||
t.Fatalf("incorrect aggregate: %+v", report)
|
||||
}
|
||||
if report.Operations[0].Name != "one.bad" || report.Operations[2].Name != "two.ok" || report.Operations[2].Status != "executed" {
|
||||
t.Fatalf("script details: %+v", report.Operations)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user