Skip to content

Commit

Permalink
fix: 1. 解决tdengine存在多个库时,链接一个库,另一个库存在相同名字的表无法创建问题
Browse files Browse the repository at this point in the history
2. 解决系统启动时,检测设备表是否在td存在,存在逻辑错误问题
3. 更新docker compose
  • Loading branch information
willem committed Mar 8, 2024
1 parent f9eb956 commit 279d174
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
9 changes: 9 additions & 0 deletions internal/consts/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ const (
DatabaseTypeMysql = "mysql" // mysql
DatabaseTypePostgresql = "pgsql" // postgresql
)

const (
TdEngineDbNameKey = "tsd.tdengine.dbName"
TdEngineTypeKey = "tsd.tdengine.type"
TdEngineDsnKey = "tsd.tdengine.dsn"
TdEngineMaxIdleConnsKey = "tsd.tdengine.maxIdleConns"
TdEngineMaxOpenConnsKey = "tsd.tdengine.maxOpenConns"
TdEngineDbName = "sagoo_iot"
)
4 changes: 2 additions & 2 deletions internal/logic/product/dev_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func (s *sDevInit) InitDeviceForTd(ctx context.Context) (err error) {
for _, d := range list {

// 检测设备表是否创建TD表的标识
if d.MetadataTable == 1 {
/*if d.MetadataTable == 1 {
continue
}
}*/

pd, err := service.DevProduct().Detail(ctx, d.ProductKey)
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions internal/logic/tdengine/td_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tdengine
import (
"context"
"database/sql"
"sagooiot/internal/consts"
"sagooiot/internal/service"
"sync"
"time"
Expand All @@ -26,13 +27,13 @@ func tdEngineNew() *sTdEngine {
return _instance
}

var dbName = "sagoo_iot"
var dbName = consts.TdEngineDbName

// 初始化函数
func init() {
service.RegisterTdEngine(tdEngineNew())
// 简化配置获取过程
dbName = g.Cfg().MustGet(context.Background(), "tsd.tdengine.dbName", "sagoo_iot").String()
dbName = g.Cfg().MustGet(context.Background(), consts.TdEngineDbNameKey, consts.TdEngineDbName).String()
}

type connections struct {
Expand Down Expand Up @@ -67,8 +68,8 @@ func (s *sTdEngine) GetConn(ctx context.Context, dbName string) (*sql.DB, error)

// 初始化连接映射
func initConnectionMap(ctx context.Context) *connections {
driver := g.Cfg().MustGet(ctx, "tsd.tdengine.type")
dsn := g.Cfg().MustGet(ctx, "tsd.tdengine.dsn")
driver := g.Cfg().MustGet(ctx, consts.TdEngineTypeKey)
dsn := g.Cfg().MustGet(ctx, consts.TdEngineDsnKey)
return &connections{
tdEngineType: driver.String(),
tdDsn: dsn.String(),
Expand All @@ -92,16 +93,17 @@ func createNewConnection(ctx context.Context, dbName string) (*sql.DB, error) {
}

// 配置连接池设置
connection.SetMaxIdleConns(g.Cfg().MustGet(ctx, "tdengine.maxIdleConns").Int())
connection.SetMaxOpenConns(g.Cfg().MustGet(ctx, "tdengine.maxOpenConns").Int())
// 配置连接池设置
connection.SetMaxIdleConns(g.Cfg().MustGet(ctx, consts.TdEngineMaxIdleConnsKey).Int())
connection.SetMaxOpenConns(g.Cfg().MustGet(ctx, consts.TdEngineMaxOpenConnsKey).Int())

connectionMap.ConnectionMap[dbName] = connection
return connection, nil
}

// Time REST连接时区处理
func (s *sTdEngine) Time(v *g.Var) (rs *g.Var) {
driver := g.Cfg().MustGet(context.TODO(), "tdengine.type")
driver := g.Cfg().MustGet(context.TODO(), consts.TdEngineTypeKey)
if driver.String() == "taosRestful" {
if t, err := time.Parse("2006-01-02 15:04:05 +0000 UTC", v.String()); err == nil {
rs = gvar.New(t.Local().Format("2006-01-02 15:04:05"))
Expand Down
4 changes: 2 additions & 2 deletions internal/logic/tdengine/tsl_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (s *sTSLTable) CheckStable(ctx context.Context, stable string) (b bool, err
}

var name string
if err = taos.QueryRow("SELECT stable_name FROM information_schema.ins_stables WHERE stable_name = '?' LIMIT 1", stable).Scan(&name); err != nil {
if err = taos.QueryRow("SELECT stable_name FROM information_schema.ins_stables WHERE db_name = '?' AND stable_name = '?' LIMIT 1", dbName, stable).Scan(&name); err != nil {
return
}
if name == "" {
Expand All @@ -265,7 +265,7 @@ func (s *sTSLTable) CheckTable(ctx context.Context, table string) (b bool, err e
}

var name string
if err = taos.QueryRowContext(ctx, "SELECT table_name FROM information_schema.ins_tables WHERE table_name = '?' LIMIT 1", table).Scan(&name); err != nil {
if err = taos.QueryRowContext(ctx, "SELECT table_name FROM information_schema.ins_tables WHERE db_name = '?' AND table_name = '?' LIMIT 1", dbName, table).Scan(&name); err != nil {
return
}
if name == "" {
Expand Down
Binary file modified manifest/docker-compose/iot-open/sagooiot
Binary file not shown.

0 comments on commit 279d174

Please sign in to comment.