Files
phishingclub/backend/database/utils.go
2025-08-21 16:14:09 +02:00

24 lines
544 B
Go

package database
import (
"fmt"
"gorm.io/gorm"
)
type Migrater interface {
Migrate(db *gorm.DB) error
}
func UniqueIndexNameAndNullCompanyID(db *gorm.DB, tableName string) error {
// SQLITE / POSTGRES
// ensure name + null company id is unique
idx := fmt.Sprintf("CREATE UNIQUE INDEX IF NOT EXISTS idx_%s_name_null_company_id ON %s (name) WHERE (company_id IS NULL)", tableName, tableName)
res := db.Exec(idx)
if res.Error != nil {
return fmt.Errorf("error creating index: %v on table %s", res.Error, tableName)
}
return nil
}