mirror of
https://github.com/Tencent/WeKnora.git
synced 2026-11-23 15:11:15 +08:00
Why:
PR CI previously left six production-critical paths unverified:
1. .github/workflows/app.yml ran without PostgreSQL/ParadeDB or Redis service
containers and left WEKNORA_*_POSTGRES_DSN / WEKNORA_TEST_REDIS_ADDR unset,
causing all PostgreSQL migration, row-locking, pgvector, catalog, and
multi-replica Redis tests to 100% t.Skip (masking a step-down regression in
TestPostgresMigrationsServeAgentHistory once migrations >000106 were added).
2. app.yml invoked go vet/test without -tags sqlite_fts5, causing SQLite FTS5
retriever tests under internal/application/repository/retriever/sqlite to
100% t.Skip.
3. Desktop build-tag checks only ran ./internal/container, missing
./internal/application/service, ./cmd/desktop, and darwin/windows
cross-compilation of ./internal/localsandbox/{seatbelt,winhost}, while
release-lite.yml remained pinned to GO_VERSION=1.24 (go.mod requires 1.26.0).
4. The standalone client/ Go SDK module was excluded by app.yml (!client/**)
and ignored by cli.yml / cli-e2e.yml / pre-push hooks despite cli/
depending on ../client (masking unformatted files and a Unix-only 0o640
permission assertion on Windows in client/).
5. High-concurrency packages (internal/stream, internal/sandbox,
internal/agent, internal/im) ran without -race, and PR CI had no
zero-external-dependency Lite mode end-to-end smoke test.
What:
- Provision paradedb/paradedb:v0.25.11-pg17 and redis:7.0-alpine services in
app.yml, create isolated disposable test databases, and inject all
PostgreSQL/Redis test DSNs with WEKNORA_REQUIRE_INTEGRATION=1; fix
TestPostgresMigrationsServeAgentHistory to migrate down before 000106 and
back up instead of stepping -1 from HEAD.
- Pass -tags sqlite_fts5 across app.yml and git hooks while keeping an explicit
non-FTS5 run for TestSaveWorksWhenTheFTS5ModuleIsNotCompiledIn.
- Vet, test, and build ./internal/container, ./internal/application/service,
and ./cmd/desktop with -tags desktop,sqlite_fts5; isolate cgo-only pg_query
and gojieba bindings behind !cgo stubs so darwin/arm64 and windows/amd64
cross-compilation of ./internal/localsandbox/... runs on Linux CI; align
release-lite.yml GO_VERSION to 1.26.
- Watch client/** in cli.yml, cli-e2e.yml, and git pre-push hooks, running
client gofmt, build, vet, and -race tests across Linux, macOS, and Windows,
and fix the pre-existing gofmt drift and Windows permission assertion in
client/ uncovered by the new gate.
- Enable -race on internal/{stream,sandbox,agent,im}/... and add
tests/lite_smoke/lite_e2e_test.go exercising a real WeKnora-lite subprocess
end-to-end (auth, models, KB, Markdown upload/indexing, vector+FTS5 search,
SSE RAG chat, message persistence, and SIGTERM shutdown).
115 lines
4.1 KiB
Go
115 lines
4.1 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestPostgresMigrationsServeAgentHistory runs the versioned migrations against
|
|
// a real PostgreSQL. Skipped unless WEKNORA_MIGRATION_TEST_POSTGRES_DSN points
|
|
// at a disposable database, for example:
|
|
//
|
|
// docker run -d --rm -e POSTGRES_PASSWORD=pg -e POSTGRES_DB=weknora -p 55432:5432 \
|
|
// paradedb/paradedb:v0.25.11-pg17
|
|
// WEKNORA_MIGRATION_TEST_POSTGRES_DSN=postgres://postgres:pg@localhost:55432/weknora?sslmode=disable
|
|
//
|
|
// It checks what SQLite cannot: that 000106 builds its index CONCURRENTLY
|
|
// through golang-migrate (which fails inside a transaction block), in both
|
|
// directions, and that the agent history queries walk it without sorting.
|
|
func TestPostgresMigrationsServeAgentHistory(t *testing.T) {
|
|
dsn := os.Getenv("WEKNORA_MIGRATION_TEST_POSTGRES_DSN")
|
|
if dsn == "" {
|
|
t.Skip("set WEKNORA_MIGRATION_TEST_POSTGRES_DSN to run PostgreSQL migration tests")
|
|
}
|
|
root := sqliteRepoRoot(t)
|
|
chdirAndRestore(t, root)
|
|
|
|
require.NoError(t, RunMigrationsWithOptions(dsn, MigrationOptions{}))
|
|
|
|
db, err := sql.Open("postgres", dsn)
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { _ = db.Close() })
|
|
|
|
var version int
|
|
var dirty bool
|
|
require.NoError(t, db.QueryRow("SELECT version, dirty FROM schema_migrations").Scan(&version, &dirty))
|
|
require.Equal(t, latestVersionedMigration(t, root), version)
|
|
require.False(t, dirty)
|
|
requirePostgresIndexValid(t, db)
|
|
|
|
// Down and up again: DROP/CREATE INDEX CONCURRENTLY through golang-migrate.
|
|
m, err := migrate.New("file://migrations/versioned", dsn)
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { _, _ = m.Close() })
|
|
require.NoError(t, ignoreNoChange(m.Migrate(versionedMigrationBefore(t, root, 106))))
|
|
var indexes int
|
|
require.NoError(t, db.QueryRow(
|
|
"SELECT count(*) FROM pg_class WHERE relname = 'idx_messages_session_created_id'").Scan(&indexes))
|
|
require.Zero(t, indexes, "the down migration drops the index")
|
|
require.NoError(t, RunMigrationsWithOptions(dsn, MigrationOptions{}))
|
|
requirePostgresIndexValid(t, db)
|
|
|
|
ctx := context.Background()
|
|
conn, err := db.Conn(ctx)
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { _ = conn.Close() })
|
|
// An empty table is always cheapest to scan; rule the scan out to see
|
|
// which index the planner can use and whether it still has to sort.
|
|
_, err = conn.ExecContext(ctx, "SET enable_seqscan = off")
|
|
require.NoError(t, err)
|
|
for name, query := range map[string]string{
|
|
"backwards page": `SELECT * FROM messages WHERE session_id = 's'
|
|
AND (created_at < now() OR (created_at = now() AND id < 'x'))
|
|
AND deleted_at IS NULL ORDER BY created_at DESC, id DESC LIMIT 200`,
|
|
"newest checkpoint": `SELECT id FROM messages WHERE session_id = 's' AND role = 'assistant'
|
|
AND context_checkpoint IS NOT NULL AND deleted_at IS NULL
|
|
ORDER BY created_at DESC, id DESC LIMIT 1`,
|
|
} {
|
|
rows, err := conn.QueryContext(ctx, "EXPLAIN "+query)
|
|
require.NoError(t, err, name)
|
|
var plan strings.Builder
|
|
for rows.Next() {
|
|
var line string
|
|
require.NoError(t, rows.Scan(&line), name)
|
|
plan.WriteString(line + "\n")
|
|
}
|
|
require.NoError(t, rows.Close(), name)
|
|
require.Contains(t, plan.String(), "idx_messages_session_created_id", "%s plan:\n%s", name, plan.String())
|
|
require.NotContains(t, plan.String(), "Sort", "%s must not sort:\n%s", name, plan.String())
|
|
}
|
|
}
|
|
|
|
func requirePostgresIndexValid(t *testing.T, db *sql.DB) {
|
|
t.Helper()
|
|
var valid bool
|
|
require.NoError(t, db.QueryRow(`SELECT i.indisvalid FROM pg_index i
|
|
JOIN pg_class c ON c.oid = i.indexrelid
|
|
WHERE c.relname = 'idx_messages_session_created_id'`).Scan(&valid))
|
|
require.True(t, valid, "a CONCURRENTLY build that failed leaves an INVALID index")
|
|
}
|
|
|
|
func latestVersionedMigration(t *testing.T, root string) int {
|
|
t.Helper()
|
|
entries, err := os.ReadDir(filepath.Join(root, "migrations", "versioned"))
|
|
require.NoError(t, err)
|
|
latest := 0
|
|
for _, e := range entries {
|
|
prefix, _, ok := strings.Cut(e.Name(), "_")
|
|
if !ok {
|
|
continue
|
|
}
|
|
if n, err := strconv.Atoi(prefix); err == nil && n > latest {
|
|
latest = n
|
|
}
|
|
}
|
|
return latest
|
|
}
|