refactor(wakunode2): Split wankunode2 SQL migrations setup to its own file

This commit is contained in:
Lorenzo Delgado 2022-07-19 11:19:18 +02:00 committed by Lorenzo Delgado
parent e16747137e
commit 10c237dc5f
3 changed files with 32 additions and 43 deletions

View File

@ -1,46 +1,5 @@
{.push raises: [Defect].}
## Collection of utilities commonly used
## during the setup phase of a Waku v2 node
import stew/results
import
std/tables,
chronos,
chronicles,
metrics,
metrics/chronos_httpserver,
stew/results,
stew/shims/net,
./storage/sqlite,
./storage/migration/migration_types,
./config,
./wakunode2
logScope:
topics = "wakunode.setup"
type
SetupResult*[T] = Result[T, string]
##########################
# Setup helper functions #
##########################
proc runMigrations*(sqliteDatabase: SqliteDatabase, conf: WakuNodeConf) =
# Run migration scripts on persistent storage
var migrationPath: string
if conf.persistPeers and conf.persistMessages:
migrationPath = migration_types.ALL_STORE_MIGRATION_PATH
elif conf.persistPeers:
migrationPath = migration_types.PEER_STORE_MIGRATION_PATH
elif conf.persistMessages:
migrationPath = migration_types.MESSAGE_STORE_MIGRATION_PATH
# run migration
info "running migration ...", migrationPath=migrationPath
let migrationResult = sqliteDatabase.migrate(migrationPath)
if migrationResult.isErr:
warn "migration failed", error=migrationResult.error
else:
info "migration is done"
type SetupResult*[T] = Result[T, string]

View File

@ -794,6 +794,7 @@ when isMainModule:
./wakunode2_setup_rest,
./wakunode2_setup_metrics,
./wakunode2_setup_rpc,
./wakunode2_setup_sql_migrations,
./storage/message/waku_message_store,
./storage/peer/waku_peer_storage

View File

@ -0,0 +1,29 @@
{.push raises: [Defect].}
import
chronicles,
./storage/sqlite,
./storage/migration/migration_types,
./config,
./wakunode2
logScope:
topics = "wakunode.setup.migrations"
proc runMigrations*(sqliteDatabase: SqliteDatabase, conf: WakuNodeConf) =
# Run migration scripts on persistent storage
var migrationPath: string
if conf.persistPeers and conf.persistMessages:
migrationPath = migration_types.ALL_STORE_MIGRATION_PATH
elif conf.persistPeers:
migrationPath = migration_types.PEER_STORE_MIGRATION_PATH
elif conf.persistMessages:
migrationPath = migration_types.MESSAGE_STORE_MIGRATION_PATH
info "running migration ...", migrationPath=migrationPath
let migrationResult = sqliteDatabase.migrate(migrationPath)
if migrationResult.isErr:
warn "migration failed", error=migrationResult.error
else:
info "migration is done"