(dbUrl string, conf *gorm.Config)
| 121 | } |
| 122 | |
| 123 | func MakeDbConnection(dbUrl string, conf *gorm.Config) (*gorm.DB, error) { |
| 124 | u, err := url.Parse(dbUrl) |
| 125 | if err != nil { |
| 126 | return nil, err |
| 127 | } |
| 128 | switch strings.ToLower(u.Scheme) { |
| 129 | case "mysql": |
| 130 | dbUrl = fmt.Sprintf("%s@tcp(%s)%s?%s", getUserString(u), u.Host, u.Path, sanitizeQuery(u.Query())) |
| 131 | if u.Query().Get("tls") != "" && u.Query().Get("ca-cert") != "" { |
| 132 | rootCertPool := x509.NewCertPool() |
| 133 | pem, err := os.ReadFile(u.Query().Get("ca-cert")) |
| 134 | if err != nil { |
| 135 | return nil, err |
| 136 | } |
| 137 | if ok := rootCertPool.AppendCertsFromPEM(pem); !ok { |
| 138 | return nil, err |
| 139 | } |
| 140 | err = tlsMysql.RegisterTLSConfig("custom", &tls.Config{RootCAs: rootCertPool}) |
| 141 | if err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | db, err := sql.Open("mysql", dbUrl) |
| 145 | if err != nil { |
| 146 | return nil, err |
| 147 | } |
| 148 | gormDB, err := gorm.Open(mysql.New(mysql.Config{ |
| 149 | Conn: db, |
| 150 | }), &gorm.Config{}) |
| 151 | |
| 152 | return gormDB, err |
| 153 | } |
| 154 | return gorm.Open(mysql.Open(dbUrl), conf) |
| 155 | case "postgresql", "postgres", "pg": |
| 156 | return gorm.Open(postgres.Open(dbUrl), conf) |
| 157 | default: |
| 158 | return nil, fmt.Errorf("invalid DB_URL:%s", dbUrl) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | func CheckDbConnection(dbUrl string, d time.Duration) errors.Error { |
| 163 | ctx := context.Background() |
no test coverage detected