2022-11-02 15:36:33 +00:00
|
|
|
{.used.}
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import std/[strutils, os], stew/results, testutils/unittests
|
2024-07-05 22:03:38 +00:00
|
|
|
import waku/common/databases/db_sqlite {.all.}, ../waku_archive/archive_utils
|
2022-11-02 15:36:33 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
template sourceDir(): string =
|
|
|
|
currentSourcePath.rsplit(DirSep, 1)[0]
|
2022-11-02 15:36:33 +00:00
|
|
|
|
|
|
|
suite "SQLite - migrations":
|
2022-11-22 18:40:24 +00:00
|
|
|
test "set and get user version":
|
|
|
|
## Given
|
2023-11-27 17:33:27 +00:00
|
|
|
let database = newSqliteDatabase()
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
|
|
|
let setRes = database.setUserVersion(5)
|
|
|
|
let getRes = database.getUserVersion()
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
setRes.isOk()
|
|
|
|
getRes.isOk()
|
|
|
|
|
|
|
|
let version = getRes.tryGet()
|
|
|
|
check:
|
|
|
|
version == 5
|
|
|
|
|
|
|
|
## Cleanup
|
|
|
|
database.close()
|
|
|
|
|
2022-11-02 15:36:33 +00:00
|
|
|
test "filter and order migration script file paths":
|
|
|
|
## Given
|
2024-03-15 23:08:47 +00:00
|
|
|
let paths =
|
|
|
|
@[
|
|
|
|
sourceDir / "00001_valid.up.sql",
|
|
|
|
sourceDir / "00002_alsoValidWithUpperCaseExtension.UP.SQL",
|
|
|
|
sourceDir / "00007_unorderedValid.up.sql",
|
|
|
|
sourceDir / "00003_validRepeated.up.sql",
|
|
|
|
sourceDir / "00003_validRepeated.up.sql",
|
|
|
|
sourceDir / "00666_noMigrationScript.bmp",
|
|
|
|
sourceDir / "00X00_invalidVersion.down.sql",
|
|
|
|
sourceDir / "00008_notWithinVersionRange.up.sql",
|
|
|
|
]
|
2022-11-02 15:36:33 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
lowerVersion = 0
|
|
|
|
highVersion = 7
|
|
|
|
|
|
|
|
## When
|
|
|
|
var migrationSciptPaths: seq[string]
|
2024-03-15 23:08:47 +00:00
|
|
|
migrationSciptPaths =
|
|
|
|
filterMigrationScripts(paths, lowerVersion, highVersion, direction = "up")
|
2022-11-02 15:36:33 +00:00
|
|
|
migrationSciptPaths = sortMigrationScripts(migrationSciptPaths)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
2024-03-15 23:08:47 +00:00
|
|
|
migrationSciptPaths ==
|
|
|
|
@[
|
|
|
|
sourceDir / "00001_valid.up.sql",
|
|
|
|
sourceDir / "00002_alsoValidWithUpperCaseExtension.UP.SQL",
|
|
|
|
sourceDir / "00003_validRepeated.up.sql",
|
|
|
|
sourceDir / "00003_validRepeated.up.sql",
|
|
|
|
sourceDir / "00007_unorderedValid.up.sql",
|
|
|
|
]
|
2022-11-02 15:36:33 +00:00
|
|
|
|
|
|
|
test "break migration scripts into queries":
|
|
|
|
## Given
|
2024-03-15 23:08:47 +00:00
|
|
|
let statement1 =
|
|
|
|
"""CREATE TABLE contacts1 (
|
2022-11-02 15:36:33 +00:00
|
|
|
contact_id INTEGER PRIMARY KEY,
|
|
|
|
first_name TEXT NOT NULL,
|
|
|
|
last_name TEXT NOT NULL,
|
|
|
|
email TEXT NOT NULL UNIQUE,
|
|
|
|
phone TEXT NOT NULL UNIQUE
|
|
|
|
);"""
|
2024-03-15 23:08:47 +00:00
|
|
|
let statement2 =
|
|
|
|
"""CREATE TABLE contacts2 (
|
2022-11-02 15:36:33 +00:00
|
|
|
contact_id INTEGER PRIMARY KEY,
|
|
|
|
first_name TEXT NOT NULL,
|
|
|
|
last_name TEXT NOT NULL,
|
|
|
|
email TEXT NOT NULL UNIQUE,
|
|
|
|
phone TEXT NOT NULL UNIQUE
|
|
|
|
);"""
|
|
|
|
let script = statement1 & statement2
|
|
|
|
|
|
|
|
## When
|
|
|
|
let statements = script.breakIntoStatements()
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2022-11-02 15:36:33 +00:00
|
|
|
## Then
|
|
|
|
check:
|
2024-03-15 23:08:47 +00:00
|
|
|
statements == @[statement1, statement2]
|
2022-11-02 15:36:33 +00:00
|
|
|
|
|
|
|
test "break statements script into queries - empty statements":
|
|
|
|
## Given
|
2024-03-15 23:08:47 +00:00
|
|
|
let statement1 =
|
|
|
|
"""CREATE TABLE contacts1 (
|
2022-11-02 15:36:33 +00:00
|
|
|
contact_id INTEGER PRIMARY KEY,
|
|
|
|
first_name TEXT NOT NULL,
|
|
|
|
last_name TEXT NOT NULL,
|
|
|
|
email TEXT NOT NULL UNIQUE,
|
|
|
|
phone TEXT NOT NULL UNIQUE
|
|
|
|
);"""
|
2024-03-15 23:08:47 +00:00
|
|
|
let statement2 =
|
|
|
|
"""CREATE TABLE contacts2 (
|
2022-11-02 15:36:33 +00:00
|
|
|
contact_id INTEGER PRIMARY KEY,
|
|
|
|
first_name TEXT NOT NULL,
|
|
|
|
last_name TEXT NOT NULL,
|
|
|
|
email TEXT NOT NULL UNIQUE,
|
|
|
|
phone TEXT NOT NULL UNIQUE
|
|
|
|
);"""
|
|
|
|
let script = statement1 & "; ;" & statement2
|
|
|
|
|
|
|
|
## When
|
|
|
|
let statements = script.breakIntoStatements()
|
|
|
|
|
|
|
|
## Then
|
2022-11-22 18:40:24 +00:00
|
|
|
check:
|
2022-11-02 15:36:33 +00:00
|
|
|
statements == @[statement1, statement2]
|