ci: cache embedded postgres downloaded binaries (#18477)

Updates CI job definitions to cache downloaded binaries for embedded-postgres.
This commit is contained in:
Cian Johnston
2025-06-25 12:00:20 +01:00
committed by GitHub
parent c4e4fe85f9
commit 42fd1c1291
5 changed files with 158 additions and 10 deletions

View File

@ -4,31 +4,43 @@ package main
import (
"database/sql"
"flag"
"log"
"os"
"path/filepath"
"time"
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
)
func main() {
var customPath string
var cachePath string
flag.StringVar(&customPath, "path", "", "Optional custom path for postgres data directory")
flag.StringVar(&cachePath, "cache", "", "Optional custom path for embedded postgres binaries")
flag.Parse()
postgresPath := filepath.Join(os.TempDir(), "coder-test-postgres")
if customPath != "" {
postgresPath = customPath
}
if err := os.MkdirAll(postgresPath, os.ModePerm); err != nil {
log.Fatalf("Failed to create directory %s: %v", postgresPath, err)
}
if cachePath == "" {
cachePath = filepath.Join(postgresPath, "cache")
}
if err := os.MkdirAll(cachePath, os.ModePerm); err != nil {
log.Fatalf("Failed to create directory %s: %v", cachePath, err)
}
ep := embeddedpostgres.NewDatabase(
embeddedpostgres.DefaultConfig().
Version(embeddedpostgres.V16).
BinariesPath(filepath.Join(postgresPath, "bin")).
// Default BinaryRepositoryURL repo1.maven.org is flaky.
BinaryRepositoryURL("https://repo.maven.apache.org/maven2").
DataPath(filepath.Join(postgresPath, "data")).
RuntimePath(filepath.Join(postgresPath, "runtime")).
CachePath(filepath.Join(postgresPath, "cache")).
CachePath(cachePath).
Username("postgres").
Password("postgres").
Database("postgres").
@ -38,8 +50,27 @@ func main() {
)
err := ep.Start()
if err != nil {
panic(err)
log.Fatalf("Failed to start embedded postgres: %v", err)
}
// Troubleshooting: list files in cachePath
if err := filepath.Walk(cachePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
switch {
case info.IsDir():
log.Printf("D: %s", path)
case info.Mode().IsRegular():
log.Printf("F: %s [%s] (%d bytes) %s", path, info.Mode().String(), info.Size(), info.ModTime().Format(time.RFC3339))
default:
log.Printf("Other: %s [%s] %s", path, info.Mode(), info.ModTime().Format(time.RFC3339))
}
return nil
}); err != nil {
log.Printf("Failed to list files in cachePath %s: %v", cachePath, err)
}
// We execute these queries instead of using the embeddedpostgres
// StartParams because it doesn't work on Windows. The library
// seems to have a bug where it sends malformed parameters to
@ -58,21 +89,21 @@ func main() {
}
db, err := sql.Open("postgres", "postgres://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable")
if err != nil {
panic(err)
log.Fatalf("Failed to connect to embedded postgres: %v", err)
}
for _, query := range paramQueries {
if _, err := db.Exec(query); err != nil {
panic(err)
log.Fatalf("Failed to execute setup query %q: %v", query, err)
}
}
if err := db.Close(); err != nil {
panic(err)
log.Fatalf("Failed to close database connection: %v", err)
}
// We restart the database to apply all the parameters.
if err := ep.Stop(); err != nil {
panic(err)
log.Fatalf("Failed to stop embedded postgres after applying parameters: %v", err)
}
if err := ep.Start(); err != nil {
panic(err)
log.Fatalf("Failed to start embedded postgres after applying parameters: %v", err)
}
}