Andrea Maria Piana 9a59d6a459 feat(metrics)_: add centralized metrics
This commit adds support for centralized metrics.
There are two providers as of now, and we haven't quite decided which
one to go for, so for the time being both are supported.
It also introduces a new endpoint InitializeApplication that replaces
OpenAccounts
2024-07-11 10:05:31 +01:00

83 lines
1.7 KiB
Go

package helpers
import (
"database/sql"
"io/ioutil"
"os"
"github.com/status-im/status-go/common/dbsetup"
"github.com/status-im/status-go/multiaccounts"
)
const kdfIterationsNumberForTests = 1
// SetupTestSQLDB creates a temporary sqlite database file, initialises and then returns with a teardown func
func SetupTestSQLDB(dbInit dbsetup.DatabaseInitializer, prefix string) (*sql.DB, func() error, error) {
tmpfile, err := ioutil.TempFile("", prefix)
if err != nil {
return nil, nil, err
}
db, err := dbInit.Initialize(tmpfile.Name(), "password", kdfIterationsNumberForTests)
if err != nil {
return nil, nil, err
}
return db, func() error {
err := db.Close()
if err != nil {
return err
}
return os.Remove(tmpfile.Name())
}, nil
}
func SetupTestMemorySQLDB(dbInit dbsetup.DatabaseInitializer) (*sql.DB, error) {
db, err := dbInit.Initialize(dbsetup.InMemoryPath, "password", kdfIterationsNumberForTests)
if err != nil {
return nil, err
}
return db, nil
}
func SetupTestMemorySQLAccountsDB(dbInit dbsetup.DatabaseInitializer) (*sql.DB, error) {
db, err := multiaccounts.InitializeDB(dbsetup.InMemoryPath)
if err != nil {
return nil, err
}
return db.DB(), nil
}
func ColumnExists(db *sql.DB, tableName string, columnName string) (bool, error) {
rows, err := db.Query("PRAGMA table_info(" + tableName + ")")
if err != nil {
return false, err
}
defer rows.Close()
var cid int
var name string
var dataType string
var notNull bool
var dFLTValue sql.NullString
var pk int
for rows.Next() {
err := rows.Scan(&cid, &name, &dataType, &notNull, &dFLTValue, &pk)
if err != nil {
return false, err
}
if name == columnName {
return true, nil
}
}
if rows.Err() != nil {
return false, rows.Err()
}
return false, nil
}