2023-08-11 11:25:14 +00:00
|
|
|
package dbsetup
|
|
|
|
|
2023-08-10 11:41:04 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2023-08-10 11:41:04 +00:00
|
|
|
)
|
2023-08-11 11:25:14 +00:00
|
|
|
|
2023-10-02 09:28:42 +00:00
|
|
|
const InMemoryPath = ":memory:"
|
2023-08-10 11:41:04 +00:00
|
|
|
|
2023-10-02 09:28:42 +00:00
|
|
|
// The reduced number of kdf iterations (for performance reasons) which is
|
|
|
|
// currently used for derivation of the database key
|
|
|
|
// https://github.com/status-im/status-go/pull/1343
|
|
|
|
// https://notes.status.im/i8Y_l7ccTiOYq09HVgoFwA
|
|
|
|
const ReducedKDFIterationsNumber = 3200
|
2023-08-10 11:41:04 +00:00
|
|
|
|
2023-10-02 09:28:42 +00:00
|
|
|
type DatabaseInitializer interface {
|
|
|
|
Initialize(path, password string, kdfIterationsNumber int) (*sql.DB, error)
|
2023-08-10 11:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDBFilename takes an instance of sql.DB and returns the filename of the "main" database
|
|
|
|
func GetDBFilename(db *sql.DB) (string, error) {
|
|
|
|
if db == nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Warn("GetDBFilename was passed a nil pointer sql.DB")
|
2023-08-10 11:41:04 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var i, category, filename string
|
|
|
|
rows, err := db.Query("PRAGMA database_list;")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
err = rows.Scan(&i, &category, &filename)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "main" database is the one we care about
|
|
|
|
if category == "main" {
|
|
|
|
return filename, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("no main database found")
|
|
|
|
}
|