Skip to content

Commit

Permalink
feat: adjust temp table usage to check for table existence first
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterlukasse committed Jan 23, 2025
1 parent 674a0fc commit 0763d91
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 10 additions & 4 deletions models/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func QueryFilterByConceptDefHelper2(query *gorm.DB, sourceId int, filterConceptD
// Transforms the data returned by query into a new temp table.
// Caches the temp table name by using the query definition + transformation method as the key,
// and the temp table name as the value. This allows the method to reuse a temp table if
// one has already been made for this combination.
// Returns the temp table name.
// one has already been made (and is still around) for this combination.
// Returns the temp table name, if found in cache and still exists in DB.
func TransformDataIntoTempTable(omopDataSource *utils.DbAndSchema, query *gorm.DB, filterConceptDef utils.CustomConceptVariableDef) (string, error) {
// Generate a unique hash key based on the query and transformation
querySQL := utils.ToSQL(query)
Expand All @@ -133,8 +133,14 @@ func TransformDataIntoTempTable(omopDataSource *utils.DbAndSchema, query *gorm.D

// Check if the temporary table already exists in the cache
if cachedTableName, exists := utils.TempTableCache.Get(cacheKey); exists {
log.Printf("Reusing cached temp table: %s", cachedTableName)
return cachedTableName.(string), nil
// check if the temporary table is still around:
if utils.TableExists(query, cachedTableName.(string)) {
log.Printf("Reusing cached temp table: %s", cachedTableName)
return cachedTableName.(string), nil
} else {
log.Printf("Temp table NOT available, removing from cache: %s", cachedTableName)
utils.TempTableCache.Delete(cachedTableName.(string))
}
}
// Create a unique temporary table name
tempTableName := fmt.Sprintf("tmp_transformed_%s", utils.GenerateSynchronizedTimestampID())
Expand Down
12 changes: 12 additions & 0 deletions utils/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"context"
"fmt"
"log"
"strings"
"time"
Expand Down Expand Up @@ -97,3 +98,14 @@ func ToSQL(query *gorm.DB) string {
})
return sql
}

func TableExists(tx *gorm.DB, tableName string) bool {
query := fmt.Sprintf("SELECT 1 FROM %s WHERE 1 = 2", tableName)
err := tx.Exec(query).Error
if err != nil {
log.Printf("TableExists check failed for: %s, error: %v", tableName, err)
return false
}
// If the query succeeds, the table exists:
return true
}

0 comments on commit 0763d91

Please sign in to comment.