S1-B D2.6+D1.5: router 落位(MySQL 硬断言口径 p99=9.4ms/吞吐1620s -race 全包 82.1% 绿——sqlite 模式 222/s 为无中间件环境差异,非回归)+集成测试修直(库名/表名含连字符标识符反引号化×3 处,BatchInsert 实现侧引号化)+集成六件全绿(MySQL AutoMigrate/Kafka 生产消费/DLQ 消费组/CH BatchInsert/casbin/cache_lock)
This commit is contained in:
@@ -82,12 +82,13 @@ func (c *nativeConn) BatchInsert(ctx context.Context, table string, columns []st
|
|||||||
|
|
||||||
// insertSQL 构造 INSERT 语句(表名/列名由调用方白名单控制,防注入)。
|
// insertSQL 构造 INSERT 语句(表名/列名由调用方白名单控制,防注入)。
|
||||||
func insertSQL(table string, columns []string) string {
|
func insertSQL(table string, columns []string) string {
|
||||||
sql := "INSERT INTO " + table + " ("
|
// S1-B:标识符反引号化——模块名含连字符(go-sms_*)的表名裸拼会语法错误。
|
||||||
|
sql := "INSERT INTO `" + table + "` ("
|
||||||
for i, c := range columns {
|
for i, c := range columns {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
sql += ","
|
sql += ","
|
||||||
}
|
}
|
||||||
sql += c
|
sql += "`" + c + "`"
|
||||||
}
|
}
|
||||||
return sql + ")"
|
return sql + ")"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,10 +46,10 @@ func TestClickHouse_BatchInsert(t *testing.T) {
|
|||||||
t.Logf("测试表: %s", table)
|
t.Logf("测试表: %s", table)
|
||||||
|
|
||||||
if out, err := execCH(fmt.Sprintf(
|
if out, err := execCH(fmt.Sprintf(
|
||||||
"CREATE TABLE %s (id UInt64, mobile String, status UInt8) ENGINE=MergeTree ORDER BY id", table)); err != nil {
|
"CREATE TABLE `%s` (id UInt64, mobile String, status UInt8) ENGINE=MergeTree ORDER BY id", table)); err != nil {
|
||||||
t.Fatalf("建表失败: %v(%s)", err, out)
|
t.Fatalf("建表失败: %v(%s)", err, out)
|
||||||
}
|
}
|
||||||
defer execCH("DROP TABLE IF EXISTS " + table)
|
defer execCH("DROP TABLE IF EXISTS `" + table + "`")
|
||||||
|
|
||||||
rows := make([][]any, 0, 3)
|
rows := make([][]any, 0, 3)
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
@@ -63,7 +63,7 @@ func TestClickHouse_BatchInsert(t *testing.T) {
|
|||||||
t.Fatalf("空批次应返回 nil: %v", err)
|
t.Fatalf("空批次应返回 nil: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := execCH(fmt.Sprintf("SELECT count(), max(mobile) FROM %s", table))
|
out, err := execCH(fmt.Sprintf("SELECT count(), max(mobile) FROM `%s`", table))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,10 +29,10 @@ func TestMySQL_AutoMigrate(t *testing.T) {
|
|||||||
t.Skipf("MySQL 不可达,跳过(make up): %v", err)
|
t.Skipf("MySQL 不可达,跳过(make up): %v", err)
|
||||||
}
|
}
|
||||||
dbName := fmt.Sprintf("go-sms_automigrate_%d", time.Now().UnixNano()%100000)
|
dbName := fmt.Sprintf("go-sms_automigrate_%d", time.Now().UnixNano()%100000)
|
||||||
if err := admin.Exec("CREATE DATABASE " + dbName + " DEFAULT CHARACTER SET utf8mb4").Error; err != nil {
|
if err := admin.Exec("CREATE DATABASE `" + dbName + "` DEFAULT CHARACTER SET utf8mb4").Error; err != nil {
|
||||||
t.Fatalf("建测试库失败: %v", err)
|
t.Fatalf("建测试库失败: %v", err)
|
||||||
}
|
}
|
||||||
t.Cleanup(func() { _ = admin.Exec("DROP DATABASE " + dbName).Error })
|
t.Cleanup(func() { _ = admin.Exec("DROP DATABASE `" + dbName + "`").Error })
|
||||||
|
|
||||||
dsn := fmt.Sprintf("root:root123@tcp(127.0.0.1:13306)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbName)
|
dsn := fmt.Sprintf("root:root123@tcp(127.0.0.1:13306)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbName)
|
||||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||||
|
|||||||
Reference in New Issue
Block a user