MCPcopy Index your code
hub / github.com/sqlc-dev/sqlc / installMySQL

Function installMySQL

cmd/sqlc-test-setup/main.go:304–383  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

302}
303
304func installMySQL() error {
305 log.Println("--- Installing MySQL 9 ---")
306
307 if commandExists("mysqld") {
308 out, err := runOutput("mysqld", "--version")
309 if err == nil {
310 version := strings.TrimSpace(out)
311 log.Printf("mysql is already installed: %s", version)
312 if isMySQLVersionOK(version) {
313 log.Println("mysql version is 9+, skipping installation")
314 return nil
315 }
316 log.Println("mysql version is too old, upgrading to MySQL 9")
317 // Stop existing MySQL before upgrading
318 _ = exec.Command("sudo", "service", "mysql", "stop").Run()
319 _ = exec.Command("sudo", "pkill", "-f", "mysqld").Run()
320 time.Sleep(2 * time.Second)
321 // Remove old MySQL packages to avoid conflicts
322 log.Println("removing old mysql packages")
323 _ = run("sudo", "apt-get", "remove", "-y", "mysql-server", "mysql-client", "mysql-common",
324 "mysql-server-core-*", "mysql-client-core-*")
325 // Clear old data directory so MySQL 9 can initialize fresh
326 log.Println("clearing old mysql data directory")
327 _ = run("sudo", "rm", "-rf", "/var/lib/mysql")
328 _ = run("sudo", "mkdir", "-p", "/var/lib/mysql")
329 _ = run("sudo", "chown", "mysql:mysql", "/var/lib/mysql")
330 }
331 }
332
333 bundleURL := "https://dev.mysql.com/get/Downloads/MySQL-9.1/mysql-server_9.1.0-1ubuntu24.04_amd64.deb-bundle.tar"
334 bundleTar := "/tmp/mysql-server-bundle.tar"
335 extractDir := "/tmp/mysql9"
336
337 if _, err := os.Stat(bundleTar); err != nil {
338 log.Printf("downloading MySQL 9 bundle from %s", bundleURL)
339 if err := run("curl", "-L", "-o", bundleTar, bundleURL); err != nil {
340 return fmt.Errorf("downloading mysql bundle: %w", err)
341 }
342 } else {
343 log.Printf("mysql bundle already downloaded at %s, skipping download", bundleTar)
344 }
345
346 log.Printf("extracting bundle to %s", extractDir)
347 if err := os.MkdirAll(extractDir, 0o755); err != nil {
348 return fmt.Errorf("creating extract dir: %w", err)
349 }
350 if err := run("tar", "-xf", bundleTar, "-C", extractDir); err != nil {
351 return fmt.Errorf("extracting mysql bundle: %w", err)
352 }
353
354 // Install packages in dependency order using dpkg.
355 // Some packages may fail due to missing dependencies, which is expected.
356 // We fix them all at the end with apt-get install -f.
357 packages := []string{
358 "mysql-common_*.deb",
359 "mysql-community-client-plugins_*.deb",
360 "mysql-community-client-core_*.deb",
361 "mysql-community-client_*.deb",

Callers 1

runInstallFunction · 0.85

Calls 4

commandExistsFunction · 0.85
runOutputFunction · 0.85
isMySQLVersionOKFunction · 0.85
runFunction · 0.70

Tested by

no test coverage detected