forked from sergicanet9/scv-go-tools
/
oracle.go
52 lines (43 loc) · 1.37 KB
/
oracle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package infrastructure
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
_ "github.com/godror/godror"
"github.com/pressly/goose/v3"
)
// ConnectOracleDB opens a connection to the OracleDB and ensures that the db is reachable
func ConnectOracleDB(ctx context.Context, dsn string) (*sql.DB, error) {
// TODO: convert dsn to godror format
arguments := strings.Split(dsn, "@")
arguments2 := strings.Split(arguments[0], ":")
username := strings.Split(arguments2[1], "//")
password := arguments2[2]
connectString := arguments[1]
dsnOracle := fmt.Sprintf(`user="%s" password="%s" connectString="%s"`, username[1], password, connectString)
db, _ := sql.Open("godror", dsnOracle)
return db, pingOracle(ctx, db)
}
func pingOracle(ctx context.Context, db *sql.DB) (err error) {
// wait until db is ready
for start := time.Now(); time.Since(start) < (5 * time.Second); {
err = db.PingContext(ctx)
if err == nil {
break
}
time.Sleep(1 * time.Second)
}
return err
}
// MigrateOracleDB runs all migrations found in the given directory against the db
func MigrateOracleDB(db *sql.DB, migrationsDir string) error {
goose.SetTableName("goose_db_version")
return goose.Up(db, migrationsDir)
}
// OracleRepository struct of a oracle repository
// Needs a specific implementation of the Repository interface for every entity
type OracleRepository struct {
DB *sql.DB
}