updates on the ai and agent and all thse things awith onboarding
This commit is contained in:
48
internal/testpg/testpg.go
Normal file
48
internal/testpg/testpg.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// Package testpg opens throwaway Postgres schemas for integration tests.
|
||||
// Imported only from _test.go files, so it never reaches the server binary.
|
||||
package testpg
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
var schemaName = regexp.MustCompile(`^[a-z_][a-z0-9_]{0,62}$`)
|
||||
|
||||
// Open connects to a THROWAWAY Postgres (the DSN from REGISTRY_TEST_DSN)
|
||||
// inside a schema of its own, created if missing.
|
||||
//
|
||||
// Each test package passes a different schema: `go test ./...` runs packages
|
||||
// in parallel, and two packages dropping and recreating the same tables in the
|
||||
// same schema at once fail each other at random.
|
||||
func Open(t testing.TB, dsn, schema string) *gorm.DB {
|
||||
t.Helper()
|
||||
if !schemaName.MatchString(schema) {
|
||||
t.Fatalf("bad test schema name %q", schema)
|
||||
}
|
||||
cfg := &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}
|
||||
base, err := gorm.Open(postgres.Open(dsn), cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
if err := base.Exec("CREATE SCHEMA IF NOT EXISTS " + schema).Error; err != nil {
|
||||
t.Fatalf("create schema %s: %v", schema, err)
|
||||
}
|
||||
if sqlDB, err := base.DB(); err == nil {
|
||||
_ = sqlDB.Close()
|
||||
}
|
||||
db, err := gorm.Open(postgres.Open(dsn+" search_path="+schema), cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("connect to schema %s: %v", schema, err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if sqlDB, err := db.DB(); err == nil {
|
||||
_ = sqlDB.Close()
|
||||
}
|
||||
})
|
||||
return db
|
||||
}
|
||||
Reference in New Issue
Block a user