migrate icon indicating copy to clipboard operation
migrate copied to clipboard

Can't make GoMigrate link CloudSQL PostgreSQL Instance

Open miguelpragier opened this issue 3 years ago • 2 comments

Describe the Bug Can't make GoMigrate link CloudSQL PostgreSQL Instance, because the CloudSQL domain/URL format.

Steps to Reproduce

   m, err := migrate.New(
        "migrate-files-path",
        "postgres://localhost:5432/database?sslmode=enable")
)
m.Up()

Expected Behavior Resolve DNS, connect and execute

Migrate Version e.g. v3.4.0

Loaded Database Drivers postgresql

Go Version e.g. go version go1.17 linux/amd64

Stacktrace Full Managed CloudRun running Debian Buster Slim image

Additional context If I put the public IP, CloudRun simply aborts the running instance, without any log. The finished URL has this format: PostgreSQL://user:password@cloudsql/PROJECT-ID:REGION:INSTANCE-ID If I try to use the URL format A -Without urlencode, results in a weird address B - With Urlencode, refuses connection with syntax error.

Did anyone managed to connect go-migrate on GCP cloudSQL ?

miguelpragier avatar Jan 10 '22 17:01 miguelpragier

Any fix @miguelpragier for GORM, im using a custom driver name but dont know how to use it with migrate

Abacaxi-Nelson avatar Jan 15 '23 10:01 Abacaxi-Nelson

If you're using gorm and can connect successfully, you're halfway there. With migrate you can use a raw SQL connection and pass it over as per their docs (https://pkg.go.dev/github.com/golang-migrate/migrate/v4#section-readme).

If this is your gorm connection:

// Construct a CloudSQL based GORM database instance
gormInstance, err = gorm.Open(postgres.New(postgres.Config{
	DriverName: "cloudsqlpostgres",
	DSN:        dsn,
}))

Then go and do this for migrate to take over the connection:

// Acquire raw SQL connection
rawSqlConnection, err := gormInstance.DB()

// Obtain postgres driver
driver, err := postgres.WithInstance(rawSqlConnection, &postgres.Config{})

// Setup migrate with the previously acquired driver
m, err := migrate.NewWithDatabaseInstance("file://database/migrations", "someDatabaseName", driver)

// Migrate up
m.Up()

And you should be ready to go.

quant-eagle avatar Aug 23 '23 00:08 quant-eagle