@@ -0,0 +1,57 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFreshDatabaseHasCurrentMonitorSchemaAndIndexes(t *testing.T) {
|
||||
db, err := Open(filepath.Join(t.TempDir(), "dockwatch.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx := context.Background()
|
||||
|
||||
rows, err := db.QueryContext(ctx, `PRAGMA table_info(monitors)`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cols := map[string]bool{}
|
||||
for rows.Next() {
|
||||
var cid, notnull, pk int
|
||||
var name, typ string
|
||||
var dflt any
|
||||
if err := rows.Scan(&cid, &name, &typ, ¬null, &dflt, &pk); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cols[name] = true
|
||||
}
|
||||
_ = rows.Close()
|
||||
for _, name := range []string{"service_id", "method", "headers_json", "maintenance_until", "require_healthy"} {
|
||||
if !cols[name] {
|
||||
t.Fatalf("fresh monitors schema is missing %s", name)
|
||||
}
|
||||
}
|
||||
|
||||
idx, err := db.QueryContext(ctx, `PRAGMA index_list(monitors)`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
indexes := map[string]bool{}
|
||||
for idx.Next() {
|
||||
var seq, unique, partial int
|
||||
var name, origin string
|
||||
if err := idx.Scan(&seq, &name, &unique, &origin, &partial); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
indexes[name] = true
|
||||
}
|
||||
_ = idx.Close()
|
||||
for _, name := range []string{"idx_monitors_service", "idx_monitors_schedule", "idx_monitors_status"} {
|
||||
if !indexes[name] {
|
||||
t.Fatalf("fresh database is missing index %s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user