fix group tenant topic creation

This commit is contained in:
richjohnrambo
2026-09-23 09:59:53 +08:00
parent 6fa3009987
commit 35a70ce9ff
3 changed files with 20 additions and 7 deletions
+7 -1
View File
@@ -116,7 +116,7 @@ func TestTenantPartitionTableNames(t *testing.T) {
} }
func TestTenantShardSQL(t *testing.T) { func TestTenantShardSQL(t *testing.T) {
query := tenantShardSQL(7, "SELECT * FROM im_topics t JOIN im_group_members gm ON gm.topic=t.name JOIN im_subscriptions s ON s.topic=gm.topic; INSERT INTO im_system_notifications(event_id) VALUES(?)") query := tenantShardSQL(7, "SELECT * FROM im_topics t JOIN im_group_members AS gm FORCE INDEX (`idx_im_group_members_topic_role`) ON gm.topic=t.name JOIN im_subscriptions s ON s.topic=gm.topic; INSERT INTO im_system_notifications(event_id) VALUES(?)")
for _, expected := range []string{"im_007_topics", "im_007_group_members", "im_007_subscriptions", "im_007_system_notifications"} { for _, expected := range []string{"im_007_topics", "im_007_group_members", "im_007_subscriptions", "im_007_system_notifications"} {
if !strings.Contains(query, expected) { if !strings.Contains(query, expected) {
t.Fatalf("tenant shard query is missing %q: %s", expected, query) t.Fatalf("tenant shard query is missing %q: %s", expected, query)
@@ -127,4 +127,10 @@ func TestTenantShardSQL(t *testing.T) {
t.Fatalf("tenant shard query should not reference shared table %q: %s", shared, query) t.Fatalf("tenant shard query should not reference shared table %q: %s", shared, query)
} }
} }
if !strings.Contains(query, "idx_im_group_members_topic_role") {
t.Fatalf("tenant shard query should not rewrite index names: %s", query)
}
if strings.Contains(query, "idx_im_007_group_members_topic_role") {
t.Fatalf("tenant shard query rewrote index name: %s", query)
}
} }
+1 -1
View File
@@ -1235,7 +1235,7 @@ func (a *adapter) TenantGroupCreate(id t.TenantID, topic *t.Topic, owner t.Uid,
return err return err
} }
defer tx.Rollback() defer tx.Rollback()
if err = a.topicCreate(tx, topic); err != nil { if err = tenantTopicCreate(tx, topic); err != nil {
return err return err
} }
ownerSub := &t.Subscription{TenantID: id, ObjHeader: t.ObjHeader{CreatedAt: topic.CreatedAt}, User: owner.String(), ownerSub := &t.Subscription{TenantID: id, ObjHeader: t.ObjHeader{CreatedAt: topic.CreatedAt}, User: owner.String(),
+12 -5
View File
@@ -5,16 +5,23 @@ package mysql
import ( import (
"fmt" "fmt"
"strings" "regexp"
t "github.com/tinode/chat/server/store/types" t "github.com/tinode/chat/server/store/types"
) )
var (
tenantTopicsTableRE = regexp.MustCompile(`\bim_topics\b`)
tenantGroupMembersTableRE = regexp.MustCompile(`\bim_group_members\b`)
tenantSubscriptionsTableRE = regexp.MustCompile(`\bim_subscriptions\b`)
tenantSystemNotificationsTableRE = regexp.MustCompile(`\bim_system_notifications\b`)
)
func tenantShardSQL(tenantID t.TenantID, query string) string { func tenantShardSQL(tenantID t.TenantID, query string) string {
query = strings.ReplaceAll(query, "im_topics", tenantTopicsTable(tenantID)) query = tenantTopicsTableRE.ReplaceAllString(query, tenantTopicsTable(tenantID))
query = strings.ReplaceAll(query, "im_group_members", tenantGroupMembersTable(tenantID)) query = tenantGroupMembersTableRE.ReplaceAllString(query, tenantGroupMembersTable(tenantID))
query = strings.ReplaceAll(query, "im_subscriptions", tenantSubscriptionsTable(tenantID)) query = tenantSubscriptionsTableRE.ReplaceAllString(query, tenantSubscriptionsTable(tenantID))
return strings.ReplaceAll(query, "im_system_notifications", tenantSystemNotificationsTable(tenantID)) return tenantSystemNotificationsTableRE.ReplaceAllString(query, tenantSystemNotificationsTable(tenantID))
} }
func tenantTablePrefix(tenantID t.TenantID) string { func tenantTablePrefix(tenantID t.TenantID) string {