-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(sqlite): move migrations logic to sqlite common module
- Loading branch information
Lorenzo Delgado
committed
Nov 2, 2022
1 parent
1c46b61
commit 194f51c
Showing
6 changed files
with
580 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Oops, something went wrong.