-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add postgres_driver/migrations.nim * Postgres and archive logic adaptation to the migration implementation * libwaku: adapt node_lifecycle_request.nim to migration refactoring * test_app.nim: add more detail for test that only fails in CI * postgres migrations: store the migration scripts inside the resulting wakunode binary instead of external .sql files.
- Loading branch information
1 parent
88ff928
commit 560f949
Showing
15 changed files
with
400 additions
and
253 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 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
20 changes: 20 additions & 0 deletions
20
migrations/message_store_postgres/content_script_version_1.nim
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,20 @@ | ||
const ContentScriptVersion_1* = """ | ||
CREATE TABLE IF NOT EXISTS messages ( | ||
pubsubTopic VARCHAR NOT NULL, | ||
contentTopic VARCHAR NOT NULL, | ||
payload VARCHAR, | ||
version INTEGER NOT NULL, | ||
timestamp BIGINT NOT NULL, | ||
id VARCHAR NOT NULL, | ||
messageHash VARCHAR NOT NULL, | ||
storedAt BIGINT NOT NULL, | ||
CONSTRAINT messageIndex PRIMARY KEY (messageHash) | ||
); | ||
CREATE TABLE iF NOT EXISTS version ( | ||
version INTEGER NOT NULL | ||
); | ||
INSERT INTO version (version) VALUES(1); | ||
""" |
68 changes: 68 additions & 0 deletions
68
migrations/message_store_postgres/content_script_version_2.nim
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,68 @@ | ||
const ContentScriptVersion_2* = """ | ||
ALTER TABLE messages RENAME TO messages_backup; | ||
ALTER TABLE messages_backup DROP CONSTRAINT messageIndex; | ||
CREATE TABLE IF NOT EXISTS messages ( | ||
pubsubTopic VARCHAR NOT NULL, | ||
contentTopic VARCHAR NOT NULL, | ||
payload VARCHAR, | ||
version INTEGER NOT NULL, | ||
timestamp BIGINT NOT NULL, | ||
id VARCHAR NOT NULL, | ||
messageHash VARCHAR NOT NULL, | ||
storedAt BIGINT NOT NULL, | ||
CONSTRAINT messageIndex PRIMARY KEY (messageHash, storedAt) | ||
) PARTITION BY RANGE (storedAt); | ||
DO $$ | ||
DECLARE | ||
min_storedAt numeric; | ||
max_storedAt numeric; | ||
min_storedAtSeconds integer = 0; | ||
max_storedAtSeconds integer = 0; | ||
partition_name TEXT; | ||
create_partition_stmt TEXT; | ||
BEGIN | ||
SELECT MIN(storedAt) into min_storedAt | ||
FROM messages_backup; | ||
SELECT MAX(storedAt) into max_storedAt | ||
FROM messages_backup; | ||
min_storedAtSeconds := min_storedAt / 1000000000; | ||
max_storedAtSeconds := max_storedAt / 1000000000; | ||
partition_name := 'messages_' || min_storedAtSeconds || '_' || max_storedAtSeconds; | ||
create_partition_stmt := 'CREATE TABLE ' || partition_name || | ||
' PARTITION OF messages FOR VALUES FROM (' || | ||
min_storedAt || ') TO (' || (max_storedAt + 1) || ')'; | ||
IF min_storedAtSeconds > 0 AND max_storedAtSeconds > 0 THEN | ||
EXECUTE create_partition_stmt USING partition_name, min_storedAt, max_storedAt; | ||
END IF; | ||
END $$; | ||
INSERT INTO messages ( | ||
pubsubTopic, | ||
contentTopic, | ||
payload, | ||
version, | ||
timestamp, | ||
id, | ||
messageHash, | ||
storedAt | ||
) | ||
SELECT pubsubTopic, | ||
contentTopic, | ||
payload, | ||
version, | ||
timestamp, | ||
id, | ||
messageHash, | ||
storedAt | ||
FROM messages_backup; | ||
DROP TABLE messages_backup; | ||
UPDATE version SET version = 2 WHERE version = 1; | ||
""" |
37 changes: 37 additions & 0 deletions
37
migrations/message_store_postgres/pg_migration_manager.nim
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,37 @@ | ||
|
||
import | ||
content_script_version_1, | ||
content_script_version_2 | ||
|
||
type | ||
MigrationScript* = object | ||
version*: int | ||
scriptContent*: string | ||
|
||
proc init*(T: type MigrationScript, | ||
targetVersion: int, | ||
scriptContent: string): T = | ||
|
||
return MigrationScript( | ||
targetVersion: targetVersion, | ||
scriptContent: scriptContent) | ||
|
||
const PgMigrationScripts* = @[ | ||
MigrationScript( | ||
version: 1, | ||
scriptContent: ContentScriptVersion_1), | ||
MigrationScript( | ||
version: 2, | ||
scriptContent: ContentScriptVersion_2) | ||
] | ||
|
||
proc getMigrationScripts*(currentVersion: int64, | ||
targetVersion: int64): seq[string] = | ||
var ret = newSeq[string]() | ||
var v = currentVersion | ||
while v < targetVersion: | ||
ret.add(PgMigrationScripts[v].scriptContent) | ||
v.inc() | ||
return ret | ||
|
||
|
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,33 @@ | ||
|
||
import | ||
chronicles, | ||
chronos | ||
import | ||
../../../waku/waku_archive, | ||
../../../waku/waku_archive/driver as driver_module, | ||
../../../waku/waku_archive/driver/builder, | ||
../../../waku/waku_archive/driver/postgres_driver | ||
|
||
const storeMessageDbUrl = "postgres://postgres:test123@localhost:5432/postgres" | ||
|
||
proc newTestPostgresDriver*(): Future[Result[ArchiveDriver, string]] {.async.} = | ||
|
||
proc onErr(errMsg: string) {.gcsafe, closure.} = | ||
error "error creating ArchiveDriver", error = errMsg | ||
quit(QuitFailure) | ||
|
||
let | ||
vacuum = false | ||
migrate = true | ||
maxNumConn = 50 | ||
|
||
let driverRes = await ArchiveDriver.new(storeMessageDbUrl, | ||
vacuum, | ||
migrate, | ||
maxNumConn, | ||
onErr) | ||
if driverRes.isErr(): | ||
onErr("could not create archive driver: " & driverRes.error) | ||
|
||
return ok(driverRes.get()) | ||
|
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
Oops, something went wrong.