CI / go-test (push) Canceled after 0s
CI / postgres-durable-first (push) Canceled after 0s
CI / codegen (push) Canceled after 0s
CI / govulncheck (push) Canceled after 0s
CI / race (push) Canceled after 0s
CI / fuzz-smoke (push) Canceled after 0s
CI / golangci (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CodeQL Advanced / Analyze (go) (push) Canceled after 0s
CodeQL Advanced / Analyze (actions) (push) Canceled after 0s
CodeQL Advanced / Analyze (javascript-typescript) (push) Canceled after 0s
Docs CI / build (push) Canceled after 0s
Docs Deploy (GitHub Pages) / build (push) Canceled after 0s
Release 3X-UI / build (386) (push) Canceled after 0s
Release 3X-UI / build (amd64) (push) Canceled after 0s
Release 3X-UI / build (arm64) (push) Canceled after 0s
Release 3X-UI / build (armv5) (push) Canceled after 0s
Release 3X-UI / build (armv6) (push) Canceled after 0s
Release 3X-UI / build (armv7) (push) Canceled after 0s
Release 3X-UI / build (s390x) (push) Canceled after 0s
Release 3X-UI / Build for Windows (amd64) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-latest) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-latest) (push) Canceled after 0s
Docs Deploy (GitHub Pages) / deploy (push) Canceled after 0s
Release 3X-UI / Publish rolling dev release (push) Canceled after 0s
49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package database
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
// The migration runs on every start, so its guard has to actually match the
|
|
// index it created — otherwise every boot re-issues the CREATE.
|
|
func TestMigrateClientEmailLowerIndexIsIdempotent(t *testing.T) {
|
|
if err := InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
|
|
t.Fatalf("InitDB: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = CloseDB() })
|
|
|
|
if !db.Migrator().HasIndex(&model.ClientRecord{}, "idx_clients_email_lower") {
|
|
t.Fatal("idx_clients_email_lower missing after InitDB")
|
|
}
|
|
if err := migrateClientEmailLowerIndex(); err != nil {
|
|
t.Fatalf("second run: %v", err)
|
|
}
|
|
if !db.Migrator().HasIndex(&model.ClientRecord{}, "idx_clients_email_lower") {
|
|
t.Fatal("idx_clients_email_lower vanished after a second run")
|
|
}
|
|
|
|
if IsPostgres() {
|
|
return
|
|
}
|
|
// The identity lookups filter on LOWER(email); without the expression index
|
|
// they seq-scan, which is the cost this migration exists to remove.
|
|
var plan []struct{ Detail string }
|
|
if err := db.Raw("EXPLAIN QUERY PLAN SELECT email FROM clients WHERE LOWER(email) IN ('a')").
|
|
Scan(&plan).Error; err != nil {
|
|
t.Fatalf("explain: %v", err)
|
|
}
|
|
used := false
|
|
for _, row := range plan {
|
|
if strings.Contains(row.Detail, "idx_clients_email_lower") {
|
|
used = true
|
|
}
|
|
}
|
|
if !used {
|
|
t.Errorf("LOWER(email) lookup does not use idx_clients_email_lower: %+v", plan)
|
|
}
|
|
}
|