Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Export database driver (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbampalikis authored Dec 13, 2022
2 parents c958c69 + 79da66d commit 6f2467a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
22 changes: 11 additions & 11 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type DBConf struct {

// SDAdb struct that acts as a receiver for the DB update methods
type SDAdb struct {
db *sql.DB
DB *sql.DB
Version int
Config DBConf
}
Expand Down Expand Up @@ -66,7 +66,7 @@ var SlowConnectRate = 1 * time.Minute
// Currently, only postgresql connections are supported.
func NewSDAdb(config DBConf) (*SDAdb, error) {

dbs := SDAdb{db: nil, Version: -1, Config: config}
dbs := SDAdb{DB: nil, Version: -1, Config: config}

err := dbs.Connect()
if err != nil {
Expand All @@ -88,8 +88,8 @@ func (dbs *SDAdb) Connect() error {
start := time.Now()

// if already connected - do nothing
if dbs.db != nil {
err := dbs.db.Ping()
if dbs.DB != nil {
err := dbs.DB.Ping()
if err == nil {
log.Infoln("Already connected to database")
return nil
Expand All @@ -104,13 +104,13 @@ func (dbs *SDAdb) Connect() error {
dbs.Config.Port, dbs.Config.Database, dbs.Config.User)

for ConnectTimeout <= 0 || ConnectTimeout > time.Since(start) {
dbs.db, err = sql.Open(dbs.Config.PgDataSource())
dbs.DB, err = sql.Open(dbs.Config.PgDataSource())
if err == nil {
log.Infoln("Connected to database")
// Open may just validate its arguments without creating a
// connection to the database. To verify that the data source name
// is valid, call Ping.
err = dbs.db.Ping()
err = dbs.DB.Ping()
return err
}
if time.Since(start) < FastConnectTimeout {
Expand Down Expand Up @@ -161,14 +161,14 @@ func (dbs *SDAdb) getVersion() (int, error) {

var dbVersion = -1

err := dbs.db.QueryRow(query).Scan(&dbVersion)
err := dbs.DB.QueryRow(query).Scan(&dbVersion)
return dbVersion, err
}

// checkAndReconnectIfNeeded validates the current connection with a ping
// and tries to reconnect if necessary
func (dbs *SDAdb) checkAndReconnectIfNeeded() {
err := dbs.db.Ping()
err := dbs.DB.Ping()
if err != nil {
log.Errorf("Database connection problem: %v", err)
dbs.Connect()
Expand All @@ -177,12 +177,12 @@ func (dbs *SDAdb) checkAndReconnectIfNeeded() {

// Close terminates the connection to the database
func (dbs *SDAdb) Close() {
if dbs.db == nil {
if dbs.DB == nil {
return
}
err := dbs.db.Ping()
err := dbs.DB.Ping()
if err == nil {
log.Info("Closing database connection")
dbs.db.Close()
dbs.DB.Close()
}
}
4 changes: 2 additions & 2 deletions database/db_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (dbs *SDAdb) RegisterFile(uploadPath, uploadUser string) (string, error) {

var fileId string

err := dbs.db.QueryRow(query, uploadPath, uploadUser).Scan(&fileId)
err := dbs.DB.QueryRow(query, uploadPath, uploadUser).Scan(&fileId)
return fileId, err
}

Expand All @@ -38,6 +38,6 @@ func (dbs *SDAdb) MarkFileAsUploaded(fileId, userId, message string) error {

query := "INSERT INTO sda.file_event_log(file_id, event, user_id, message) VALUES ($1, 'uploaded', $2, $3)"

_, err := dbs.db.Exec(query, fileId, userId, message)
_, err := dbs.DB.Exec(query, fileId, userId, message)
return err
}
6 changes: 3 additions & 3 deletions database/db_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func (suite *DatabaseTests) TestRegisterFile() {

// check that the file is in the database
exists := false
err = db.db.QueryRow("SELECT EXISTS(SELECT 1 FROM sda.files WHERE id=$1)", fileId).Scan(&exists)
err = db.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM sda.files WHERE id=$1)", fileId).Scan(&exists)
assert.Nil(suite.T(), err, "Failed to check if registered file exists")
assert.True(suite.T(), exists, "RegisterFile() did not insert a row into sda.files with id: "+fileId)

// check that there is a "registered" file event connected to the file
err = db.db.QueryRow("SELECT EXISTS(SELECT 1 FROM sda.file_event_log WHERE file_id=$1 AND event='registered')", fileId).Scan(&exists)
err = db.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM sda.file_event_log WHERE file_id=$1 AND event='registered')", fileId).Scan(&exists)
assert.Nil(suite.T(), err, "Failed to check if registered file event exists")
assert.True(suite.T(), exists, "RegisterFile() did not insert a row into sda.file_event_log with id: "+fileId)

Expand Down Expand Up @@ -64,7 +64,7 @@ func (suite *DatabaseTests) TestMarkFileAsUploaded() {

exists := false
// check that there is an "uploaded" file event connected to the file
err = db.db.QueryRow("SELECT EXISTS(SELECT 1 FROM sda.file_event_log WHERE file_id=$1 AND event='uploaded')", fileId).Scan(&exists)
err = db.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM sda.file_event_log WHERE file_id=$1 AND event='uploaded')", fileId).Scan(&exists)
assert.Nil(suite.T(), err, "Failed to check if uploaded file event exists")
assert.True(suite.T(), exists, "MarkFileAsUploaded() did not insert a row into sda.file_event_log with id: "+fileId)
}
6 changes: 3 additions & 3 deletions database/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (suite *DatabaseTests) TestNewSDAdb() {
func (suite *DatabaseTests) TestConnect() {

// test connecting to a database
db := SDAdb{db: nil, Version: -1, Config: suite.dbConf}
db := SDAdb{DB: nil, Version: -1, Config: suite.dbConf}

err := db.Connect()
assert.Nil(suite.T(), err, "failed connecting: %s", err)
Expand All @@ -90,7 +90,7 @@ func (suite *DatabaseTests) TestConnect() {
db.Close()
query := "SELECT MAX(version) FROM local_ega.dbschema_version"
var dbVersion = -1
err = db.db.QueryRow(query).Scan(&dbVersion)
err = db.DB.QueryRow(query).Scan(&dbVersion)
assert.NotNil(suite.T(), err, "query possible on closed connection")

// test reconnection by using getVersion()
Expand Down Expand Up @@ -130,7 +130,7 @@ func (suite *DatabaseTests) TestClose() {
// check that we can't do queries on a closed connection
query := "SELECT MAX(version) FROM local_ega.dbschema_version"
var dbVersion = -1
err = db.db.QueryRow(query).Scan(&dbVersion)
err = db.DB.QueryRow(query).Scan(&dbVersion)
assert.NotNil(suite.T(), err, "query possible on closed connection")

// check that nothing happens if Close is called on a closed connection
Expand Down

0 comments on commit 6f2467a

Please sign in to comment.