-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Postgres migrations #2477
Changes from all commits
9d933f1
26d7cf6
afe5223
0ff9711
fa492fe
cc6e377
a2b07a8
850cabc
545cae4
b9c3a6b
c45bc69
f5848dc
72b62fd
fd7828d
e7df8bb
d1ac066
1723133
6de70a5
8bb6813
4b8d76e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
|
||
""" |
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; | ||
Ivansete-status marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this migration I think it is better to add this index later after data inserted its time saving as more efficient to do it once for the db engine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok good point , I will cover that in a separate PR. |
||
) 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; | ||
Ivansete-status marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
|
||
""" |
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 | ||
|
||
|
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) | ||
Ivansete-status marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return ok(driverRes.get()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you can
waitFor
in a async proc. That would prevent the runtime from progressing no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can, it just means you are intended to wait till future become avail...
although I'm not pretty sure if it would not be better to wait with timeout?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comments!
@SionoiS - yes this is feasible. In this case we are blocking because this is a needed condition to be satisfied.
@NagyZoltanPeter - I will submit a separate PR where we apply that great proposal of using
withTimeout