Skip to content

Commit

Permalink
appengine/go11x/cloudsql: allow override of socket file (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
grayside authored and broady committed Sep 24, 2018
1 parent 83adaf7 commit e2dd98b
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion appengine/go11x/cloudsql/cloudsql.go
Expand Up @@ -44,11 +44,17 @@ func DB() *sql.DB {
connectionName = mustGetenv("CLOUDSQL_CONNECTION_NAME")
user = mustGetenv("CLOUDSQL_USER")
password = os.Getenv("CLOUDSQL_PASSWORD") // NOTE: password may be empty
socket = os.Getenv("CLOUDSQL_SOCKET_PREFIX")
)

// /cloudsql is used on App Engine.
if socket == "" {
socket = "/cloudsql"
}

// MySQL Connection, comment out to use PostgreSQL.
// connection string format: USER:PASSWORD@unix(/cloudsql/)PROJECT_ID:REGION_ID:INSTANCE_ID/[DB_NAME]
dbURI := fmt.Sprintf("%s:%s@unix(/cloudsql/%s)/", user, password, connectionName)
dbURI := fmt.Sprintf("%s:%s@unix(%s/%s)/", user, password, socket, connectionName)
conn, err := sql.Open("mysql", dbURI)

// PostgreSQL Connection, uncomment to use.
Expand Down

0 comments on commit e2dd98b

Please sign in to comment.