From b8c2feae0bbb6a08a40d8075eb3bdb2f7faea9ff Mon Sep 17 00:00:00 2001 From: pieterlukasse Date: Mon, 20 Jan 2025 21:18:42 +0100 Subject: [PATCH] fix: fix tests and fix temp table name --- models/helper.go | 4 ++-- tests/models_tests/models_test.go | 6 +++--- utils/parsing.go | 12 ++++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/models/helper.go b/models/helper.go index 4265b0c..56c33fc 100644 --- a/models/helper.go +++ b/models/helper.go @@ -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) @@ -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) diff --git a/tests/models_tests/models_test.go b/tests/models_tests/models_test.go index 76fdce8..90b30c0 100644 --- a/tests/models_tests/models_test.go +++ b/tests/models_tests/models_test.go @@ -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") } diff --git a/utils/parsing.go b/utils/parsing.go index aa7469a..d2bff65 100644 --- a/utils/parsing.go +++ b/utils/parsing.go @@ -8,6 +8,8 @@ import ( "fmt" "log" "strconv" + "sync" + "time" "github.com/gin-gonic/gin" ) @@ -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()) +}