fix: stabilize im server startup and p2p topic scans
This commit is contained in:
@@ -996,6 +996,10 @@ func (a *adapter) UpgradeDb() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := a.ensureMessageTopicSeqIndex(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if a.version == adpVersion {
|
||||
return nil
|
||||
}
|
||||
@@ -1004,6 +1008,22 @@ func (a *adapter) UpgradeDb() error {
|
||||
strconv.Itoa(a.version) + " to " + strconv.Itoa(adpVersion) + " is not supported")
|
||||
}
|
||||
|
||||
func (a *adapter) ensureMessageTopicSeqIndex() error {
|
||||
const indexName = "messages_topic_seqid"
|
||||
var count int
|
||||
if err := a.db.QueryRow(
|
||||
`SELECT COUNT(*) FROM information_schema.statistics
|
||||
WHERE table_schema=DATABASE() AND table_name='im_messages' AND index_name=?`,
|
||||
indexName).Scan(&count); err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
_, err := a.db.Exec("ALTER TABLE im_messages ADD UNIQUE INDEX messages_topic_seqid(topic, seqid)")
|
||||
return err
|
||||
}
|
||||
|
||||
func createIMNotificationTables(tx *sql.Tx) error {
|
||||
_, err := tx.Exec(
|
||||
`CREATE TABLE im_system_notifications(
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package types
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDefaultAccessScanAllowsNil(t *testing.T) {
|
||||
var da DefaultAccess
|
||||
if err := da.Scan(nil); err != nil {
|
||||
t.Fatalf("Scan(nil) returned error: %v", err)
|
||||
}
|
||||
if da.Auth != ModeNone || da.Anon != ModeNone {
|
||||
t.Fatalf("Scan(nil) changed default access: %#v", da)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultAccessScanJSON(t *testing.T) {
|
||||
for name, input := range map[string]any{
|
||||
"bytes": []byte(`{"Auth":"JRWP","Anon":"N"}`),
|
||||
"string": `{"Auth":"JRWP","Anon":"N"}`,
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
var da DefaultAccess
|
||||
if err := da.Scan(input); err != nil {
|
||||
t.Fatalf("Scan returned error: %v", err)
|
||||
}
|
||||
if da.Auth.String() != "JRWP" || da.Anon.String() != "N" {
|
||||
t.Fatalf("unexpected default access: %#v", da)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -863,7 +863,17 @@ type DefaultAccess struct {
|
||||
// Scan is an implementation of Scanner interface so the value can be read from SQL DBs
|
||||
// It assumes the value is serialized and stored as JSON
|
||||
func (da *DefaultAccess) Scan(val any) error {
|
||||
return json.Unmarshal(val.([]byte), da)
|
||||
if val == nil {
|
||||
return nil
|
||||
}
|
||||
switch v := val.(type) {
|
||||
case []byte:
|
||||
return json.Unmarshal(v, da)
|
||||
case string:
|
||||
return json.Unmarshal([]byte(v), da)
|
||||
default:
|
||||
return errors.New("scan failed: data is not a byte slice or string")
|
||||
}
|
||||
}
|
||||
|
||||
// Value implements sql's driver.Valuer interface.
|
||||
|
||||
@@ -142,12 +142,6 @@
|
||||
}
|
||||
],
|
||||
|
||||
"webrtc": {
|
||||
"enabled": false,
|
||||
"call_establishment_timeout": 30,
|
||||
"ice_servers": []
|
||||
},
|
||||
|
||||
"cluster_config": {
|
||||
"self": "",
|
||||
"nodes": [],
|
||||
|
||||
Reference in New Issue
Block a user