mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: add postgres template caching for tests (#15336)
This PR is the first in a series aimed at closing [#15109](https://github.com/coder/coder/issues/15109). ### Changes - **Template Database Creation:** `dbtestutil.Open` now has the ability to create a template database if none is provided via `DB_FROM`. The template database’s name is derived from a hash of the migration files, ensuring that it can be reused across tests and is automatically updated whenever migrations change. - **Optimized Database Handling:** Previously, `dbtestutil.Open` would spin up a new container for each test when `DB_FROM` was unset. Now, it first checks for an active PostgreSQL instance on `localhost:5432`. If none is found, it creates a single container that remains available for subsequent tests, eliminating repeated container startups. These changes address the long individual test times (10+ seconds) reported by some users, likely due to the time Docker took to start and complete migrations.
This commit is contained in:
@@ -2,11 +2,16 @@ package migrations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/golang-migrate/migrate/v4/source"
|
||||
@@ -17,6 +22,56 @@ import (
|
||||
//go:embed *.sql
|
||||
var migrations embed.FS
|
||||
|
||||
var (
|
||||
migrationsHash string
|
||||
migrationsHashOnce sync.Once
|
||||
)
|
||||
|
||||
// A migrations hash is a sha256 hash of the contents and names
|
||||
// of the migrations sorted by filename.
|
||||
func calculateMigrationsHash(migrationsFs embed.FS) (string, error) {
|
||||
files, err := migrationsFs.ReadDir(".")
|
||||
if err != nil {
|
||||
return "", xerrors.Errorf("read migrations directory: %w", err)
|
||||
}
|
||||
sortedFiles := make([]fs.DirEntry, len(files))
|
||||
copy(sortedFiles, files)
|
||||
sort.Slice(sortedFiles, func(i, j int) bool {
|
||||
return sortedFiles[i].Name() < sortedFiles[j].Name()
|
||||
})
|
||||
|
||||
var builder strings.Builder
|
||||
for _, file := range sortedFiles {
|
||||
if _, err := builder.WriteString(file.Name()); err != nil {
|
||||
return "", xerrors.Errorf("write migration file name %q: %w", file.Name(), err)
|
||||
}
|
||||
content, err := migrationsFs.ReadFile(file.Name())
|
||||
if err != nil {
|
||||
return "", xerrors.Errorf("read migration file %q: %w", file.Name(), err)
|
||||
}
|
||||
if _, err := builder.Write(content); err != nil {
|
||||
return "", xerrors.Errorf("write migration file content %q: %w", file.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
hash := sha256.New()
|
||||
if _, err := hash.Write([]byte(builder.String())); err != nil {
|
||||
return "", xerrors.Errorf("write to hash: %w", err)
|
||||
}
|
||||
return fmt.Sprintf("%x", hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
func GetMigrationsHash() string {
|
||||
migrationsHashOnce.Do(func() {
|
||||
hash, err := calculateMigrationsHash(migrations)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
migrationsHash = hash
|
||||
})
|
||||
return migrationsHash
|
||||
}
|
||||
|
||||
func setup(db *sql.DB, migs fs.FS) (source.Driver, *migrate.Migrate, error) {
|
||||
if migs == nil {
|
||||
migs = migrations
|
||||
|
||||
@@ -95,9 +95,8 @@ func TestMigrate(t *testing.T) {
|
||||
func testSQLDB(t testing.TB) *sql.DB {
|
||||
t.Helper()
|
||||
|
||||
connection, closeFn, err := dbtestutil.Open()
|
||||
connection, err := dbtestutil.Open(t)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(closeFn)
|
||||
|
||||
db, err := sql.Open("postgres", connection)
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user