snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled

This commit is contained in:
upstream-mirror
2026-09-16 02:17:34 +05:00
commit 0f4df23290
7340 changed files with 238743 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
package docker
import (
"fmt"
"os/exec"
"golang.org/x/sync/singleflight"
)
var flight singleflight.Group
func Installed() error {
if _, err := exec.LookPath("docker"); err != nil {
return fmt.Errorf("docker not found: %w", err)
}
// Verify the Docker daemon is actually running and accessible.
// Without this check, tests will try Docker, fail on docker pull,
// and t.Fatal instead of falling back to native database support.
if out, err := exec.Command("docker", "info").CombinedOutput(); err != nil {
return fmt.Errorf("docker daemon not available: %w\n%s", err, out)
}
return nil
}

View File

@@ -0,0 +1,104 @@
package docker
import (
"context"
"database/sql"
"fmt"
"os/exec"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
)
var mysqlHost string
func StartMySQLServer(c context.Context) (string, error) {
if err := Installed(); err != nil {
return "", err
}
if mysqlHost != "" {
return mysqlHost, nil
}
value, err, _ := flight.Do("mysql", func() (any, error) {
host, err := startMySQLServer(c)
if err != nil {
return "", err
}
mysqlHost = host
return host, nil
})
if err != nil {
return "", err
}
data, ok := value.(string)
if !ok {
return "", fmt.Errorf("returned value was not a string")
}
return data, nil
}
func startMySQLServer(c context.Context) (string, error) {
{
_, err := exec.Command("docker", "pull", "mysql:9").CombinedOutput()
if err != nil {
return "", fmt.Errorf("docker pull: mysql:9 %w", err)
}
}
var exists bool
{
cmd := exec.Command("docker", "container", "inspect", "sqlc_sqltest_docker_mysql")
// This means we've already started the container
exists = cmd.Run() == nil
}
if !exists {
cmd := exec.Command("docker", "run",
"--name", "sqlc_sqltest_docker_mysql",
"-e", "MYSQL_ROOT_PASSWORD=mysecretpassword",
"-e", "MYSQL_DATABASE=dinotest",
"-p", "3306:3306",
"-d",
"mysql:9",
)
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
msg := `Conflict. The container name "/sqlc_sqltest_docker_mysql" is already in use by container`
if !strings.Contains(string(output), msg) && err != nil {
return "", err
}
}
ctx, cancel := context.WithTimeout(c, 10*time.Second)
defer cancel()
// Create a ticker that fires every 10ms
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
uri := "root:mysecretpassword@/dinotest?multiStatements=true&parseTime=true"
db, err := sql.Open("mysql", uri)
if err != nil {
return "", fmt.Errorf("sql.Open: %w", err)
}
defer db.Close()
for {
select {
case <-ctx.Done():
return "", fmt.Errorf("timeout reached: %w", ctx.Err())
case <-ticker.C:
// Run your function here
if err := db.PingContext(ctx); err != nil {
continue
}
return uri, nil
}
}
}

View File

@@ -0,0 +1,105 @@
package docker
import (
"context"
"fmt"
"log/slog"
"os/exec"
"strings"
"time"
"github.com/jackc/pgx/v5"
)
var postgresHost string
func StartPostgreSQLServer(c context.Context) (string, error) {
if err := Installed(); err != nil {
return "", err
}
if postgresHost != "" {
return postgresHost, nil
}
value, err, _ := flight.Do("postgresql", func() (any, error) {
host, err := startPostgreSQLServer(c)
if err != nil {
return "", err
}
postgresHost = host
return host, err
})
if err != nil {
return "", err
}
data, ok := value.(string)
if !ok {
return "", fmt.Errorf("returned value was not a string")
}
return data, nil
}
func startPostgreSQLServer(c context.Context) (string, error) {
{
_, err := exec.Command("docker", "pull", "postgres:16").CombinedOutput()
if err != nil {
return "", fmt.Errorf("docker pull: postgres:16 %w", err)
}
}
uri := "postgres://postgres:mysecretpassword@localhost:5432/postgres?sslmode=disable"
var exists bool
{
cmd := exec.Command("docker", "container", "inspect", "sqlc_sqltest_docker_postgres")
// This means we've already started the container
exists = cmd.Run() == nil
}
if !exists {
cmd := exec.Command("docker", "run",
"--name", "sqlc_sqltest_docker_postgres",
"-e", "POSTGRES_PASSWORD=mysecretpassword",
"-e", "POSTGRES_USER=postgres",
"-p", "5432:5432",
"-d",
"postgres:16",
"-c", "max_connections=200",
)
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
msg := `Conflict. The container name "/sqlc_sqltest_docker_postgres" is already in use by container`
if !strings.Contains(string(output), msg) && err != nil {
return "", err
}
}
ctx, cancel := context.WithTimeout(c, 5*time.Second)
defer cancel()
// Create a ticker that fires every 10ms
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return "", fmt.Errorf("timeout reached: %w", ctx.Err())
case <-ticker.C:
// Run your function here
conn, err := pgx.Connect(ctx, uri)
if err != nil {
slog.Debug("sqltest", "connect", err)
continue
}
defer conn.Close(ctx)
if err := conn.Ping(ctx); err != nil {
slog.Error("sqltest", "ping", err)
continue
}
return uri, nil
}
}
}

View File

@@ -0,0 +1,13 @@
package local
import "math/rand"
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func id() string {
b := make([]rune, 10)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}

View File

@@ -0,0 +1,109 @@
package local
import (
"context"
"database/sql"
"fmt"
"os"
"strings"
"sync"
"testing"
"github.com/go-sql-driver/mysql"
migrate "github.com/sqlc-dev/sqlc/internal/migrations"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
"github.com/sqlc-dev/sqlc/internal/sqltest/docker"
"github.com/sqlc-dev/sqlc/internal/sqltest/native"
)
var mysqlSync sync.Once
var mysqlPool *sql.DB
func MySQL(t *testing.T, migrations []string) string {
ctx := context.Background()
t.Helper()
dburi := os.Getenv("MYSQL_SERVER_URI")
if dburi == "" {
if ierr := docker.Installed(); ierr == nil {
u, err := docker.StartMySQLServer(ctx)
if err != nil {
t.Fatal(err)
}
dburi = u
} else if ierr := native.Supported(); ierr == nil {
// Fall back to native installation when Docker is not available
u, err := native.StartMySQLServer(ctx)
if err != nil {
t.Fatal(err)
}
dburi = u
} else {
t.Skip("MYSQL_SERVER_URI is empty and neither Docker nor native installation is available")
}
}
mysqlSync.Do(func() {
db, err := sql.Open("mysql", dburi)
if err != nil {
t.Fatal(err)
}
mysqlPool = db
})
if mysqlPool == nil {
t.Fatalf("MySQL pool creation failed")
}
var seed []string
files, err := sqlpath.Glob(migrations)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
blob, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
seed = append(seed, migrate.RemoveRollbackStatements(string(blob)))
}
cfg, err := mysql.ParseDSN(dburi)
if err != nil {
t.Fatal(err)
}
name := fmt.Sprintf("sqlc_test_%s", id())
if _, err := mysqlPool.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE `%s`", name)); err != nil {
t.Fatal(err)
}
cfg.DBName = name
dropQuery := fmt.Sprintf("DROP DATABASE `%s`", name)
t.Cleanup(func() {
if _, err := mysqlPool.ExecContext(ctx, dropQuery); err != nil {
t.Fatal(err)
}
})
db, err := sql.Open("mysql", cfg.FormatDSN())
if err != nil {
t.Fatalf("connect %s: %s", name, err)
}
defer db.Close()
for _, q := range seed {
if len(strings.TrimSpace(q)) == 0 {
continue
}
if _, err := db.ExecContext(ctx, q); err != nil {
t.Fatalf("%s: %s", q, err)
}
}
return cfg.FormatDSN()
}

View File

@@ -0,0 +1,136 @@
package local
import (
"context"
"fmt"
"hash/fnv"
"net/url"
"os"
"strings"
"testing"
"github.com/jackc/pgx/v5"
"golang.org/x/sync/singleflight"
migrate "github.com/sqlc-dev/sqlc/internal/migrations"
"github.com/sqlc-dev/sqlc/internal/pgx/poolcache"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
"github.com/sqlc-dev/sqlc/internal/sqltest/docker"
"github.com/sqlc-dev/sqlc/internal/sqltest/native"
)
var flight singleflight.Group
var cache = poolcache.New()
func PostgreSQL(t *testing.T, migrations []string) string {
return postgreSQL(t, migrations, true)
}
func ReadOnlyPostgreSQL(t *testing.T, migrations []string) string {
return postgreSQL(t, migrations, false)
}
func postgreSQL(t *testing.T, migrations []string, rw bool) string {
ctx := context.Background()
t.Helper()
dburi := os.Getenv("POSTGRESQL_SERVER_URI")
if dburi == "" {
if ierr := docker.Installed(); ierr == nil {
u, err := docker.StartPostgreSQLServer(ctx)
if err != nil {
t.Fatal(err)
}
dburi = u
} else if ierr := native.Supported(); ierr == nil {
// Fall back to native installation when Docker is not available
u, err := native.StartPostgreSQLServer(ctx)
if err != nil {
t.Fatal(err)
}
dburi = u
} else {
t.Skip("POSTGRESQL_SERVER_URI is empty and neither Docker nor native installation is available")
}
}
postgresPool, err := cache.Open(ctx, dburi)
if err != nil {
t.Fatalf("PostgreSQL pool creation failed: %s", err)
}
var seed []string
files, err := sqlpath.Glob(migrations)
if err != nil {
t.Fatal(err)
}
h := fnv.New64()
for _, f := range files {
blob, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
h.Write(blob)
seed = append(seed, migrate.RemoveRollbackStatements(string(blob)))
}
var name string
if rw {
name = fmt.Sprintf("sqlc_test_%s", id())
} else {
name = fmt.Sprintf("sqlc_test_%x", h.Sum(nil))
}
uri, err := url.Parse(dburi)
if err != nil {
t.Fatal(err)
}
uri.Path = name
dropQuery := fmt.Sprintf(`DROP DATABASE IF EXISTS "%s" WITH (FORCE)`, name)
key := uri.String()
_, err, _ = flight.Do(key, func() (any, error) {
row := postgresPool.QueryRow(ctx,
fmt.Sprintf(`SELECT datname FROM pg_database WHERE datname = '%s'`, name))
var datname string
if err := row.Scan(&datname); err == nil {
t.Logf("database exists: %s", name)
return nil, nil
}
t.Logf("creating database: %s", name)
if _, err := postgresPool.Exec(ctx, fmt.Sprintf(`CREATE DATABASE "%s"`, name)); err != nil {
return nil, err
}
conn, err := pgx.Connect(ctx, uri.String())
if err != nil {
return nil, fmt.Errorf("connect %s: %s", name, err)
}
defer conn.Close(ctx)
for _, q := range seed {
if len(strings.TrimSpace(q)) == 0 {
continue
}
if _, err := conn.Exec(ctx, q); err != nil {
return nil, fmt.Errorf("%s: %s", q, err)
}
}
return nil, nil
})
if rw || err != nil {
t.Cleanup(func() {
if _, err := postgresPool.Exec(ctx, dropQuery); err != nil {
t.Fatalf("failed cleaning up: %s", err)
}
})
}
if err != nil {
t.Fatalf("create db: %s", err)
}
return key
}

104
internal/sqltest/mysql.go Normal file
View File

@@ -0,0 +1,104 @@
package sqltest
import (
"database/sql"
"fmt"
"math/rand"
"os"
"path/filepath"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func id() string {
b := make([]rune, 10)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func MySQL(t *testing.T, migrations []string) (*sql.DB, func()) {
// For each test, pick a new database name at random.
name := "sqltest_mysql_" + id()
return CreateMySQLDatabase(t, name, migrations)
}
func CreateMySQLDatabase(t *testing.T, name string, migrations []string) (*sql.DB, func()) {
t.Helper()
data := os.Getenv("MYSQL_DATABASE")
host := os.Getenv("MYSQL_HOST")
pass := os.Getenv("MYSQL_ROOT_PASSWORD")
port := os.Getenv("MYSQL_PORT")
user := os.Getenv("MYSQL_USER")
if user == "" {
user = "root"
}
if pass == "" {
pass = "mysecretpassword"
}
if port == "" {
port = "3306"
}
if host == "" {
host = "127.0.0.1"
}
if data == "" {
data = "dinotest"
}
source := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?multiStatements=true&parseTime=true", user, pass, host, port, data)
t.Logf("db: %s", source)
db, err := sql.Open("mysql", source)
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec("CREATE DATABASE " + name); err != nil {
t.Fatal(err)
}
source = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?multiStatements=true&parseTime=true", user, pass, host, port, name)
sdb, err := sql.Open("mysql", source)
if err != nil {
t.Fatal(err)
}
files, err := sqlpath.Glob(migrations)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
blob, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
if _, err := sdb.Exec(string(blob)); err != nil {
t.Fatalf("%s: %s", filepath.Base(f), err)
}
}
return sdb, func() {
// Drop the test db after test runs
if _, err := db.Exec("DROP DATABASE " + name); err != nil {
t.Fatal(err)
}
}
}

View File

@@ -0,0 +1,20 @@
package native
import (
"fmt"
"os/exec"
"runtime"
)
// Supported returns nil if native database installation is supported on this platform.
// Currently only Linux (Ubuntu/Debian) is supported.
func Supported() error {
if runtime.GOOS != "linux" {
return fmt.Errorf("native database installation only supported on linux, got %s", runtime.GOOS)
}
// Check if apt-get is available (Debian/Ubuntu)
if _, err := exec.LookPath("apt-get"); err != nil {
return fmt.Errorf("apt-get not found: %w", err)
}
return nil
}

View File

@@ -0,0 +1,203 @@
package native
import (
"context"
"database/sql"
"fmt"
"log/slog"
"os/exec"
"time"
_ "github.com/go-sql-driver/mysql"
"golang.org/x/sync/singleflight"
)
var mysqlFlight singleflight.Group
var mysqlURI string
// StartMySQLServer starts an existing MySQL installation natively (without Docker).
func StartMySQLServer(ctx context.Context) (string, error) {
if err := Supported(); err != nil {
return "", err
}
if mysqlURI != "" {
return mysqlURI, nil
}
value, err, _ := mysqlFlight.Do("mysql", func() (any, error) {
uri, err := startMySQLServer(ctx)
if err != nil {
return "", err
}
mysqlURI = uri
return uri, nil
})
if err != nil {
return "", err
}
data, ok := value.(string)
if !ok {
return "", fmt.Errorf("returned value was not a string")
}
return data, nil
}
func startMySQLServer(ctx context.Context) (string, error) {
// Standard URI for test MySQL
uri := "root:mysecretpassword@tcp(localhost:3306)/mysql?multiStatements=true&parseTime=true"
// Try to connect first - it might already be running
if err := waitForMySQL(ctx, uri, 500*time.Millisecond); err == nil {
slog.Info("native/mysql", "status", "already running")
return uri, nil
}
// Also try without password (default MySQL installation)
uriNoPassword := "root@tcp(localhost:3306)/mysql?multiStatements=true&parseTime=true"
if err := waitForMySQL(ctx, uriNoPassword, 500*time.Millisecond); err == nil {
slog.Info("native/mysql", "status", "already running (no password)")
// MySQL is running without password, try to set one
if err := setMySQLPassword(ctx); err != nil {
slog.Debug("native/mysql", "set-password-error", err)
// Return without password if we can't set one
return uriNoPassword, nil
}
// Try again with password
if err := waitForMySQL(ctx, uri, 1*time.Second); err == nil {
return uri, nil
}
// If password didn't work, use no password
return uriNoPassword, nil
}
// Try to start existing MySQL service (might be installed but not running)
if _, err := exec.LookPath("mysqld"); err == nil {
slog.Info("native/mysql", "status", "starting existing service")
if err := startMySQLService(); err != nil {
slog.Debug("native/mysql", "start-error", err)
} else {
// Wait for MySQL to be ready
waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
// Try with password first
if err := waitForMySQL(waitCtx, uri, 15*time.Second); err == nil {
return uri, nil
}
// Try without password
if err := waitForMySQL(waitCtx, uriNoPassword, 15*time.Second); err == nil {
if err := setMySQLPassword(ctx); err != nil {
slog.Debug("native/mysql", "set-password-error", err)
return uriNoPassword, nil
}
if err := waitForMySQL(ctx, uri, 1*time.Second); err == nil {
return uri, nil
}
return uriNoPassword, nil
}
}
}
return "", fmt.Errorf("MySQL is not installed or could not be started")
}
func startMySQLService() error {
// Try systemctl first
cmd := exec.Command("sudo", "systemctl", "start", "mysql")
if err := cmd.Run(); err == nil {
// Give MySQL time to fully initialize
time.Sleep(2 * time.Second)
return nil
}
// Try mysqld
cmd = exec.Command("sudo", "systemctl", "start", "mysqld")
if err := cmd.Run(); err == nil {
time.Sleep(2 * time.Second)
return nil
}
// Try service command
cmd = exec.Command("sudo", "service", "mysql", "start")
if err := cmd.Run(); err == nil {
time.Sleep(2 * time.Second)
return nil
}
cmd = exec.Command("sudo", "service", "mysqld", "start")
if err := cmd.Run(); err == nil {
time.Sleep(2 * time.Second)
return nil
}
return fmt.Errorf("could not start MySQL service")
}
func setMySQLPassword(ctx context.Context) error {
// Connect without password
db, err := sql.Open("mysql", "root@tcp(localhost:3306)/mysql")
if err != nil {
return err
}
defer db.Close()
// Set root password using mysql_native_password for broader compatibility
_, err = db.ExecContext(ctx, "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mysecretpassword';")
if err != nil {
// Try without specifying auth plugin
_, err = db.ExecContext(ctx, "ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysecretpassword';")
if err != nil {
// Try older MySQL syntax
_, err = db.ExecContext(ctx, "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mysecretpassword');")
if err != nil {
return fmt.Errorf("could not set MySQL password: %w", err)
}
}
}
// Flush privileges
_, _ = db.ExecContext(ctx, "FLUSH PRIVILEGES;")
return nil
}
func waitForMySQL(ctx context.Context, uri string, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
// Make an immediate first attempt before waiting for the ticker
if err := tryMySQLConnection(ctx, uri); err == nil {
return nil
}
var lastErr error
for {
select {
case <-ctx.Done():
return fmt.Errorf("context cancelled: %w (last error: %v)", ctx.Err(), lastErr)
case <-ticker.C:
if time.Now().After(deadline) {
return fmt.Errorf("timeout waiting for MySQL (last error: %v)", lastErr)
}
if err := tryMySQLConnection(ctx, uri); err != nil {
lastErr = err
continue
}
return nil
}
}
}
func tryMySQLConnection(ctx context.Context, uri string) error {
db, err := sql.Open("mysql", uri)
if err != nil {
slog.Debug("native/mysql", "open-attempt", err)
return err
}
defer db.Close()
// Use a short timeout for ping to avoid hanging
pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
return db.PingContext(pingCtx)
}

View File

@@ -0,0 +1,221 @@
package native
import (
"context"
"fmt"
"log/slog"
"os/exec"
"strings"
"time"
"github.com/jackc/pgx/v5"
"golang.org/x/sync/singleflight"
)
var postgresFlight singleflight.Group
var postgresURI string
// StartPostgreSQLServer starts an existing PostgreSQL installation natively (without Docker).
func StartPostgreSQLServer(ctx context.Context) (string, error) {
if err := Supported(); err != nil {
return "", err
}
if postgresURI != "" {
return postgresURI, nil
}
value, err, _ := postgresFlight.Do("postgresql", func() (any, error) {
uri, err := startPostgreSQLServer(ctx)
if err != nil {
return "", err
}
postgresURI = uri
return uri, nil
})
if err != nil {
return "", err
}
data, ok := value.(string)
if !ok {
return "", fmt.Errorf("returned value was not a string")
}
return data, nil
}
func startPostgreSQLServer(ctx context.Context) (string, error) {
// Standard URI for test PostgreSQL
uri := "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
// Try to connect first - it might already be running
if err := waitForPostgres(ctx, uri, 500*time.Millisecond); err == nil {
slog.Info("native/postgres", "status", "already running")
return uri, nil
}
// Check if PostgreSQL is installed
if _, err := exec.LookPath("psql"); err != nil {
return "", fmt.Errorf("PostgreSQL is not installed (psql not found)")
}
// Start PostgreSQL service
slog.Info("native/postgres", "status", "starting service")
// Try systemctl first, fall back to pg_ctlcluster
if err := startPostgresService(); err != nil {
return "", fmt.Errorf("failed to start PostgreSQL: %w", err)
}
// Configure PostgreSQL for password authentication
if err := configurePostgres(); err != nil {
return "", fmt.Errorf("failed to configure PostgreSQL: %w", err)
}
// Wait for PostgreSQL to be ready
waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
if err := waitForPostgres(waitCtx, uri, 30*time.Second); err != nil {
return "", fmt.Errorf("timeout waiting for PostgreSQL: %w", err)
}
return uri, nil
}
func startPostgresService() error {
// Try systemctl first
cmd := exec.Command("sudo", "systemctl", "start", "postgresql")
if err := cmd.Run(); err == nil {
return nil
}
// Try service command
cmd = exec.Command("sudo", "service", "postgresql", "start")
if err := cmd.Run(); err == nil {
return nil
}
// Try pg_ctlcluster (Debian/Ubuntu specific)
// Find the installed PostgreSQL version
output, err := exec.Command("ls", "/etc/postgresql/").CombinedOutput()
if err != nil {
return fmt.Errorf("could not find PostgreSQL version: %w", err)
}
versions := strings.Fields(string(output))
if len(versions) == 0 {
return fmt.Errorf("no PostgreSQL version found in /etc/postgresql/")
}
version := versions[0]
cmd = exec.Command("sudo", "pg_ctlcluster", version, "main", "start")
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("pg_ctlcluster start failed: %w\n%s", err, output)
}
return nil
}
func configurePostgres() error {
// Set password for postgres user using sudo -u postgres
cmd := exec.Command("sudo", "-u", "postgres", "psql", "-c", "ALTER USER postgres PASSWORD 'postgres';")
if output, err := cmd.CombinedOutput(); err != nil {
// This might fail if password is already set, which is fine
slog.Debug("native/postgres", "set-password", string(output))
}
// Update pg_hba.conf to allow password authentication
// First, find the pg_hba.conf file
output, err := exec.Command("sudo", "-u", "postgres", "psql", "-t", "-c", "SHOW hba_file;").CombinedOutput()
if err != nil {
return fmt.Errorf("could not find hba_file: %w", err)
}
hbaFile := strings.TrimSpace(string(output))
if hbaFile == "" {
return fmt.Errorf("empty hba_file path")
}
// Check if we need to update pg_hba.conf
catOutput, err := exec.Command("sudo", "cat", hbaFile).CombinedOutput()
if err != nil {
return fmt.Errorf("could not read %s: %w", hbaFile, err)
}
// If md5 or scram-sha-256 auth is not configured for local connections, add it
content := string(catOutput)
if !strings.Contains(content, "host all all 127.0.0.1/32 md5") &&
!strings.Contains(content, "host all all 127.0.0.1/32 scram-sha-256") {
// Prepend a rule for localhost password authentication
newRule := "host all all 127.0.0.1/32 md5\n"
// Use sed to add the rule at the beginning (after comments)
cmd := exec.Command("sudo", "bash", "-c",
fmt.Sprintf(`echo '%s' | cat - %s > /tmp/pg_hba.conf.new && sudo mv /tmp/pg_hba.conf.new %s`,
newRule, hbaFile, hbaFile))
if output, err := cmd.CombinedOutput(); err != nil {
slog.Debug("native/postgres", "update-hba-error", string(output))
}
// Reload PostgreSQL to apply changes
if err := reloadPostgres(); err != nil {
slog.Debug("native/postgres", "reload-error", err)
}
}
return nil
}
func reloadPostgres() error {
// Try systemctl reload
cmd := exec.Command("sudo", "systemctl", "reload", "postgresql")
if err := cmd.Run(); err == nil {
return nil
}
// Try service reload
cmd = exec.Command("sudo", "service", "postgresql", "reload")
if err := cmd.Run(); err == nil {
return nil
}
// Try pg_ctlcluster reload
output, _ := exec.Command("ls", "/etc/postgresql/").CombinedOutput()
versions := strings.Fields(string(output))
if len(versions) > 0 {
cmd = exec.Command("sudo", "pg_ctlcluster", versions[0], "main", "reload")
return cmd.Run()
}
return fmt.Errorf("could not reload PostgreSQL")
}
func waitForPostgres(ctx context.Context, uri string, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
var lastErr error
for {
select {
case <-ctx.Done():
return fmt.Errorf("context cancelled: %w (last error: %v)", ctx.Err(), lastErr)
case <-ticker.C:
if time.Now().After(deadline) {
return fmt.Errorf("timeout waiting for PostgreSQL (last error: %v)", lastErr)
}
conn, err := pgx.Connect(ctx, uri)
if err != nil {
lastErr = err
slog.Debug("native/postgres", "connect-attempt", err)
continue
}
if err := conn.Ping(ctx); err != nil {
lastErr = err
conn.Close(ctx)
continue
}
conn.Close(ctx)
return nil
}
}
}

View File

@@ -0,0 +1,56 @@
package sqltest
import (
"database/sql"
"os"
"path/filepath"
"testing"
_ "github.com/ncruces/go-sqlite3/driver"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
_ "github.com/sqlc-dev/sqlc/internal/sqlite3ext"
)
func SQLite(t *testing.T, migrations []string) (*sql.DB, func()) {
t.Helper()
// For each test, pick a new database name at random.
source, err := os.CreateTemp("", "sqltest_sqlite_")
if err != nil {
t.Fatal(err)
}
if err := source.Close(); err != nil {
t.Fatal(err)
}
return CreateSQLiteDatabase(t, source.Name(), migrations)
}
func CreateSQLiteDatabase(t *testing.T, path string, migrations []string) (*sql.DB, func()) {
t.Helper()
t.Logf("open %s\n", path)
sdb, err := sql.Open("sqlite3", path)
if err != nil {
t.Fatal(err)
}
files, err := sqlpath.Glob(migrations)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
blob, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
if _, err := sdb.Exec(string(blob)); err != nil {
t.Fatalf("%s: %s", filepath.Base(f), err)
}
}
return sdb, func() {
if _, err := os.Stat(path); err == nil {
os.Remove(path)
}
}
}