Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(compiler): resolve global built-in template scope (#19426)

Sbaaoui Idriss3fa257a0dada
diff --git a/internal/dao/compilation_template.go b/internal/dao/compilation_template.go
index 0f5515dc74..48c5818d8b 100644
--- a/internal/dao/compilation_template.go
+++ b/internal/dao/compilation_template.go
@@ -117,7 +117,8 @@ func (dao *CompilationTemplateDAO) ResolveGroupTemplateIDs(ctx context.Context,
func (dao *CompilationTemplateDAO) GetTemplate(ctx context.Context, db *gorm.DB, tenantID, templateID string) (*entity.CompilationTemplate, error) {
var t entity.CompilationTemplate
if err := db.WithContext(ctx).
- Where("id = ? AND tenant_id = ? AND status = ?", templateID, tenantID, string(entity.StatusValid)).
+ Where("id = ? AND status = ? AND (tenant_id = ? OR (tenant_id IS NULL AND (is_builtin = ? OR group_id = ?)))",
+ templateID, string(entity.StatusValid), tenantID, true, BuiltinCompilationTemplateGroupID).
First(&t).Error; err != nil {
return nil, fmt.Errorf("load compilation template %q: %w", templateID, err)
}
diff --git a/internal/dao/compilation_template_seed_test.go b/internal/dao/compilation_template_seed_test.go
index d9c52bc77c..d40f6f878a 100644
--- a/internal/dao/compilation_template_seed_test.go
+++ b/internal/dao/compilation_template_seed_test.go
@@ -141,4 +141,33 @@ func TestSeedBuiltinCompilationTemplatesForTenant(t *testing.T) {
if len(resolved) != len(builtinCompilationTemplateKinds) {
t.Fatalf("resolved %d template ids, want %d", len(resolved), len(builtinCompilationTemplateKinds))
}
+
+ got, err := NewCompilationTemplateDAO().GetTemplate(ctx, db, "some-other-tenant", builtinTemplateID("tree"))
+ if err != nil {
+ t.Fatalf("resolve global built-in template: %v", err)
+ }
+ if got.Kind != "tree" {
+ t.Fatalf("resolved built-in kind = %q, want tree", got.Kind)
+ }
+}
+
+func TestGetTemplateKeepsTenantScopeForCustomTemplates(t *testing.T) {
+ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
+ if err != nil {
+ t.Fatalf("open sqlite: %v", err)
+ }
+ if err = db.AutoMigrate(&entity.CompilationTemplate{}); err != nil {
+ t.Fatalf("migrate: %v", err)
+ }
+ tenantID := "tenant-1"
+ status := string(entity.StatusValid)
+ if err = db.Create(&entity.CompilationTemplate{
+ ID: "custom-template", TenantID: &tenantID, Name: "Custom", Kind: "tree",
+ Config: entity.JSONMap{"kind": "tree"}, Status: &status,
+ }).Error; err != nil {
+ t.Fatalf("insert custom template: %v", err)
+ }
+ if _, err = NewCompilationTemplateDAO().GetTemplate(t.Context(), db, "tenant-2", "custom-template"); err == nil {
+ t.Fatal("custom template resolved for the wrong tenant")
+ }
}