From 693c3354477942001544f4d2b223bba09b90c190 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 18 Dec 2025 18:01:42 +0000 Subject: [PATCH 1/9] feat: add native database support for e2e tests without Docker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new native package (internal/sqltest/native/) that can install and start PostgreSQL and MySQL servers directly on Linux systems without requiring Docker. This enables running end-to-end tests in environments where Docker is not available (e.g., CI environments without Docker). Key changes: - Add internal/sqltest/native/ package with support for: - PostgreSQL installation and startup via apt-get and systemctl/pg_ctlcluster - MySQL installation and startup via apt-get and systemctl/service - Graceful fallback when installation fails (e.g., no network) - Update internal/sqltest/local/ to fall back to native installation when Docker is not available - Update internal/endtoend/endtoend_test.go to: - Check environment variables first (POSTGRESQL_SERVER_URI, MYSQL_SERVER_URI) - Fall back to Docker, then native installation - Skip database-specific tests when that database is unavailable - Support partial database availability (run with just PostgreSQL) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/endtoend/endtoend_test.go | 90 ++++++++--- internal/sqltest/local/mysql.go | 10 +- internal/sqltest/local/postgres.go | 10 +- internal/sqltest/native/enabled.go | 20 +++ internal/sqltest/native/mysql.go | 229 +++++++++++++++++++++++++++ internal/sqltest/native/postgres.go | 235 ++++++++++++++++++++++++++++ 6 files changed, 573 insertions(+), 21 deletions(-) create mode 100644 internal/sqltest/native/enabled.go create mode 100644 internal/sqltest/native/mysql.go create mode 100644 internal/sqltest/native/postgres.go diff --git a/internal/endtoend/endtoend_test.go b/internal/endtoend/endtoend_test.go index 537307e453..c99b2ea16d 100644 --- a/internal/endtoend/endtoend_test.go +++ b/internal/endtoend/endtoend_test.go @@ -18,6 +18,7 @@ import ( "github.com/sqlc-dev/sqlc/internal/config" "github.com/sqlc-dev/sqlc/internal/opts" "github.com/sqlc-dev/sqlc/internal/sqltest/docker" + "github.com/sqlc-dev/sqlc/internal/sqltest/native" ) func lineEndings() cmp.Option { @@ -113,23 +114,63 @@ func TestReplay(t *testing.T) { ctx := context.Background() var mysqlURI, postgresURI string - if err := docker.Installed(); err == nil { - { - host, err := docker.StartPostgreSQLServer(ctx) - if err != nil { - t.Fatalf("starting postgresql failed: %s", err) + + // First, check environment variables + if uri := os.Getenv("POSTGRESQL_SERVER_URI"); uri != "" { + postgresURI = uri + } + if uri := os.Getenv("MYSQL_SERVER_URI"); uri != "" { + mysqlURI = uri + } + + // Try Docker for any missing databases + if postgresURI == "" || mysqlURI == "" { + if err := docker.Installed(); err == nil { + if postgresURI == "" { + host, err := docker.StartPostgreSQLServer(ctx) + if err != nil { + t.Logf("docker postgresql startup failed: %s", err) + } else { + postgresURI = host + } + } + if mysqlURI == "" { + host, err := docker.StartMySQLServer(ctx) + if err != nil { + t.Logf("docker mysql startup failed: %s", err) + } else { + mysqlURI = host + } } - postgresURI = host } - { - host, err := docker.StartMySQLServer(ctx) - if err != nil { - t.Fatalf("starting mysql failed: %s", err) + } + + // Try native installation for any missing databases (Linux only) + if postgresURI == "" || mysqlURI == "" { + if err := native.Supported(); err == nil { + if postgresURI == "" { + host, err := native.StartPostgreSQLServer(ctx) + if err != nil { + t.Logf("native postgresql startup failed: %s", err) + } else { + postgresURI = host + } + } + if mysqlURI == "" { + host, err := native.StartMySQLServer(ctx) + if err != nil { + t.Logf("native mysql startup failed: %s", err) + } else { + mysqlURI = host + } } - mysqlURI = host } } + // Log which databases are available + t.Logf("PostgreSQL available: %v (URI: %s)", postgresURI != "", postgresURI) + t.Logf("MySQL available: %v (URI: %s)", mysqlURI != "", mysqlURI) + contexts := map[string]textContext{ "base": { Mutate: func(t *testing.T, path string) func(*config.Config) { return func(c *config.Config) {} }, @@ -138,26 +179,37 @@ func TestReplay(t *testing.T) { "managed-db": { Mutate: func(t *testing.T, path string) func(*config.Config) { return func(c *config.Config) { - c.Servers = []config.Server{ - { + // Only add servers that are available + var servers []config.Server + if postgresURI != "" { + servers = append(servers, config.Server{ Name: "postgres", Engine: config.EnginePostgreSQL, URI: postgresURI, - }, - - { + }) + } + if mysqlURI != "" { + servers = append(servers, config.Server{ Name: "mysql", Engine: config.EngineMySQL, URI: mysqlURI, - }, + }) } + c.Servers = servers + for i := range c.SQL { switch c.SQL[i].Engine { case config.EnginePostgreSQL: + if postgresURI == "" { + t.Skipf("PostgreSQL not available") + } c.SQL[i].Database = &config.Database{ Managed: true, } case config.EngineMySQL: + if mysqlURI == "" { + t.Skipf("MySQL not available") + } c.SQL[i].Database = &config.Database{ Managed: true, } @@ -172,8 +224,8 @@ func TestReplay(t *testing.T) { } }, Enabled: func() bool { - err := docker.Installed() - return err == nil + // Enable if at least one database is available + return postgresURI != "" || mysqlURI != "" }, }, } diff --git a/internal/sqltest/local/mysql.go b/internal/sqltest/local/mysql.go index dedd3dfd78..05733f6e8b 100644 --- a/internal/sqltest/local/mysql.go +++ b/internal/sqltest/local/mysql.go @@ -14,6 +14,7 @@ import ( 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 @@ -31,8 +32,15 @@ func MySQL(t *testing.T, migrations []string) string { 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") + t.Skip("MYSQL_SERVER_URI is empty and neither Docker nor native installation is available") } } diff --git a/internal/sqltest/local/postgres.go b/internal/sqltest/local/postgres.go index feda4cf7ac..243a7133ab 100644 --- a/internal/sqltest/local/postgres.go +++ b/internal/sqltest/local/postgres.go @@ -16,6 +16,7 @@ import ( "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 @@ -41,8 +42,15 @@ func postgreSQL(t *testing.T, migrations []string, rw bool) string { 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") + t.Skip("POSTGRESQL_SERVER_URI is empty and neither Docker nor native installation is available") } } diff --git a/internal/sqltest/native/enabled.go b/internal/sqltest/native/enabled.go new file mode 100644 index 0000000000..e5e12ccd80 --- /dev/null +++ b/internal/sqltest/native/enabled.go @@ -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 +} diff --git a/internal/sqltest/native/mysql.go b/internal/sqltest/native/mysql.go new file mode 100644 index 0000000000..04960f8a10 --- /dev/null +++ b/internal/sqltest/native/mysql.go @@ -0,0 +1,229 @@ +package native + +import ( + "context" + "database/sql" + "fmt" + "log/slog" + "os" + "os/exec" + "time" + + _ "github.com/go-sql-driver/mysql" + "golang.org/x/sync/singleflight" +) + +var mysqlFlight singleflight.Group +var mysqlURI string + +// StartMySQLServer installs and starts MySQL natively (without Docker). +// This is intended for CI environments like GitHub Actions where Docker may not be available. +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() (interface{}, 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 { + // MySQL is running without password, set one + if err := setMySQLPassword(ctx); err != nil { + slog.Debug("native/mysql", "set-password-error", err) + } + // 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 first (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 { + // Wait for MySQL to be ready + waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + + // Try without password first + if err := waitForMySQL(waitCtx, uriNoPassword, 30*time.Second); err == nil { + if err := setMySQLPassword(ctx); err != nil { + slog.Debug("native/mysql", "set-password-error", err) + return uriNoPassword, nil + } + return uri, nil + } + + // Try with password + if err := waitForMySQL(waitCtx, uri, 5*time.Second); err == nil { + return uri, nil + } + } + } + + // Install MySQL if needed + if _, err := exec.LookPath("mysql"); err != nil { + slog.Info("native/mysql", "status", "installing") + + // Pre-configure MySQL root password + setSelectionsCmd := exec.Command("sudo", "bash", "-c", + `echo "mysql-server mysql-server/root_password password mysecretpassword" | sudo debconf-set-selections && `+ + `echo "mysql-server mysql-server/root_password_again password mysecretpassword" | sudo debconf-set-selections`) + setSelectionsCmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") + if output, err := setSelectionsCmd.CombinedOutput(); err != nil { + slog.Debug("native/mysql", "debconf", string(output)) + } + + // Try to install MySQL server + cmd := exec.Command("sudo", "apt-get", "install", "-y", "-qq", "mysql-server") + cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") + if output, err := cmd.CombinedOutput(); err != nil { + // If apt-get fails (no network), return error + return "", fmt.Errorf("apt-get install mysql-server failed (network may be unavailable): %w\n%s", err, output) + } + } + + // Start MySQL service + slog.Info("native/mysql", "status", "starting service") + if err := startMySQLService(); err != nil { + return "", fmt.Errorf("failed to start MySQL: %w", err) + } + + // Wait for MySQL to be ready with no password first + waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + + // Try without password first (fresh installation) + if err := waitForMySQL(waitCtx, uriNoPassword, 30*time.Second); err == nil { + // Set the password + if err := setMySQLPassword(ctx); err != nil { + slog.Debug("native/mysql", "set-password-error", err) + // Return without password + return uriNoPassword, nil + } + return uri, nil + } + + // Try with password + if err := waitForMySQL(waitCtx, uri, 5*time.Second); err != nil { + return "", fmt.Errorf("timeout waiting for MySQL: %w", err) + } + + return uri, nil +} + +func startMySQLService() error { + // Try systemctl first + cmd := exec.Command("sudo", "systemctl", "start", "mysql") + if err := cmd.Run(); err == nil { + return nil + } + + // Try mysqld + cmd = exec.Command("sudo", "systemctl", "start", "mysqld") + if err := cmd.Run(); err == nil { + return nil + } + + // Try service command + cmd = exec.Command("sudo", "service", "mysql", "start") + if err := cmd.Run(); err == nil { + return nil + } + + cmd = exec.Command("sudo", "service", "mysqld", "start") + if err := cmd.Run(); err == nil { + 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 + _, 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;") + + // Create dinotest database + _, _ = db.ExecContext(ctx, "CREATE DATABASE IF NOT EXISTS dinotest;") + + return nil +} + +func waitForMySQL(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 MySQL (last error: %v)", lastErr) + } + db, err := sql.Open("mysql", uri) + if err != nil { + lastErr = err + slog.Debug("native/mysql", "open-attempt", err) + continue + } + if err := db.PingContext(ctx); err != nil { + lastErr = err + db.Close() + continue + } + db.Close() + return nil + } + } +} diff --git a/internal/sqltest/native/postgres.go b/internal/sqltest/native/postgres.go new file mode 100644 index 0000000000..a0215387bc --- /dev/null +++ b/internal/sqltest/native/postgres.go @@ -0,0 +1,235 @@ +package native + +import ( + "context" + "fmt" + "log/slog" + "os" + "os/exec" + "strings" + "time" + + "github.com/jackc/pgx/v5" + "golang.org/x/sync/singleflight" +) + +var postgresFlight singleflight.Group +var postgresURI string + +// StartPostgreSQLServer installs and starts PostgreSQL natively (without Docker). +// This is intended for CI environments like GitHub Actions where Docker may not be available. +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() (interface{}, 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) { + // Check if PostgreSQL is already running + 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 + } + + // Install PostgreSQL if needed + if _, err := exec.LookPath("psql"); err != nil { + slog.Info("native/postgres", "status", "installing") + // Update package lists and install PostgreSQL + cmd := exec.Command("sudo", "apt-get", "update", "-qq") + cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") + if output, err := cmd.CombinedOutput(); err != nil { + return "", fmt.Errorf("apt-get update failed: %w\n%s", err, output) + } + + cmd = exec.Command("sudo", "apt-get", "install", "-y", "-qq", "postgresql", "postgresql-contrib") + cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") + if output, err := cmd.CombinedOutput(); err != nil { + return "", fmt.Errorf("apt-get install postgresql failed: %w\n%s", err, output) + } + } + + // 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 + } + } +} From 1a2126df8fbcc80839e2c43b827e1e49f87deba0 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 18 Dec 2025 18:09:44 +0000 Subject: [PATCH 2/9] fix: fail tests instead of skipping when database unavailable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change tests to fail rather than skip when databases are not available. This ensures CI properly reports failures when database connectivity issues occur. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/endtoend/endtoend_test.go | 26 ++++++++------------------ internal/sqltest/local/mysql.go | 2 +- internal/sqltest/local/postgres.go | 2 +- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/internal/endtoend/endtoend_test.go b/internal/endtoend/endtoend_test.go index c99b2ea16d..a85f2985a7 100644 --- a/internal/endtoend/endtoend_test.go +++ b/internal/endtoend/endtoend_test.go @@ -179,37 +179,27 @@ func TestReplay(t *testing.T) { "managed-db": { Mutate: func(t *testing.T, path string) func(*config.Config) { return func(c *config.Config) { - // Only add servers that are available - var servers []config.Server - if postgresURI != "" { - servers = append(servers, config.Server{ + // Add all servers - tests will fail if database isn't available + c.Servers = []config.Server{ + { Name: "postgres", Engine: config.EnginePostgreSQL, URI: postgresURI, - }) - } - if mysqlURI != "" { - servers = append(servers, config.Server{ + }, + { Name: "mysql", Engine: config.EngineMySQL, URI: mysqlURI, - }) + }, } - c.Servers = servers for i := range c.SQL { switch c.SQL[i].Engine { case config.EnginePostgreSQL: - if postgresURI == "" { - t.Skipf("PostgreSQL not available") - } c.SQL[i].Database = &config.Database{ Managed: true, } case config.EngineMySQL: - if mysqlURI == "" { - t.Skipf("MySQL not available") - } c.SQL[i].Database = &config.Database{ Managed: true, } @@ -224,8 +214,8 @@ func TestReplay(t *testing.T) { } }, Enabled: func() bool { - // Enable if at least one database is available - return postgresURI != "" || mysqlURI != "" + // Always enabled - tests will fail if databases aren't available + return true }, }, } diff --git a/internal/sqltest/local/mysql.go b/internal/sqltest/local/mysql.go index 05733f6e8b..7d0cf935e8 100644 --- a/internal/sqltest/local/mysql.go +++ b/internal/sqltest/local/mysql.go @@ -40,7 +40,7 @@ func MySQL(t *testing.T, migrations []string) string { } dburi = u } else { - t.Skip("MYSQL_SERVER_URI is empty and neither Docker nor native installation is available") + t.Fatal("MYSQL_SERVER_URI is empty and neither Docker nor native installation is available") } } diff --git a/internal/sqltest/local/postgres.go b/internal/sqltest/local/postgres.go index 243a7133ab..f215faa85a 100644 --- a/internal/sqltest/local/postgres.go +++ b/internal/sqltest/local/postgres.go @@ -50,7 +50,7 @@ func postgreSQL(t *testing.T, migrations []string, rw bool) string { } dburi = u } else { - t.Skip("POSTGRESQL_SERVER_URI is empty and neither Docker nor native installation is available") + t.Fatal("POSTGRESQL_SERVER_URI is empty and neither Docker nor native installation is available") } } From e0cd02f0d3bc1dacf3362cf2b609f97ed1e10b36 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 18 Dec 2025 18:17:59 +0000 Subject: [PATCH 3/9] fix: only enable managed-db context when databases are available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update the Enabled check for managed-db context to verify at least one database URI is available before running those tests. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/endtoend/endtoend_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/endtoend/endtoend_test.go b/internal/endtoend/endtoend_test.go index a85f2985a7..cd7072a7a9 100644 --- a/internal/endtoend/endtoend_test.go +++ b/internal/endtoend/endtoend_test.go @@ -214,8 +214,8 @@ func TestReplay(t *testing.T) { } }, Enabled: func() bool { - // Always enabled - tests will fail if databases aren't available - return true + // Enabled if at least one database URI is available + return postgresURI != "" || mysqlURI != "" }, }, } From a354b38cd181ea7eb86cb5014edbd6f3d32ff92c Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 18 Dec 2025 18:25:22 +0000 Subject: [PATCH 4/9] fix: restore skip behavior in local database helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keep t.Skip() instead of t.Fatal() in local postgres.go and mysql.go when database is unavailable. This allows tests to be skipped gracefully when neither Docker nor native installation is available. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/sqltest/local/mysql.go | 2 +- internal/sqltest/local/postgres.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/sqltest/local/mysql.go b/internal/sqltest/local/mysql.go index 7d0cf935e8..05733f6e8b 100644 --- a/internal/sqltest/local/mysql.go +++ b/internal/sqltest/local/mysql.go @@ -40,7 +40,7 @@ func MySQL(t *testing.T, migrations []string) string { } dburi = u } else { - t.Fatal("MYSQL_SERVER_URI is empty and neither Docker nor native installation is available") + t.Skip("MYSQL_SERVER_URI is empty and neither Docker nor native installation is available") } } diff --git a/internal/sqltest/local/postgres.go b/internal/sqltest/local/postgres.go index f215faa85a..243a7133ab 100644 --- a/internal/sqltest/local/postgres.go +++ b/internal/sqltest/local/postgres.go @@ -50,7 +50,7 @@ func postgreSQL(t *testing.T, migrations []string, rw bool) string { } dburi = u } else { - t.Fatal("POSTGRESQL_SERVER_URI is empty and neither Docker nor native installation is available") + t.Skip("POSTGRESQL_SERVER_URI is empty and neither Docker nor native installation is available") } } From 7f2af48e387bbf6645f57abb117fa7ce8cee6bf6 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 18 Dec 2025 18:30:17 +0000 Subject: [PATCH 5/9] fix: add timeout to MySQL apt-get installation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add context timeout to prevent apt-get install from blocking indefinitely when network is unavailable or slow. Uses 60 second timeout for install and 10 second timeout for debconf setup. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/sqltest/native/mysql.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/sqltest/native/mysql.go b/internal/sqltest/native/mysql.go index 04960f8a10..eac2e8e4ec 100644 --- a/internal/sqltest/native/mysql.go +++ b/internal/sqltest/native/mysql.go @@ -96,8 +96,10 @@ func startMySQLServer(ctx context.Context) (string, error) { if _, err := exec.LookPath("mysql"); err != nil { slog.Info("native/mysql", "status", "installing") - // Pre-configure MySQL root password - setSelectionsCmd := exec.Command("sudo", "bash", "-c", + // Pre-configure MySQL root password (with timeout) + debconfCtx, debconfCancel := context.WithTimeout(ctx, 10*time.Second) + defer debconfCancel() + setSelectionsCmd := exec.CommandContext(debconfCtx, "sudo", "bash", "-c", `echo "mysql-server mysql-server/root_password password mysecretpassword" | sudo debconf-set-selections && `+ `echo "mysql-server mysql-server/root_password_again password mysecretpassword" | sudo debconf-set-selections`) setSelectionsCmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") @@ -105,12 +107,14 @@ func startMySQLServer(ctx context.Context) (string, error) { slog.Debug("native/mysql", "debconf", string(output)) } - // Try to install MySQL server - cmd := exec.Command("sudo", "apt-get", "install", "-y", "-qq", "mysql-server") + // Try to install MySQL server (with timeout) + installCtx, installCancel := context.WithTimeout(ctx, 60*time.Second) + defer installCancel() + cmd := exec.CommandContext(installCtx, "sudo", "apt-get", "install", "-y", "-qq", "mysql-server") cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") if output, err := cmd.CombinedOutput(); err != nil { - // If apt-get fails (no network), return error - return "", fmt.Errorf("apt-get install mysql-server failed (network may be unavailable): %w\n%s", err, output) + // If apt-get fails (no network or timeout), return error + return "", fmt.Errorf("apt-get install mysql-server failed (network may be unavailable or timed out): %w\n%s", err, output) } } From 43aa8f96dc63bfb7c4f9ec5120784729cee117ce Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 18 Dec 2025 18:35:56 +0000 Subject: [PATCH 6/9] fix: use Linux timeout command for apt-get installation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the Linux timeout command instead of context timeout for apt-get since exec.CommandContext doesn't properly kill child processes when using sudo. This ensures apt-get is properly terminated after 60 seconds. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/sqltest/native/mysql.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/internal/sqltest/native/mysql.go b/internal/sqltest/native/mysql.go index eac2e8e4ec..14f215472b 100644 --- a/internal/sqltest/native/mysql.go +++ b/internal/sqltest/native/mysql.go @@ -96,10 +96,9 @@ func startMySQLServer(ctx context.Context) (string, error) { if _, err := exec.LookPath("mysql"); err != nil { slog.Info("native/mysql", "status", "installing") - // Pre-configure MySQL root password (with timeout) - debconfCtx, debconfCancel := context.WithTimeout(ctx, 10*time.Second) - defer debconfCancel() - setSelectionsCmd := exec.CommandContext(debconfCtx, "sudo", "bash", "-c", + // Pre-configure MySQL root password (with timeout using Linux timeout command) + setSelectionsCmd := exec.Command("sudo", "timeout", "10", + "bash", "-c", `echo "mysql-server mysql-server/root_password password mysecretpassword" | sudo debconf-set-selections && `+ `echo "mysql-server mysql-server/root_password_again password mysecretpassword" | sudo debconf-set-selections`) setSelectionsCmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") @@ -107,10 +106,8 @@ func startMySQLServer(ctx context.Context) (string, error) { slog.Debug("native/mysql", "debconf", string(output)) } - // Try to install MySQL server (with timeout) - installCtx, installCancel := context.WithTimeout(ctx, 60*time.Second) - defer installCancel() - cmd := exec.CommandContext(installCtx, "sudo", "apt-get", "install", "-y", "-qq", "mysql-server") + // Try to install MySQL server (with 60 second timeout using Linux timeout command) + cmd := exec.Command("sudo", "timeout", "60", "apt-get", "install", "-y", "-qq", "mysql-server") cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") if output, err := cmd.CombinedOutput(); err != nil { // If apt-get fails (no network or timeout), return error From 2b4e7401e7f59fd1e3de8808cddeb185d5af1652 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 18 Dec 2025 18:49:47 +0000 Subject: [PATCH 7/9] refactor: remove apt-get installation from native database support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify native database support to only start existing installations instead of attempting to install databases via apt-get. This makes the code more reliable and appropriate for CI environments where databases should be pre-installed via services directives. Also add CLAUDE.md documentation for manual database installation with HTTP proxy configuration. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/endtoend/CLAUDE.md | 155 ++++++++++++++++++++++++++++ internal/sqltest/native/mysql.go | 119 ++++++++------------- internal/sqltest/native/postgres.go | 24 ++--- 3 files changed, 205 insertions(+), 93 deletions(-) create mode 100644 internal/endtoend/CLAUDE.md diff --git a/internal/endtoend/CLAUDE.md b/internal/endtoend/CLAUDE.md new file mode 100644 index 0000000000..ef761fe448 --- /dev/null +++ b/internal/endtoend/CLAUDE.md @@ -0,0 +1,155 @@ +# End-to-End Tests - Native Database Setup + +This document describes how to set up MySQL and PostgreSQL for running end-to-end tests in environments without Docker, particularly when using an HTTP proxy. + +## Overview + +The end-to-end tests support three methods for connecting to databases: + +1. **Environment Variables**: Set `POSTGRESQL_SERVER_URI` and `MYSQL_SERVER_URI` directly +2. **Docker**: Automatically starts containers via the docker package +3. **Native Installation**: Starts existing database services on Linux + +## Installing Databases with HTTP Proxy + +In environments where DNS doesn't work directly but an HTTP proxy is available (e.g., some CI environments), you need to configure apt to use the proxy before installing packages. + +### Configure apt Proxy + +```bash +# Check if HTTP_PROXY is set +echo $HTTP_PROXY + +# Configure apt to use the proxy +sudo tee /etc/apt/apt.conf.d/99proxy << EOF +Acquire::http::Proxy "$HTTP_PROXY"; +Acquire::https::Proxy "$HTTPS_PROXY"; +EOF + +# Update package lists +sudo apt-get update -qq +``` + +### Install PostgreSQL + +```bash +# Install PostgreSQL +sudo DEBIAN_FRONTEND=noninteractive apt-get install -y postgresql postgresql-contrib + +# Start the service +sudo service postgresql start + +# Set password for postgres user +sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';" + +# Configure pg_hba.conf for password authentication +# Find the hba_file location: +sudo -u postgres psql -t -c "SHOW hba_file;" + +# Add md5 authentication for localhost (add to the beginning of pg_hba.conf): +# host all all 127.0.0.1/32 md5 + +# Reload PostgreSQL +sudo service postgresql reload +``` + +### Install MySQL + +```bash +# Pre-configure MySQL root password +echo "mysql-server mysql-server/root_password password mysecretpassword" | sudo debconf-set-selections +echo "mysql-server mysql-server/root_password_again password mysecretpassword" | sudo debconf-set-selections + +# Install MySQL +sudo DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server + +# Start the service +sudo service mysql start + +# Verify connection +mysql -uroot -pmysecretpassword -e "SELECT 1;" +``` + +## Expected Database Credentials + +The native database support expects the following credentials: + +### PostgreSQL +- **URI**: `postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable` +- **User**: `postgres` +- **Password**: `postgres` +- **Port**: `5432` + +### MySQL +- **URI**: `root:mysecretpassword@tcp(localhost:3306)/mysql?multiStatements=true&parseTime=true` +- **User**: `root` +- **Password**: `mysecretpassword` +- **Port**: `3306` + +## GitHub Actions Setup + +For GitHub Actions, use the services directive instead of manual installation: + +```yaml +services: + postgres: + image: postgres:16 + env: + POSTGRES_PASSWORD: postgres + ports: + - 5432:5432 + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + mysql: + image: mysql:8 + env: + MYSQL_ROOT_PASSWORD: mysecretpassword + ports: + - 3306:3306 + options: >- + --health-cmd "mysqladmin ping" + --health-interval 10s + --health-timeout 5s + --health-retries 5 +``` + +Then set environment variables: +```yaml +env: + POSTGRESQL_SERVER_URI: postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable + MYSQL_SERVER_URI: root:mysecretpassword@tcp(localhost:3306)/mysql?multiStatements=true&parseTime=true +``` + +## Running Tests + +```bash +# Run end-to-end tests +go test -v -run TestReplay -timeout 20m ./internal/endtoend/... + +# With verbose logging +go test -v -run TestReplay -timeout 20m ./internal/endtoend/... 2>&1 | tee test.log +``` + +## Troubleshooting + +### apt-get times out or fails +- Ensure HTTP proxy is configured in `/etc/apt/apt.conf.d/99proxy` +- Check that the proxy URL is correct: `echo $HTTP_PROXY` +- Try running `sudo apt-get update` first to verify connectivity + +### MySQL connection refused +- Check if MySQL is running: `sudo service mysql status` +- Verify the password: `mysql -uroot -pmysecretpassword -e "SELECT 1;"` +- Check if MySQL is listening on TCP: `netstat -tlnp | grep 3306` + +### PostgreSQL authentication failed +- Verify pg_hba.conf has md5 authentication for localhost +- Check password: `PGPASSWORD=postgres psql -h localhost -U postgres -c "SELECT 1;"` +- Reload PostgreSQL after config changes: `sudo service postgresql reload` + +### DNS resolution fails +This is expected in some environments. Configure apt proxy as shown above. diff --git a/internal/sqltest/native/mysql.go b/internal/sqltest/native/mysql.go index 14f215472b..1788262ab2 100644 --- a/internal/sqltest/native/mysql.go +++ b/internal/sqltest/native/mysql.go @@ -5,7 +5,6 @@ import ( "database/sql" "fmt" "log/slog" - "os" "os/exec" "time" @@ -16,8 +15,9 @@ import ( var mysqlFlight singleflight.Group var mysqlURI string -// StartMySQLServer installs and starts MySQL natively (without Docker). -// This is intended for CI environments like GitHub Actions where Docker may not be available. +// StartMySQLServer starts an existing MySQL installation natively (without Docker). +// This is intended for CI environments like GitHub Actions where Docker may not be available +// but MySQL can be installed via the services directive. func StartMySQLServer(ctx context.Context) (string, error) { if err := Supported(); err != nil { return "", err @@ -44,7 +44,7 @@ func StartMySQLServer(ctx context.Context) (string, error) { } func startMySQLServer(ctx context.Context) (string, error) { - // Standard URI for test MySQL + // Standard URI for test MySQL (matches GitHub Actions MySQL service default) uri := "root:mysecretpassword@tcp(localhost:3306)/mysql?multiStatements=true&parseTime=true" // Try to connect first - it might already be running @@ -56,9 +56,12 @@ func startMySQLServer(ctx context.Context) (string, error) { // 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 { - // MySQL is running without password, set one + 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 { @@ -68,103 +71,64 @@ func startMySQLServer(ctx context.Context) (string, error) { return uriNoPassword, nil } - // Try to start existing MySQL service first (might be installed but not running) + // 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 { + 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 without password first - if err := waitForMySQL(waitCtx, uriNoPassword, 30*time.Second); err == nil { + // Try with password first (GitHub Actions MySQL service has password) + 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 } - return uri, nil - } - - // Try with password - if err := waitForMySQL(waitCtx, uri, 5*time.Second); err == nil { - return uri, nil + if err := waitForMySQL(ctx, uri, 1*time.Second); err == nil { + return uri, nil + } + return uriNoPassword, nil } } } - // Install MySQL if needed - if _, err := exec.LookPath("mysql"); err != nil { - slog.Info("native/mysql", "status", "installing") - - // Pre-configure MySQL root password (with timeout using Linux timeout command) - setSelectionsCmd := exec.Command("sudo", "timeout", "10", - "bash", "-c", - `echo "mysql-server mysql-server/root_password password mysecretpassword" | sudo debconf-set-selections && `+ - `echo "mysql-server mysql-server/root_password_again password mysecretpassword" | sudo debconf-set-selections`) - setSelectionsCmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") - if output, err := setSelectionsCmd.CombinedOutput(); err != nil { - slog.Debug("native/mysql", "debconf", string(output)) - } - - // Try to install MySQL server (with 60 second timeout using Linux timeout command) - cmd := exec.Command("sudo", "timeout", "60", "apt-get", "install", "-y", "-qq", "mysql-server") - cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") - if output, err := cmd.CombinedOutput(); err != nil { - // If apt-get fails (no network or timeout), return error - return "", fmt.Errorf("apt-get install mysql-server failed (network may be unavailable or timed out): %w\n%s", err, output) - } - } - - // Start MySQL service - slog.Info("native/mysql", "status", "starting service") - if err := startMySQLService(); err != nil { - return "", fmt.Errorf("failed to start MySQL: %w", err) - } - - // Wait for MySQL to be ready with no password first - waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second) - defer cancel() - - // Try without password first (fresh installation) - if err := waitForMySQL(waitCtx, uriNoPassword, 30*time.Second); err == nil { - // Set the password - if err := setMySQLPassword(ctx); err != nil { - slog.Debug("native/mysql", "set-password-error", err) - // Return without password - return uriNoPassword, nil - } - return uri, nil - } - - // Try with password - if err := waitForMySQL(waitCtx, uri, 5*time.Second); err != nil { - return "", fmt.Errorf("timeout waiting for MySQL: %w", err) - } - - return uri, 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 } @@ -179,28 +143,29 @@ func setMySQLPassword(ctx context.Context) error { } defer db.Close() - // Set root password - _, err = db.ExecContext(ctx, "ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysecretpassword';") + // 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 older MySQL syntax - _, err = db.ExecContext(ctx, "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mysecretpassword');") + // Try without specifying auth plugin + _, err = db.ExecContext(ctx, "ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysecretpassword';") if err != nil { - return fmt.Errorf("could not set MySQL password: %w", err) + // 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;") - // Create dinotest database - _, _ = db.ExecContext(ctx, "CREATE DATABASE IF NOT EXISTS dinotest;") - return nil } func waitForMySQL(ctx context.Context, uri string, timeout time.Duration) error { deadline := time.Now().Add(timeout) - ticker := time.NewTicker(100 * time.Millisecond) + ticker := time.NewTicker(500 * time.Millisecond) defer ticker.Stop() var lastErr error @@ -218,7 +183,11 @@ func waitForMySQL(ctx context.Context, uri string, timeout time.Duration) error slog.Debug("native/mysql", "open-attempt", err) continue } - if err := db.PingContext(ctx); err != nil { + // Use a short timeout for ping to avoid hanging + pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second) + err = db.PingContext(pingCtx) + cancel() + if err != nil { lastErr = err db.Close() continue diff --git a/internal/sqltest/native/postgres.go b/internal/sqltest/native/postgres.go index a0215387bc..99dc0e6b0d 100644 --- a/internal/sqltest/native/postgres.go +++ b/internal/sqltest/native/postgres.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log/slog" - "os" "os/exec" "strings" "time" @@ -16,8 +15,9 @@ import ( var postgresFlight singleflight.Group var postgresURI string -// StartPostgreSQLServer installs and starts PostgreSQL natively (without Docker). -// This is intended for CI environments like GitHub Actions where Docker may not be available. +// StartPostgreSQLServer starts an existing PostgreSQL installation natively (without Docker). +// This is intended for CI environments like GitHub Actions where Docker may not be available +// but PostgreSQL can be installed via the services directive. func StartPostgreSQLServer(ctx context.Context) (string, error) { if err := Supported(); err != nil { return "", err @@ -44,7 +44,7 @@ func StartPostgreSQLServer(ctx context.Context) (string, error) { } func startPostgreSQLServer(ctx context.Context) (string, error) { - // Check if PostgreSQL is already running + // Standard URI for test PostgreSQL uri := "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" // Try to connect first - it might already be running @@ -53,21 +53,9 @@ func startPostgreSQLServer(ctx context.Context) (string, error) { return uri, nil } - // Install PostgreSQL if needed + // Check if PostgreSQL is installed if _, err := exec.LookPath("psql"); err != nil { - slog.Info("native/postgres", "status", "installing") - // Update package lists and install PostgreSQL - cmd := exec.Command("sudo", "apt-get", "update", "-qq") - cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") - if output, err := cmd.CombinedOutput(); err != nil { - return "", fmt.Errorf("apt-get update failed: %w\n%s", err, output) - } - - cmd = exec.Command("sudo", "apt-get", "install", "-y", "-qq", "postgresql", "postgresql-contrib") - cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") - if output, err := cmd.CombinedOutput(); err != nil { - return "", fmt.Errorf("apt-get install postgresql failed: %w\n%s", err, output) - } + return "", fmt.Errorf("PostgreSQL is not installed (psql not found)") } // Start PostgreSQL service From 4d81c76f567f105b33074dde1035ad40f49cdccf Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 18 Dec 2025 18:52:27 +0000 Subject: [PATCH 8/9] chore: remove GitHub Actions references from comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/sqltest/native/mysql.go | 6 ++---- internal/sqltest/native/postgres.go | 2 -- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/internal/sqltest/native/mysql.go b/internal/sqltest/native/mysql.go index 1788262ab2..82881fdfb7 100644 --- a/internal/sqltest/native/mysql.go +++ b/internal/sqltest/native/mysql.go @@ -16,8 +16,6 @@ var mysqlFlight singleflight.Group var mysqlURI string // StartMySQLServer starts an existing MySQL installation natively (without Docker). -// This is intended for CI environments like GitHub Actions where Docker may not be available -// but MySQL can be installed via the services directive. func StartMySQLServer(ctx context.Context) (string, error) { if err := Supported(); err != nil { return "", err @@ -44,7 +42,7 @@ func StartMySQLServer(ctx context.Context) (string, error) { } func startMySQLServer(ctx context.Context) (string, error) { - // Standard URI for test MySQL (matches GitHub Actions MySQL service default) + // 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 @@ -81,7 +79,7 @@ func startMySQLServer(ctx context.Context) (string, error) { waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second) defer cancel() - // Try with password first (GitHub Actions MySQL service has password) + // Try with password first if err := waitForMySQL(waitCtx, uri, 15*time.Second); err == nil { return uri, nil } diff --git a/internal/sqltest/native/postgres.go b/internal/sqltest/native/postgres.go index 99dc0e6b0d..f805a40a1c 100644 --- a/internal/sqltest/native/postgres.go +++ b/internal/sqltest/native/postgres.go @@ -16,8 +16,6 @@ var postgresFlight singleflight.Group var postgresURI string // StartPostgreSQLServer starts an existing PostgreSQL installation natively (without Docker). -// This is intended for CI environments like GitHub Actions where Docker may not be available -// but PostgreSQL can be installed via the services directive. func StartPostgreSQLServer(ctx context.Context) (string, error) { if err := Supported(); err != nil { return "", err From ebe4489575610372b5ed2adca7d6a2ec784e174c Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 18 Dec 2025 19:04:53 +0000 Subject: [PATCH 9/9] chore: remove GitHub Actions section from CLAUDE.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/endtoend/CLAUDE.md | 38 ------------------------------------- 1 file changed, 38 deletions(-) diff --git a/internal/endtoend/CLAUDE.md b/internal/endtoend/CLAUDE.md index ef761fe448..b9c995c9df 100644 --- a/internal/endtoend/CLAUDE.md +++ b/internal/endtoend/CLAUDE.md @@ -86,44 +86,6 @@ The native database support expects the following credentials: - **Password**: `mysecretpassword` - **Port**: `3306` -## GitHub Actions Setup - -For GitHub Actions, use the services directive instead of manual installation: - -```yaml -services: - postgres: - image: postgres:16 - env: - POSTGRES_PASSWORD: postgres - ports: - - 5432:5432 - options: >- - --health-cmd pg_isready - --health-interval 10s - --health-timeout 5s - --health-retries 5 - - mysql: - image: mysql:8 - env: - MYSQL_ROOT_PASSWORD: mysecretpassword - ports: - - 3306:3306 - options: >- - --health-cmd "mysqladmin ping" - --health-interval 10s - --health-timeout 5s - --health-retries 5 -``` - -Then set environment variables: -```yaml -env: - POSTGRESQL_SERVER_URI: postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable - MYSQL_SERVER_URI: root:mysecretpassword@tcp(localhost:3306)/mysql?multiStatements=true&parseTime=true -``` - ## Running Tests ```bash