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,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
}
}
}