fix(mysql): route tenant data to physical shard tables
This commit is contained in:
@@ -56,7 +56,7 @@ func TestEnsureIMGroupBusinessTablesUsesSelectedTenantDatabase(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGroupMessageShardTableRejectsUnsafeMonth(t *testing.T) {
|
func TestGroupMessageShardTableRejectsUnsafeMonth(t *testing.T) {
|
||||||
if table, ok := groupMessageShardTable(1, "202609"); !ok || table != "im_group_messages_202609" {
|
if table, ok := groupMessageShardTable(1, "202609"); !ok || table != "im_001_group_messages_202609" {
|
||||||
t.Fatalf("unexpected valid shard: table=%q ok=%v", table, ok)
|
t.Fatalf("unexpected valid shard: table=%q ok=%v", table, ok)
|
||||||
}
|
}
|
||||||
for _, month := range []string{"", "20269", "202613", "2026_09", "202609;DROP TABLE im_users"} {
|
for _, month := range []string{"", "20269", "202613", "2026_09", "202609;DROP TABLE im_users"} {
|
||||||
@@ -73,9 +73,9 @@ func TestTenantPartitionTableNames(t *testing.T) {
|
|||||||
messages string
|
messages string
|
||||||
group string
|
group string
|
||||||
}{
|
}{
|
||||||
{tenantID: 1, friends: "im_friendships", messages: "im_messages", group: "im_group_messages_202609"},
|
{tenantID: 1, friends: "im_001_friendships", messages: "im_001_messages", group: "im_001_group_messages_202609"},
|
||||||
{tenantID: 10, friends: "im_friendships", messages: "im_messages", group: "im_group_messages_202609"},
|
{tenantID: 10, friends: "im_010_friendships", messages: "im_010_messages", group: "im_010_group_messages_202609"},
|
||||||
{tenantID: 1000, friends: "im_friendships", messages: "im_messages", group: "im_group_messages_202609"},
|
{tenantID: 1000, friends: "im_1000_friendships", messages: "im_1000_messages", group: "im_1000_group_messages_202609"},
|
||||||
}
|
}
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
if got := tenantFriendshipsTable(tc.tenantID); got != tc.friends {
|
if got := tenantFriendshipsTable(tc.tenantID); got != tc.friends {
|
||||||
|
|||||||
@@ -9,19 +9,23 @@ import (
|
|||||||
t "github.com/tinode/chat/server/store/types"
|
t "github.com/tinode/chat/server/store/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func tenantFriendshipsTable(t.TenantID) string {
|
func tenantTablePrefix(tenantID t.TenantID) string {
|
||||||
return "im_friendships"
|
return fmt.Sprintf("im_%03d_", tenantID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tenantMessagesTable(t.TenantID) string {
|
func tenantFriendshipsTable(tenantID t.TenantID) string {
|
||||||
return "im_messages"
|
return tenantTablePrefix(tenantID) + "friendships"
|
||||||
}
|
}
|
||||||
|
|
||||||
func tenantGroupMessagesTable(_ t.TenantID, month string) (string, bool) {
|
func tenantMessagesTable(tenantID t.TenantID) string {
|
||||||
|
return tenantTablePrefix(tenantID) + "messages"
|
||||||
|
}
|
||||||
|
|
||||||
|
func tenantGroupMessagesTable(tenantID t.TenantID, month string) (string, bool) {
|
||||||
if !validShardMonth(month) {
|
if !validShardMonth(month) {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("im_group_messages_%s", month), true
|
return fmt.Sprintf("%sgroup_messages_%s", tenantTablePrefix(tenantID), month), true
|
||||||
}
|
}
|
||||||
|
|
||||||
func validShardMonth(month string) bool {
|
func validShardMonth(month string) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user