Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to use Gorm with Cloud SQL Go Connector #6991

Closed
enocom opened this issue Apr 24, 2024 · 2 comments
Closed

Document how to use Gorm with Cloud SQL Go Connector #6991

enocom opened this issue Apr 24, 2024 · 2 comments
Labels

Comments

@enocom
Copy link

enocom commented Apr 24, 2024

Currently the Gorm docs showcase how to connect to Cloud SQL using the v1 Cloud SQL Proxy. Meanwhile, there is a newer Cloud SQL Go Connector.

This is a request to update the docs to show how to use the Cloud SQL Go Connector with Gorm.

If there's interest, I'm happy to send the PR to do this work. Opening an issue to discuss first.

Here's a full example:

package main

import (
        "fmt"
        "time"

        "cloud.google.com/go/cloudsqlconn"
        "cloud.google.com/go/cloudsqlconn/postgres/pgxv5"
        "gorm.io/driver/postgres"
        "gorm.io/gorm"
)

func main() {
        cleanup, err := pgxv5.RegisterDriver("cloudsql-postgres")
        if err != nil {
                panic(err)
        }
        // cleanup will stop the driver from retrieving ephemeral certificates
        // Don't call cleanup until you're done with your database connections
        defer cleanup()

        dsn := "host=my-project:us-central1:my-instance user=postgres password=postgres dbname=postgres sslmode=disable"

        db, err := gorm.Open(postgres.New(postgres.Config{
                DriverName: "cloudsql-postgres",
                DSN:        dsn,
        }))
        if err != nil {
                panic(err)
        }

        // get the underlying *sql.DB type to verify the connection
        sdb, err := db.DB()
        if err != nil {
                panic(err)
        }

        var t time.Time
        if err := sdb.QueryRow("select now()").Scan(&t); err != nil {
                panic(err)
        }

        fmt.Println(t)
}

cc @jackwotherspoon

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Apr 24, 2024
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant