Post-Migrator/services/migration/get-db-connection.go

27 lines
459 B
Go
Raw Normal View History

package migration
2024-05-14 21:59:04 +00:00
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
func Connect(user, pass, url, port, dbName string) (*sql.DB, error) {
connectionString := user + ":" + pass + "@tcp(" + url + ":" + port + ")/" + dbName
db, err := sql.Open("mysql", connectionString)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
fmt.Println("Successfully connected to:", dbName)
2024-05-14 21:59:04 +00:00
return db, nil
}