diff --git a/database/database.go b/database/database.go index 1d39cfa..1c06759 100644 --- a/database/database.go +++ b/database/database.go @@ -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 } @@ -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 { @@ -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 @@ -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 { @@ -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() @@ -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() } } diff --git a/database/db_functions.go b/database/db_functions.go index 02a3f03..f3fa657 100644 --- a/database/db_functions.go +++ b/database/db_functions.go @@ -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 } @@ -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 } diff --git a/database/db_functions_test.go b/database/db_functions_test.go index 4cddb1a..4168c3c 100644 --- a/database/db_functions_test.go +++ b/database/db_functions_test.go @@ -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) @@ -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) } diff --git a/database/db_test.go b/database/db_test.go index a47166d..9a5410a 100644 --- a/database/db_test.go +++ b/database/db_test.go @@ -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) @@ -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() @@ -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