mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:43:03 +08:00 
			
		
		
		
	升级数据库时同时升级edgeClientAgentIPs中的countIPs字段值
This commit is contained in:
		@@ -52,6 +52,7 @@ var recordsTables = []*SQLRecordsTable{
 | 
			
		||||
	{
 | 
			
		||||
		TableName:    "edgeClientAgents",
 | 
			
		||||
		UniqueFields: []string{"code"},
 | 
			
		||||
		ExceptFields: []string{"countIPs"},
 | 
			
		||||
	},
 | 
			
		||||
	{
 | 
			
		||||
		TableName:    "edgeClientAgentIPs",
 | 
			
		||||
 
 | 
			
		||||
@@ -124,6 +124,12 @@ func (this *SQLExecutor) checkData(db *dbs.DB) error {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 更新Agents
 | 
			
		||||
	err = this.checkClientAgents(db)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 更新版本号
 | 
			
		||||
	err = this.updateVersion(db, ComposeSQLVersion())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -471,6 +477,29 @@ func (this *SQLExecutor) checkMetricItems(db *dbs.DB) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 更新Agents表
 | 
			
		||||
func (this *SQLExecutor) checkClientAgents(db *dbs.DB) error {
 | 
			
		||||
	ones, _, err := db.FindOnes("SELECT id FROM edgeClientAgents")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, one := range ones {
 | 
			
		||||
		var agentId = one.GetInt64("id")
 | 
			
		||||
 | 
			
		||||
		countIPs, err := db.FindCol(0, "SELECT COUNT(*) FROM edgeClientAgentIPs WHERE agentId=?", agentId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		_, err = db.Exec("UPDATE edgeClientAgents SET countIPs=? WHERE id=?", countIPs, agentId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 更新版本号
 | 
			
		||||
func (this *SQLExecutor) updateVersion(db *dbs.DB, version string) error {
 | 
			
		||||
	stmt, err := db.Prepare("SELECT COUNT(*) FROM edgeVersions")
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,7 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestSQLExecutor_Run(t *testing.T) {
 | 
			
		||||
	executor := NewSQLExecutor(&dbs.DBConfig{
 | 
			
		||||
	var executor = NewSQLExecutor(&dbs.DBConfig{
 | 
			
		||||
		Driver: "mysql",
 | 
			
		||||
		Prefix: "edge",
 | 
			
		||||
		Dsn:    "root:123456@tcp(127.0.0.1:3306)/db_edge_new?charset=utf8mb4&multiStatements=true",
 | 
			
		||||
@@ -19,7 +19,7 @@ func TestSQLExecutor_Run(t *testing.T) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSQLExecutor_checkCluster(t *testing.T) {
 | 
			
		||||
	executor := NewSQLExecutor(&dbs.DBConfig{
 | 
			
		||||
	var executor = NewSQLExecutor(&dbs.DBConfig{
 | 
			
		||||
		Driver: "mysql",
 | 
			
		||||
		Prefix: "edge",
 | 
			
		||||
		Dsn:    "root:123456@tcp(127.0.0.1:3306)/db_edge_new?charset=utf8mb4&multiStatements=true",
 | 
			
		||||
@@ -40,7 +40,7 @@ func TestSQLExecutor_checkCluster(t *testing.T) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSQLExecutor_checkMetricItems(t *testing.T) {
 | 
			
		||||
	executor := NewSQLExecutor(&dbs.DBConfig{
 | 
			
		||||
	var executor = NewSQLExecutor(&dbs.DBConfig{
 | 
			
		||||
		Driver: "mysql",
 | 
			
		||||
		Prefix: "edge",
 | 
			
		||||
		Dsn:    "root:123456@tcp(127.0.0.1:3306)/db_edge_new?charset=utf8mb4&multiStatements=true",
 | 
			
		||||
@@ -61,7 +61,7 @@ func TestSQLExecutor_checkMetricItems(t *testing.T) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSQLExecutor_checkNS(t *testing.T) {
 | 
			
		||||
	executor := NewSQLExecutor(&dbs.DBConfig{
 | 
			
		||||
	var executor = NewSQLExecutor(&dbs.DBConfig{
 | 
			
		||||
		Driver: "mysql",
 | 
			
		||||
		Prefix: "edge",
 | 
			
		||||
		Dsn:    "root:123456@tcp(127.0.0.1:3306)/db_edge_new?charset=utf8mb4&multiStatements=true",
 | 
			
		||||
@@ -80,3 +80,24 @@ func TestSQLExecutor_checkNS(t *testing.T) {
 | 
			
		||||
	}
 | 
			
		||||
	t.Log("ok")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSQLExecutor_checkClientAgents(t *testing.T) {
 | 
			
		||||
	var executor = NewSQLExecutor(&dbs.DBConfig{
 | 
			
		||||
		Driver: "mysql",
 | 
			
		||||
		Prefix: "edge",
 | 
			
		||||
		Dsn:    "root:123456@tcp(127.0.0.1:3306)/db_edge?charset=utf8mb4&multiStatements=true",
 | 
			
		||||
	})
 | 
			
		||||
	db, err := dbs.NewInstanceFromConfig(executor.dbConfig)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer func() {
 | 
			
		||||
		_ = db.Close()
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	err = executor.checkClientAgents(db)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	t.Log("ok")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user