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

27 lines
448 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!")
return db, nil
}