diff --git a/internal/repository/mysql/repository.go b/internal/repository/mysql/repository.go index 7955f3b..aacbf3a 100644 --- a/internal/repository/mysql/repository.go +++ b/internal/repository/mysql/repository.go @@ -7,7 +7,7 @@ import ( "fmt" "time" - _ "github.com/go-sql-driver/mysql" + mysql "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" "github.com/shopspring/decimal" @@ -699,9 +699,17 @@ func (r *Repository) insertSystemStateHistory(ctx context.Context, state domain. _, err := r.execer().ExecContext(ctx, ` INSERT INTO system_state_history (ts, state, mode, halted, halt_reason, context_json) VALUES (UTC_TIMESTAMP(3), ?, ?, ?, ?, ?)`, state, mode, halted, nullableString(reason), contextJSON) + if isMissingTableError(err) { + return nil + } return err } +func isMissingTableError(err error) bool { + var mysqlErr *mysql.MySQLError + return errors.As(err, &mysqlErr) && mysqlErr.Number == 1146 +} + func (r *Repository) Unhalt(ctx context.Context, reason string) error { return r.RunInTx(ctx, func(ctx context.Context, repo repository.Repository) error { state, halted, haltReason, err := repo.GetSystemState(ctx) diff --git a/internal/repository/mysql/rows_test.go b/internal/repository/mysql/rows_test.go index 7106ec4..630c3eb 100644 --- a/internal/repository/mysql/rows_test.go +++ b/internal/repository/mysql/rows_test.go @@ -1,9 +1,12 @@ package mysql import ( + "errors" + "fmt" "testing" "time" + mysql "github.com/go-sql-driver/mysql" "github.com/shopspring/decimal" "overnight-trading-bot/internal/domain" @@ -25,3 +28,15 @@ func TestMinuteCandleRowPreservesTimestamp(t *testing.T) { t.Fatalf("daily timestamp was not truncated to date: %s", daily.TradeDate) } } + +func TestIsMissingTableError(t *testing.T) { + if !isMissingTableError(&mysql.MySQLError{Number: 1146}) { + t.Fatal("expected MySQL 1146 to be treated as missing table") + } + if !isMissingTableError(fmt.Errorf("wrap: %w", &mysql.MySQLError{Number: 1146})) { + t.Fatal("expected wrapped MySQL 1146 to be treated as missing table") + } + if isMissingTableError(errors.New("not mysql")) { + t.Fatal("plain errors must not be treated as missing table") + } +}