package v071 import ( "context" "errors" "fmt" "github.com/jackc/pgx/v5/pgxpool" ) // ScopeContentCounts 数一个知识库在各内容表里的行数(数据库侧事实)。 // // 为什么不能只数 knowledges:文档被删掉、分块与 Wiki/资源还在的库并不为空。 // 只数一张表会把这种目标判成"首次发布的空库",从而让一条本该走基线初始化的 // 路径绕过去;这里用与整库读写完全相同的表清单与过滤条件,保证"空"的定义只有一份。 func ScopeContentCounts(ctx context.Context, pool *pgxpool.Pool, identity ProductionScope) (map[string]int64, error) { if pool == nil || identity.TenantID == 0 || !validSearchIdentifier(identity.KnowledgeBaseID) { return nil, errors.New("invalid scope identity") } counts := make(map[string]int64, len(productionLibraryTables)) for _, table := range productionLibraryTables { var count int64 if err := pool.QueryRow(ctx, fmt.Sprintf(`SELECT count(*) FROM %s WHERE %s`, table, scopeWhere(table)), identity.TenantID, identity.KnowledgeBaseID).Scan(&count); err != nil { return nil, fmt.Errorf("count %s: %w", table, err) } counts[table] = count } return counts, nil } // ScopeContentEmpty 只有每张内容表都是 0 才算空目标,并交回逐表行数作为证据。 // 注意它**不**包含检索索引:索引由分块派生,分块为空而索引非空属于不一致, // 应在执行侧(预备份/基线捕获)报出来,不能在这里被当成"空"。 func ScopeContentEmpty(ctx context.Context, pool *pgxpool.Pool, identity ProductionScope) (bool, map[string]int64, error) { counts, err := ScopeContentCounts(ctx, pool, identity) if err != nil { return false, nil, err } for _, count := range counts { if count != 0 { return false, counts, nil } } return true, counts, nil }