Skip to content

Commit ce94b10

Browse files
committed
dashboard/app: add spanner Jobs.Correct field
Lay down foundation for spanner DB migrations by adding Jobs.Correct field. This will allow us to test deployment of such changes. The field will be used to record results of manual assessment of AI job results.
1 parent 787e7cd commit ce94b10

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

dashboard/app/ai_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ func TestAIMigrations(t *testing.T) {
1919
c := NewSpannerCtx(t)
2020
defer c.Close()
2121

22-
up, err := loadDDLStatements("1_initialize.up.sql")
22+
up, err := loadUpDDLStatements()
2323
require.NoError(t, err)
24-
down, err := loadDDLStatements("1_initialize.down.sql")
24+
down, err := loadDownDDLStatements()
2525
require.NoError(t, err)
2626

2727
require.NoError(t, executeSpannerDDL(c.ctx, down))

dashboard/app/aidb/entities.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Job struct {
3333
Error string // for finished jobs
3434
Args spanner.NullJSON
3535
Results spanner.NullJSON
36+
Correct spanner.NullBool
3637
}
3738

3839
type TrajectorySpan struct {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE Jobs DROP COLUMN Correct;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE Jobs ADD COLUMN Correct BOOL;

dashboard/app/util_test.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"path/filepath"
2121
"reflect"
2222
"runtime"
23+
"slices"
2324
"strings"
2425
"sync"
2526
"sync/atomic"
@@ -107,7 +108,7 @@ func newCtx(t *testing.T, appID string) *Ctx {
107108
var appIDSeq = uint32(0)
108109

109110
func NewSpannerCtx(t *testing.T) *Ctx {
110-
ddlStatements, err := loadDDLStatements("1_initialize.up.sql")
111+
ddlStatements, err := loadUpDDLStatements()
111112
if err != nil {
112113
t.Fatal(err)
113114
}
@@ -140,14 +141,38 @@ func executeSpannerDDL(ctx context.Context, statements []string) error {
140141
return nil
141142
}
142143

143-
func loadDDLStatements(file string) ([]string, error) {
144-
data, err := os.ReadFile(filepath.Join("aidb", "migrations", file))
144+
func loadUpDDLStatements() ([]string, error) {
145+
return loadDDLStatements("*.up.sql", 1)
146+
}
147+
148+
func loadDownDDLStatements() ([]string, error) {
149+
return loadDDLStatements("*.down.sql", -1)
150+
}
151+
152+
func loadDDLStatements(wildcard string, sortOrder int) ([]string, error) {
153+
files, err := filepath.Glob(filepath.Join("aidb", "migrations", wildcard))
145154
if err != nil {
146155
return nil, err
147156
}
148-
// We need individual statements. Assume semicolon is not used in other places than statements end.
149-
statements := strings.Split(string(data), ";")
150-
return statements[:len(statements)-1], nil
157+
if len(files) == 0 {
158+
return nil, fmt.Errorf("loadDDLStatements: wildcard did not match any files: %q", wildcard)
159+
}
160+
// We prefix DDL file names with sequence numbers.
161+
slices.SortFunc(files, func(a, b string) int {
162+
return strings.Compare(a, b) * sortOrder
163+
})
164+
var all []string
165+
for _, file := range files {
166+
data, err := os.ReadFile(file)
167+
if err != nil {
168+
return nil, err
169+
}
170+
// We need individual statements. Assume semicolon is not used in other places than statements end.
171+
statements := strings.Split(string(data), ";")
172+
statements = statements[:len(statements)-1]
173+
all = append(all, statements...)
174+
}
175+
return all, nil
151176
}
152177

153178
func (c *Ctx) config() *GlobalConfig {

0 commit comments

Comments
 (0)