Files
seaweedfs/weed/filer/store_test/postgres.go
T
Chris LuandClaude Opus 5.5 6c6cd81b89 filer: run postgres2 and mysql2 through the conformance suite
RUN_POSTGRES_TESTS and RUN_MYSQL_TESTS enable them, as RUN_REDIS_TESTS
does the Redis stores; each test gets a schema or database of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
2026-12-01 16:34:54 -08:00

53 lines
1.4 KiB
Go

package store_test
import (
"database/sql"
"fmt"
"os"
"strconv"
"testing"
"time"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/seaweedfs/seaweedfs/weed/util/pgxutil"
)
// NewPostgresTestDatabase gives the test a schema of its own, dropped after
// it. PGHOST, PGPORT, PGUSER, PGPASSWORD and PGDATABASE override the defaults.
func NewPostgresTestDatabase(t *testing.T) pgxutil.DSNOptions {
if os.Getenv("RUN_POSTGRES_TESTS") != "1" {
t.Skip("postgres tests are disabled. Start a postgres server and set RUN_POSTGRES_TESTS=1 to enable, it defaults to postgres:postgres@127.0.0.1:5432/postgres.")
}
port, _ := strconv.Atoi(envOr("PGPORT", "5432"))
opts := pgxutil.DSNOptions{
Hostname: envOr("PGHOST", "127.0.0.1"),
Port: port,
User: envOr("PGUSER", "postgres"),
Password: envOr("PGPASSWORD", "postgres"),
Database: envOr("PGDATABASE", "postgres"),
SSLMode: "disable",
}
dsn, _ := pgxutil.BuildDSN(opts)
conn, err := sql.Open("pgx", dsn)
if err != nil {
t.Fatal(err)
}
opts.Schema = fmt.Sprintf("test_%d", time.Now().UnixNano())
if _, err := conn.Exec(`CREATE SCHEMA "` + opts.Schema + `"`); err != nil {
conn.Close()
t.Fatal(err)
}
t.Cleanup(func() {
conn.Exec(`DROP SCHEMA "` + opts.Schema + `" CASCADE`)
conn.Close()
})
return opts
}
func envOr(name, fallback string) string {
if v := os.Getenv(name); v != "" {
return v
}
return fallback
}