Skip to content

Commit

Permalink
feat(devtools): device infos now returns the amount of sql rows
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Nov 7, 2018
1 parent 6cb6637 commit 0aa2295
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
16 changes: 16 additions & 0 deletions core/node/nodeapi_devtools.go
Expand Up @@ -19,6 +19,7 @@ import (
"berty.tech/core/crypto/keypair"
"berty.tech/core/crypto/sigchain"
"berty.tech/core/entity"
"berty.tech/core/sql"
"berty.tech/core/testrunner"
"github.com/brianvoe/gofakeit"
"github.com/gogo/protobuf/proto"
Expand Down Expand Up @@ -119,6 +120,21 @@ func (n *Node) DeviceInfos(_ context.Context, input *node.Void) (*node.DeviceInf
m.NumGC,
)})

for _, table := range sql.AllTables() {
var count uint32
if err := n.sql.Table(table).Count(&count).Error; err != nil {
output.Infos = append(output.Infos, &node.DeviceInfo{
Key: fmt.Sprintf("sql: %s (error)", table),
Value: err.Error(),
})
} else {
output.Infos = append(output.Infos, &node.DeviceInfo{
Key: fmt.Sprintf("sql: %s", table),
Value: fmt.Sprintf("%d entries", count),
})
}
}

output.Infos = append(output.Infos, &node.DeviceInfo{Key: "Versions", Value: fmt.Sprintf("core=%s (p2p=%d, node=%d)", core.Version, p2p.Version, node.Version)})
output.Infos = append(output.Infos, &node.DeviceInfo{Key: "Platform", Value: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)})
output.Infos = append(output.Infos, &node.DeviceInfo{Key: "CPUs", Value: fmt.Sprintf("%d", runtime.NumCPU())})
Expand Down
22 changes: 22 additions & 0 deletions core/sql/common.go
@@ -0,0 +1,22 @@
package sql

func AllTables() []string {
return []string{
// events
"event",

// entities
"sender_alias",
"device",
"contact",
"conversation",
"conversation_member",
"config",

// association tables
// FIXME: add m2m

// internal
"migrations",
}
}
24 changes: 6 additions & 18 deletions core/sql/gorm.go
Expand Up @@ -26,15 +26,7 @@ func Migrate(db *gorm.DB) error {
{
ID: "1",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(
p2p.Event{},
entity.Contact{},
entity.Conversation{},
entity.ConversationMember{},
entity.Device{},
entity.Config{},
entity.SenderAlias{},
).Error
return tx.AutoMigrate(append(entity.AllEntities(), p2p.Event{})...).Error
},
Rollback: func(tx *gorm.DB) error {
return DropDatabase(tx)
Expand All @@ -49,13 +41,9 @@ func Migrate(db *gorm.DB) error {
}

func DropDatabase(db *gorm.DB) error {
return db.DropTableIfExists(
// base entities
"config", "contact", "event", "device", "conversation", "conversation_member",

// association tables

// internal
"migrations",
).Error
var tables []interface{}
for _, table := range AllTables() {
tables = append(tables, table)
}
return db.DropTableIfExists(tables...).Error
}

0 comments on commit 0aa2295

Please sign in to comment.