2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2022-11-04 08:40:13 +00:00
|
|
|
|
2024-07-09 11:14:28 +00:00
|
|
|
import std/[tables, strutils, os], results, chronicles
|
2024-03-15 23:08:47 +00:00
|
|
|
import ../../../common/databases/db_sqlite, ../../../common/databases/common
|
2022-11-04 08:40:13 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "waku node peer_manager"
|
|
|
|
|
|
|
|
const SchemaVersion* = 1 # increase this when there is an update in the database schema
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
template projectRoot(): string =
|
|
|
|
currentSourcePath.rsplit(DirSep, 1)[0] / ".." / ".." / ".." / ".." / ".."
|
2022-11-04 08:40:13 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
const PeerStoreMigrationPath: string = projectRoot / "migrations" / "peer_store"
|
2022-11-04 08:40:13 +00:00
|
|
|
|
2023-06-22 09:27:40 +00:00
|
|
|
proc migrate*(db: SqliteDatabase, targetVersion = SchemaVersion): DatabaseResult[void] =
|
2022-11-04 08:40:13 +00:00
|
|
|
## Compares the `user_version` of the sqlite database with the provided `targetVersion`, then
|
|
|
|
## it runs migration scripts if the `user_version` is outdated. The `migrationScriptsDir` path
|
|
|
|
## points to the directory holding the migrations scripts once the db is updated, it sets the
|
|
|
|
## `user_version` to the `tragetVersion`.
|
|
|
|
##
|
|
|
|
## If not `targetVersion` is provided, it defaults to `SchemaVersion`.
|
|
|
|
##
|
|
|
|
## NOTE: Down migration it is not currently supported
|
|
|
|
debug "starting peer store's sqlite database migration"
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let migrationRes =
|
|
|
|
migrate(db, targetVersion, migrationsScriptsDir = PeerStoreMigrationPath)
|
2022-11-04 08:40:13 +00:00
|
|
|
if migrationRes.isErr():
|
|
|
|
return err("failed to execute migration scripts: " & migrationRes.error)
|
|
|
|
|
|
|
|
debug "finished peer store's sqlite database migration"
|
2024-03-15 23:08:47 +00:00
|
|
|
ok()
|