Correct handling of db modes

This commit is contained in:
lash 2025-01-22 12:31:35 +00:00
parent 8530c45074
commit 6749c632b0
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 33 additions and 14 deletions

View File

@ -65,21 +65,16 @@ func setConn() error {
func ApplyConn(override *Override) { func ApplyConn(override *Override) {
if override.DbConn != nil { if override.DbConn != nil {
dbConn = *override.DbConn dbConn = *override.DbConn
dbConnMode = override.DbConnMode
} }
dbConnMode = override.DbConnMode
if override.StateConn != nil { if override.StateConn != nil {
stateDbConn = *override.StateConn stateDbConn = *override.StateConn
stateDbConnMode = override.StateConnMode
} }
if override.ResourceConn != nil { if override.ResourceConn != nil {
resourceDbConn = *override.ResourceConn resourceDbConn = *override.ResourceConn
resourceDbConnMode = override.ResourceConnMode
} }
if override.UserConn != nil { if override.UserConn != nil {
userDbConn = *override.UserConn userDbConn = *override.UserConn
if override.UserConnMode != storage.DBMODE_BINARY {
logg.Warnf("user db non-binary mode ignored")
}
} }
if dbConn == "?" { if dbConn == "?" {
@ -96,7 +91,17 @@ func ApplyConn(override *Override) {
} }
if userDbConn == "?" { if userDbConn == "?" {
userDbConn = dbConn userDbConn = dbConn
//userDbConnMode = dbConnMode userDbConnMode = dbConnMode
}
if override.StateConnMode != storage.DBMODE_ANY {
stateDbConnMode = override.StateConnMode
}
if override.ResourceConnMode != storage.DBMODE_ANY {
resourceDbConnMode = override.ResourceConnMode
}
if override.UserConnMode != storage.DBMODE_ANY {
userDbConnMode = override.UserConnMode
} }
} }

View File

@ -16,7 +16,8 @@ const (
) )
const ( const (
DBMODE_BINARY DbMode = iota DBMODE_ANY DbMode = iota
DBMODE_BINARY
DBMODE_TEXT DBMODE_TEXT
) )
@ -27,6 +28,12 @@ const (
_STORETYPE_MAX _STORETYPE_MAX
) )
var (
DbModeDebug = []string{"ANY", "BIN", "TXT"}
DbTypeDebug = []string{"NONE", "MEM", "FS", "GDBM", "POSTGRES"}
DbStoreDebug = []string{"STATE", "RESOURCE", "USER"}
)
type Conns map[int8]ConnData type Conns map[int8]ConnData
func NewConns() Conns { func NewConns() Conns {
@ -48,8 +55,10 @@ func (c Conns) Have(conn *ConnData) int8 {
if !ok { if !ok {
continue continue
} }
if v.String() == conn.String() { if v.Raw() == conn.Raw() {
return ii if v.Mode() == DBMODE_ANY || v.Mode() == conn.Mode() {
return ii
}
} }
} }
return -1 return -1
@ -66,8 +75,8 @@ func (cd *ConnData) DbType() int {
return cd.typ return cd.typ
} }
func (cd *ConnData) String() string { func (cd ConnData) String() string {
return cd.str return fmt.Sprintf("conn: %s, mod %s, typ %s", cd.str, DbModeDebug[uint8(cd.mode)], DbTypeDebug[uint8(cd.typ)])
} }
func (cd *ConnData) Domain() string { func (cd *ConnData) Domain() string {
@ -83,3 +92,7 @@ func (cd *ConnData) Path() string {
v.RawQuery = "" v.RawQuery = ""
return v.String() return v.String()
} }
func (cd *ConnData) Raw() string {
return cd.str
}

View File

@ -61,6 +61,7 @@ func (ms *MenuStorageService) checkDb(ctx context.Context, typ int8) db.Db {
return store return store
} }
connData := ms.conns[typ] connData := ms.conns[typ]
logg.DebugCtxf(ctx, "db check", "conn", connData, "store", DbStoreDebug[typ])
v := ms.conns.Have(&connData) v := ms.conns.Have(&connData)
if v == -1 { if v == -1 {
return nil return nil
@ -84,7 +85,7 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, section string,
} }
connData := ms.conns[typ] connData := ms.conns[typ]
connStr := connData.String() connStr := connData.Raw()
dbTyp := connData.DbType() dbTyp := connData.DbType()
if dbTyp == DBTYPE_POSTGRES { if dbTyp == DBTYPE_POSTGRES {
// TODO: move to vise // TODO: move to vise
@ -114,7 +115,7 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, section string,
logg.WarnCtxf(ctx, "using volatile storage (memdb)") logg.WarnCtxf(ctx, "using volatile storage (memdb)")
newDb = memdb.NewMemDb() newDb = memdb.NewMemDb()
} else { } else {
return nil, fmt.Errorf("unsupported connection string: '%s'\n", connData.String()) return nil, fmt.Errorf("unsupported connection string: '%s'\n", connData.Raw())
} }
logg.InfoCtxf(ctx, "connecting to db", "conn", connData, "typ", typ) logg.InfoCtxf(ctx, "connecting to db", "conn", connData, "typ", typ)
err = newDb.Connect(ctx, connStr) err = newDb.Connect(ctx, connStr)