Cleanup unused code

This commit is contained in:
2025-07-29 10:51:03 +02:00
parent f178a14e24
commit 74cc552c24
7 changed files with 36 additions and 232 deletions
+21 -113
View File
@@ -1,34 +1,16 @@
package database
import (
"fmt"
"io"
"os"
"path/filepath"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
var migrationDone bool
var isTestDb bool
// testDataLoader is a callback function that can be set to load test data
var testDataLoader func(*gorm.DB) error
// migrationCallback is a callback function that can be set to migrate all structures
var migrationCallback func(*gorm.DB) error
// SetTestDataLoader sets the function to load test data
func SetTestDataLoader(loader func(*gorm.DB) error) {
testDataLoader = loader
}
// SetMigrationCallback sets the function to migrate all structures
func SetMigrationCallback(migrator func(*gorm.DB) error) {
migrationCallback = migrator
}
var testdbTempDir string
// copyFile copies a file from src to dst
func copyFile(src, dst string) error {
@@ -48,121 +30,47 @@ func copyFile(src, dst string) error {
return err
}
// LoadCopyOfPredefinedTestData loads predefined test data from a specific file into the provided database
func LoadCopyOfPredefinedTestData(targetDB *gorm.DB, dataFile string) error {
// Check if file exists
if _, err := os.Stat(dataFile); os.IsNotExist(err) {
return fmt.Errorf("predefined test data file not found: %s", dataFile)
}
testpath := filepath.Join(filepath.Dir(dataFile), "tmp_test_data.db")
if _, err := os.Stat(testpath); os.IsExist(err) {
os.Remove(testpath) // delete existing test data file
}
copyFile(dataFile, testpath)
targetDB, err := gorm.Open(sqlite.Open(testpath), &gorm.Config{})
if err != nil {
return fmt.Errorf("failed to open temporary test database: %w", err)
}
DB = targetDB
return nil
}
// The Database for testing is created from the live database whenever needed and stored in the path defined in database.PreparedTestDB
// to use the test database make a temporary copy of it and then open this new copy as testing database
// This allows to have a clean database for each test run without affecting the live database
// However SetupTestDB can still open the live database if required by setting isTestDb to false
// SetupTestDB creates an in-memory SQLite database for testing
// Parameters:
// - opts[0]: isTestDb (bool) - whether to use in-memory SQLite (true) or persistent MariaDB (false)
// - opts[1]: loadTestData (bool) - whether to load predefined test data from file
// - opts[0]: isTestDb (bool) - whether to use precopied SQLite (true) or persistent (Live) MariaDB (false)
func SetupTestDB(opts ...bool) {
isTestDb = true
loadTestData := false
if len(opts) > 0 {
isTestDb = opts[0]
}
if len(opts) > 1 {
loadTestData = opts[1]
}
if DB == nil {
var db *gorm.DB
var err error
if isTestDb {
//*
db, err = gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
testdbTempDir, err := os.MkdirTemp("", "bamort-test-")
if err != nil {
panic("failed to connect to the test database")
panic("failed to create temporary directory: " + err.Error())
}
//*/
targetFile := filepath.Join(testdbTempDir, "test_backup.db")
err = copyFile(PreparedTestDB, targetFile)
if err != nil {
panic("failed to copy prepared test database: " + err.Error())
}
db, err = gorm.Open(sqlite.Open(targetFile), &gorm.Config{})
if err != nil {
panic("failed to connect to the test database: " + err.Error())
}
defer os.RemoveAll(testdbTempDir)
} else {
//* //testing with persistent MariaDB
dsn := os.Getenv("TEST_DB_DSN")
if dsn == "" {
dsn = "bamort:password@tcp(localhost:3306)/bamort_test?charset=utf8mb4&parseTime=True&loc=Local"
}
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
//panic("failed to connect to the live database")
// noch ein versuch mit der lokalen SQLite-Datenbank
testDataPath := filepath.Join("..", "testdata", "test_data.db")
// Check if test_data.db exists, if not try to copy from predefined_test_data.db
if _, err := os.Stat(testDataPath); os.IsNotExist(err) {
predefinedPath := filepath.Join("..", "testdata", "prepared_test_data.db")
if _, err := os.Stat(predefinedPath); err == nil {
// Create directory if it doesn't exist
if mkdirErr := os.MkdirAll(filepath.Dir(testDataPath), 0755); mkdirErr != nil {
panic("failed to create testdata directory: " + mkdirErr.Error())
}
// Copy predefined_test_data.db to test_data.db
if copyErr := copyFile(predefinedPath, testDataPath); copyErr != nil {
panic("failed to copy predefined test data: " + copyErr.Error())
}
}
}
db, err = gorm.Open(sqlite.Open(testDataPath), &gorm.Config{})
}
if err != nil {
db = ConnectDatabase()
if db == nil {
panic("failed to connect to the live database")
}
//*/
migrationDone = true
}
DB = db
}
// If loadTestData is requested and we have an in-memory database
if loadTestData && isTestDb && DB != nil {
// First migrate the structures using callback if available
if migrationCallback != nil {
err := migrationCallback(DB)
if err != nil {
panic("failed to migrate all structures for test data loading: " + err.Error())
}
} else {
// Fallback to basic migration
err := MigrateStructure()
if err != nil {
panic("failed to MigrateStructure for test data loading: " + err.Error())
}
}
// Load test data using maintenance function
// We need to import the maintenance package, but to avoid circular imports,
// we'll create a callback mechanism
if testDataLoader != nil {
err := testDataLoader(DB)
if err != nil {
panic("failed to load test data: " + err.Error())
}
} else if isTestDb {
// If no test data loader is set, we can still use the default test data loading
err := LoadCopyOfPredefinedTestData(DB, "../testdata/prepared_test_data.db")
if err != nil {
panic("failed to load predefined test data: " + err.Error())
}
}
migrationDone = true
}
}
func ResetTestDB() {
if isTestDb {
@@ -170,7 +78,7 @@ func ResetTestDB() {
if err == nil {
sqlDB.Close()
DB = nil
migrationDone = false
os.RemoveAll(testdbTempDir)
}
}
}