Skip to content

Commit 249a69d

Browse files
committed
Use PostgreSQL 18 and store binaries in user cache directory
- Upgrade from PostgreSQL 16.8.0 to 18.2.0 with updated SHA256 checksums - Store PostgreSQL binaries in ~/.cache/sqlc/postgresql (via os.UserCacheDir) instead of /opt/sqlc/postgresql, eliminating the need for sudo to create the install directory https://claude.ai/code/session_018koD7tHEa2W5cXbiPPYatB
1 parent f69582d commit 249a69d

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

cmd/sqlc-test-setup/main.go

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import (
1717

1818
const (
1919
// pgVersion is the PostgreSQL version to install.
20-
pgVersion = "16.8.0"
21-
22-
// pgBaseDir is the sqlc-specific directory where PostgreSQL is installed.
23-
pgBaseDir = "/opt/sqlc/postgresql"
20+
pgVersion = "18.2.0"
2421
)
2522

2623
// pgBinary contains the download information for a PostgreSQL binary release.
@@ -33,11 +30,11 @@ type pgBinary struct {
3330
var pgBinaries = map[string]pgBinary{
3431
"linux/amd64": {
3532
URL: "https://github.com/theseus-rs/postgresql-binaries/releases/download/" + pgVersion + "/postgresql-" + pgVersion + "-x86_64-unknown-linux-gnu.tar.gz",
36-
SHA256: "cb6a4c786f463d1af2ec036da7bca0eadc3484dc80ae3819a555f9b9c828ade4",
33+
SHA256: "cc2674e1641aa2a62b478971a22c131a768eb783f313e6a3385888f58a604074",
3734
},
3835
"linux/arm64": {
3936
URL: "https://github.com/theseus-rs/postgresql-binaries/releases/download/" + pgVersion + "/postgresql-" + pgVersion + "-aarch64-unknown-linux-gnu.tar.gz",
40-
SHA256: "bcd8060dd76bac17aeefb6814fe9cd3e132e09a089793243a4ac1f038af693cf",
37+
SHA256: "8b415a11c7a5484e5fbf7a57fca71554d2d1d7acd34faf066606d2fee1261854",
4138
},
4239
}
4340

@@ -109,14 +106,24 @@ func isMySQLVersionOK(versionOutput string) bool {
109106
return false
110107
}
111108

109+
// pgBaseDir returns the sqlc-specific directory where PostgreSQL is installed,
110+
// using the user's cache directory (~/.cache/sqlc/postgresql on Linux).
111+
func pgBaseDir() string {
112+
cacheDir, err := os.UserCacheDir()
113+
if err != nil {
114+
cacheDir = filepath.Join(os.Getenv("HOME"), ".cache")
115+
}
116+
return filepath.Join(cacheDir, "sqlc", "postgresql")
117+
}
118+
112119
// pgBinDir returns the path to the PostgreSQL bin directory.
113120
func pgBinDir() string {
114-
return filepath.Join(pgBaseDir, "bin")
121+
return filepath.Join(pgBaseDir(), "bin")
115122
}
116123

117124
// pgDataDir returns the path to the PostgreSQL data directory.
118125
func pgDataDir() string {
119-
return filepath.Join(pgBaseDir, "data")
126+
return filepath.Join(pgBaseDir(), "data")
120127
}
121128

122129
// pgBin returns the full path to a PostgreSQL binary.
@@ -208,31 +215,18 @@ func installPostgreSQL() error {
208215
}
209216
log.Printf("SHA256 checksum verified: %s", actualHash)
210217

211-
// Create the base directory
212-
if err := os.MkdirAll(pgBaseDir, 0o755); err != nil {
213-
// Try with sudo if permission denied
214-
if os.IsPermission(err) {
215-
if err := run("sudo", "mkdir", "-p", pgBaseDir); err != nil {
216-
return fmt.Errorf("creating %s: %w", pgBaseDir, err)
217-
}
218-
// Make it owned by current user so we don't need sudo later
219-
user := os.Getenv("USER")
220-
if user == "" {
221-
user = "root"
222-
}
223-
if err := run("sudo", "chown", "-R", user+":"+user, pgBaseDir); err != nil {
224-
return fmt.Errorf("chowning %s: %w", pgBaseDir, err)
225-
}
226-
} else {
227-
return fmt.Errorf("creating %s: %w", pgBaseDir, err)
228-
}
218+
baseDir := pgBaseDir()
219+
220+
// Create the base directory in the user cache
221+
if err := os.MkdirAll(baseDir, 0o755); err != nil {
222+
return fmt.Errorf("creating %s: %w", baseDir, err)
229223
}
230224

231225
// Extract the tarball - it contains a top-level directory like
232-
// postgresql-16.8.0-x86_64-unknown-linux-gnu/ with bin/, lib/, share/ inside.
233-
// We strip that top-level directory and extract directly into pgBaseDir.
234-
log.Printf("extracting postgresql to %s", pgBaseDir)
235-
if err := run("tar", "-xzf", tarball, "-C", pgBaseDir, "--strip-components=1"); err != nil {
226+
// postgresql-18.2.0-x86_64-unknown-linux-gnu/ with bin/, lib/, share/ inside.
227+
// We strip that top-level directory and extract directly into the base dir.
228+
log.Printf("extracting postgresql to %s", baseDir)
229+
if err := run("tar", "-xzf", tarball, "-C", baseDir, "--strip-components=1"); err != nil {
236230
return fmt.Errorf("extracting postgresql: %w", err)
237231
}
238232

@@ -395,7 +389,7 @@ func startPostgreSQL() error {
395389
log.Println("--- Starting PostgreSQL ---")
396390

397391
dataDir := pgDataDir()
398-
logFile := filepath.Join(pgBaseDir, "postgresql.log")
392+
logFile := filepath.Join(pgBaseDir(), "postgresql.log")
399393

400394
// Check if already running
401395
if pgIsReady() {

0 commit comments

Comments
 (0)