golang-migrate / migrate

Database migrations. CLI and Golang library.
Other
14.64k stars 1.36k forks source link

Can't make GoMigrate link CloudSQL PostgreSQL Instance #685

Open miguelpragier opened 2 years ago

miguelpragier commented 2 years ago

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 ?

Abacaxi-Nelson commented 1 year ago

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

Helmisek commented 11 months ago

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.