test(database): give each package its own schema when tests run on PostgreSQL (#6594)

With XUI_DB_TYPE=postgres every test package shared one database and worked in public. Go runs package test binaries concurrently, so migrations raced and rows a previous run left behind leaked into the next.

testpg.IsolatePackage creates a schema for the calling package, puts it first on search_path and drops it when the package finishes. It returns at once unless XUI_DB_TYPE is postgres. internal/web/service's TestMain adopts it.
This commit is contained in:
n0ctal
2026-09-27 00:13:42 +05:00
committed by GitHub
parent c54c28d92d
commit 66df77665f
2 changed files with 107 additions and 1 deletions
@@ -2,6 +2,7 @@ package service
import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"
@@ -10,6 +11,7 @@ import (
"github.com/mhsanaei/3x-ui/v3/internal/amneziawgnet"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
xuilogger "github.com/mhsanaei/3x-ui/v3/internal/logger"
"github.com/mhsanaei/3x-ui/v3/internal/testpg"
"github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
@@ -25,7 +27,16 @@ func TestMain(m *testing.M) {
// injectPanelEgress logs when it skips injection; the package logger must
// exist before any test exercises a skipped path.
xuilogger.InitLogger(logging.ERROR)
os.Exit(m.Run())
// Against PostgreSQL every package shares one database; give this one its
// own schema so a parallel package and a previous run cannot reach it.
cleanup, err := testpg.IsolatePackage("internal_web_service")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
code := m.Run()
cleanup()
os.Exit(code)
}
func TestEnsureAPIServices(t *testing.T) {