2019-07-25 05:35:09 +00:00
|
|
|
package appdatabase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase/migrations"
|
2022-01-12 20:04:43 +00:00
|
|
|
migrationsprevnodecfg "github.com/status-im/status-go/appdatabase/migrationsprevnodecfg"
|
|
|
|
"github.com/status-im/status-go/nodecfg"
|
2019-07-25 05:35:09 +00:00
|
|
|
"github.com/status-im/status-go/sqlite"
|
|
|
|
)
|
|
|
|
|
2022-01-26 17:18:01 +00:00
|
|
|
const nodeCfgMigrationDate = 1640111208
|
|
|
|
|
2019-07-25 05:35:09 +00:00
|
|
|
// InitializeDB creates db file at a given path and applies migrations.
|
|
|
|
func InitializeDB(path, password string) (*sql.DB, error) {
|
|
|
|
db, err := sqlite.OpenDB(path, password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-12 20:04:43 +00:00
|
|
|
|
2022-01-26 17:18:01 +00:00
|
|
|
// Check if the migration table exists
|
|
|
|
row := db.QueryRow("SELECT exists(SELECT name FROM sqlite_master WHERE type='table' AND name='status_go_schema_migrations')")
|
|
|
|
migrationTableExists := false
|
|
|
|
err = row.Scan(&migrationTableExists)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2022-01-12 20:04:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:18:01 +00:00
|
|
|
var lastMigration uint64 = 0
|
|
|
|
if migrationTableExists {
|
|
|
|
row = db.QueryRow("SELECT version FROM status_go_schema_migrations")
|
|
|
|
err = row.Scan(&lastMigration)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !migrationTableExists || (lastMigration > 0 && lastMigration < nodeCfgMigrationDate) {
|
|
|
|
// If it's the first time migration's being run, or latest migration happened before migrating the nodecfg table
|
|
|
|
err = migrationsprevnodecfg.Migrate(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeConfig migration cannot be done with SQL
|
|
|
|
err = nodecfg.MigrateNodeConfig(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-12 20:04:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 05:35:09 +00:00
|
|
|
err = migrations.Migrate(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
}
|
2021-01-07 11:15:02 +00:00
|
|
|
|
|
|
|
// DecryptDatabase creates an unencrypted copy of the database and copies it
|
|
|
|
// over to the given directory
|
|
|
|
func DecryptDatabase(oldPath, newPath, password string) error {
|
|
|
|
return sqlite.DecryptDB(oldPath, newPath, password)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptDatabase creates an encrypted copy of the database and copies it to the
|
|
|
|
// user path
|
|
|
|
func EncryptDatabase(oldPath, newPath, password string) error {
|
|
|
|
return sqlite.EncryptDB(oldPath, newPath, password)
|
|
|
|
}
|
2021-06-23 09:21:21 +00:00
|
|
|
|
|
|
|
func ChangeDatabasePassword(path, password, newPassword string) error {
|
|
|
|
return sqlite.ChangeEncryptionKey(path, password, newPassword)
|
|
|
|
}
|