Skip to content

Commit

Permalink
refactor(sqlite): move migrations logic to sqlite common module
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo Delgado committed Nov 2, 2022
1 parent 1c46b61 commit 194f51c
Show file tree
Hide file tree
Showing 6 changed files with 580 additions and 395 deletions.
2 changes: 1 addition & 1 deletion tests/all_tests_v2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import
./v2/test_waku_bridge,
./v2/test_peer_storage,
./v2/test_waku_keepalive,
./v2/test_migration_utils,
./v2/test_sqlite_migrations,
./v2/test_namespacing_utils,
./v2/test_waku_dnsdisc,
./v2/test_waku_discv5,
Expand Down
63 changes: 0 additions & 63 deletions tests/v2/test_migration_utils.nim

This file was deleted.

95 changes: 95 additions & 0 deletions tests/v2/test_sqlite_migrations.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{.used.}

import
std/[strutils, os],
stew/results,
testutils/unittests
import
../../waku/common/sqlite/migrations {.all.}

template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]


suite "SQLite - migrations":

test "filter and order migration script file paths":
## Given
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",
]

let
lowerVersion = 0
highVersion = 7

## When
var migrationSciptPaths: seq[string]
migrationSciptPaths = filterMigrationScripts(paths, lowerVersion, highVersion, direction="up")
migrationSciptPaths = sortMigrationScripts(migrationSciptPaths)

## Then
check:
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"
]

test "break migration scripts into queries":
## Given
let statement1 = """CREATE TABLE contacts1 (
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 statement2 = """CREATE TABLE contacts2 (
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
check:
statements == @[statement1, statement2]

test "break statements script into queries - empty statements":
## Given
let statement1 = """CREATE TABLE contacts1 (
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 statement2 = """CREATE TABLE contacts2 (
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
check:
statements == @[statement1, statement2]
Loading

0 comments on commit 194f51c

Please sign in to comment.