Skip to content

Commit

Permalink
fix: fix tests and fix temp table name
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterlukasse committed Jan 20, 2025
1 parent 802e335 commit b8c2fea
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions models/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TransformDataIntoTempTable(omopDataSource *utils.DbAndSchema, query *gorm.D
return cachedTableName.(string), nil
}
// Create a unique temporary table name
tempTableName := fmt.Sprintf("tmp_transformed_%s", cacheKey[:64]) // Use the first 64 chars of the hash for brevity - a collision will cause the CREATE stament below to fail
tempTableName := fmt.Sprintf("tmp_transformed_%s", utils.GenerateSynchronizedTimestampID())

finalTempTableName := CreateAndFillTempTable(omopDataSource, query, tempTableName, querySQL, filterConceptDef)

Expand Down Expand Up @@ -201,7 +201,7 @@ func TempTableSQLAndFinalName(omopDataSource *utils.DbAndSchema, tempTableName s
finalTempTableName = "#" + tempTableName // Local temp table for MSSQL
tempTableSQL = fmt.Sprintf(
"SELECT %s INTO %s FROM (%s) AS T",
selectStatement, tempTableName, fromSQL,
selectStatement, finalTempTableName, fromSQL,
)
} else {
return "", "", fmt.Errorf("unsupported database type: %s", omopDataSource.Vendor)
Expand Down
6 changes: 3 additions & 3 deletions tests/models_tests/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,16 +915,16 @@ func TestTransformDataIntoTempTable(t *testing.T) {
querySQL := "(SELECT person_id, observation_concept_id, value_as_number FROM " + omopDataSource.Schema + ".observation_continuous) as tmpTest "
query := resultsDataSource.Db.Table(querySQL)

tmpTableName1, _ := models.TransformDataIntoTempTable(query, filterConceptDef)
tmpTableName1, _ := models.TransformDataIntoTempTable(omopDataSource, query, filterConceptDef)
// repeat the exact same query...it should return the same temp table:
tmpTableName2, _ := models.TransformDataIntoTempTable(query, filterConceptDef)
tmpTableName2, _ := models.TransformDataIntoTempTable(omopDataSource, query, filterConceptDef)
if tmpTableName1 != tmpTableName2 {
t.Errorf("tmp table should have been reused")
}
// do a slightly different query...and the temp table should be a different one:
querySQL = "(SELECT person_id, observation_concept_id, value_as_number FROM " + omopDataSource.Schema + ".observation_continuous) as tmpTest2 "
query = resultsDataSource.Db.Table(querySQL)
tmpTableName3, _ := models.TransformDataIntoTempTable(query, filterConceptDef)
tmpTableName3, _ := models.TransformDataIntoTempTable(omopDataSource, query, filterConceptDef)
if tmpTableName1 == tmpTableName3 {
t.Errorf("tmp table should have a new one")
}
Expand Down
12 changes: 12 additions & 0 deletions utils/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"log"
"strconv"
"sync"
"time"

"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -467,3 +469,13 @@ func GenerateHash(input string) string {
hash := sha256.Sum256([]byte(input))
return hex.EncodeToString(hash[:]) // Convert to hexadecimal string
}

var mu sync.Mutex

// A unique ID based on timestamp
func GenerateSynchronizedTimestampID() string {
mu.Lock()
defer mu.Unlock()

return fmt.Sprintf("%x", time.Now().UnixNano())
}

0 comments on commit b8c2fea

Please sign in to comment.