Skip to content

Commit d2d2652

Browse files
committed
Check MySQL version and upgrade to 9 if needed
GitHub Actions runners come with MySQL 8.0 pre-installed. The install step was skipping installation because mysqld already existed, leaving us with MySQL 8.0 which has authentication incompatibilities with the test suite. Now checks the mysqld version string. If it's below 9, stops the old MySQL, removes old packages, clears the data directory, and installs MySQL 9 from Oracle's deb bundle. https://claude.ai/code/session_01CsyRwSkRxBcQoaQFVkMQsJ
1 parent 7ac2e2c commit d2d2652

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

cmd/sqlc-test-setup/main.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ func commandExists(name string) bool {
5757
return err == nil
5858
}
5959

60+
// isMySQLVersionOK checks if the mysqld --version output indicates MySQL 9+.
61+
// Example version string: "/usr/sbin/mysqld Ver 8.0.44-0ubuntu0.24.04.2 ..."
62+
func isMySQLVersionOK(versionOutput string) bool {
63+
// Look for "Ver X.Y.Z" pattern
64+
fields := strings.Fields(versionOutput)
65+
for i, f := range fields {
66+
if strings.EqualFold(f, "Ver") && i+1 < len(fields) {
67+
ver := strings.Split(fields[i+1], ".")
68+
if len(ver) > 0 {
69+
major := strings.TrimLeft(ver[0], "0")
70+
if major == "" {
71+
return false
72+
}
73+
return major[0] >= '9'
74+
}
75+
}
76+
}
77+
return false
78+
}
79+
6080
// ---- install ----
6181

6282
func runInstall() error {
@@ -129,9 +149,26 @@ func installMySQL() error {
129149
if commandExists("mysqld") {
130150
out, err := runOutput("mysqld", "--version")
131151
if err == nil {
132-
log.Printf("mysql is already installed: %s", strings.TrimSpace(out))
133-
log.Println("skipping mysql installation")
134-
return nil
152+
version := strings.TrimSpace(out)
153+
log.Printf("mysql is already installed: %s", version)
154+
if isMySQLVersionOK(version) {
155+
log.Println("mysql version is 9+, skipping installation")
156+
return nil
157+
}
158+
log.Println("mysql version is too old, upgrading to MySQL 9")
159+
// Stop existing MySQL before upgrading
160+
_ = exec.Command("sudo", "service", "mysql", "stop").Run()
161+
_ = exec.Command("sudo", "pkill", "-f", "mysqld").Run()
162+
time.Sleep(2 * time.Second)
163+
// Remove old MySQL packages to avoid conflicts
164+
log.Println("removing old mysql packages")
165+
_ = run("sudo", "apt-get", "remove", "-y", "mysql-server", "mysql-client", "mysql-common",
166+
"mysql-server-core-*", "mysql-client-core-*")
167+
// Clear old data directory so MySQL 9 can initialize fresh
168+
log.Println("clearing old mysql data directory")
169+
_ = run("sudo", "rm", "-rf", "/var/lib/mysql")
170+
_ = run("sudo", "mkdir", "-p", "/var/lib/mysql")
171+
_ = run("sudo", "chown", "mysql:mysql", "/var/lib/mysql")
135172
}
136173
}
137174

0 commit comments

Comments
 (0)