MCPcopy
hub / github.com/sqlc-dev/sqlc / startMySQLServer

Function startMySQLServer

internal/sqltest/native/mysql.go:44–102  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

42}
43
44func startMySQLServer(ctx context.Context) (string, error) {
45 // Standard URI for test MySQL
46 uri := "root:mysecretpassword@tcp(localhost:3306)/mysql?multiStatements=true&parseTime=true"
47
48 // Try to connect first - it might already be running
49 if err := waitForMySQL(ctx, uri, 500*time.Millisecond); err == nil {
50 slog.Info("native/mysql", "status", "already running")
51 return uri, nil
52 }
53
54 // Also try without password (default MySQL installation)
55 uriNoPassword := "root@tcp(localhost:3306)/mysql?multiStatements=true&parseTime=true"
56 if err := waitForMySQL(ctx, uriNoPassword, 500*time.Millisecond); err == nil {
57 slog.Info("native/mysql", "status", "already running (no password)")
58 // MySQL is running without password, try to set one
59 if err := setMySQLPassword(ctx); err != nil {
60 slog.Debug("native/mysql", "set-password-error", err)
61 // Return without password if we can't set one
62 return uriNoPassword, nil
63 }
64 // Try again with password
65 if err := waitForMySQL(ctx, uri, 1*time.Second); err == nil {
66 return uri, nil
67 }
68 // If password didn't work, use no password
69 return uriNoPassword, nil
70 }
71
72 // Try to start existing MySQL service (might be installed but not running)
73 if _, err := exec.LookPath("mysqld"); err == nil {
74 slog.Info("native/mysql", "status", "starting existing service")
75 if err := startMySQLService(); err != nil {
76 slog.Debug("native/mysql", "start-error", err)
77 } else {
78 // Wait for MySQL to be ready
79 waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
80 defer cancel()
81
82 // Try with password first
83 if err := waitForMySQL(waitCtx, uri, 15*time.Second); err == nil {
84 return uri, nil
85 }
86
87 // Try without password
88 if err := waitForMySQL(waitCtx, uriNoPassword, 15*time.Second); err == nil {
89 if err := setMySQLPassword(ctx); err != nil {
90 slog.Debug("native/mysql", "set-password-error", err)
91 return uriNoPassword, nil
92 }
93 if err := waitForMySQL(ctx, uri, 1*time.Second); err == nil {
94 return uri, nil
95 }
96 return uriNoPassword, nil
97 }
98 }
99 }
100
101 return "", fmt.Errorf("MySQL is not installed or could not be started")

Callers 1

StartMySQLServerFunction · 0.70

Calls 3

setMySQLPasswordFunction · 0.85
startMySQLServiceFunction · 0.85
waitForMySQLFunction · 0.70

Tested by

no test coverage detected