Skip to content
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

refactor(databases): Creation of the databases folder to keep the logic for sqlite and postgres #1811

Merged
merged 10 commits into from
Jun 22, 2023

Conversation

Ivansete-status
Copy link
Collaborator

Description

Simple refactoring to move all the database logic in one place (common/databases)

@Ivansete-status Ivansete-status marked this pull request as ready for review June 20, 2023 16:15
waku/common/databases/db_postgres/pgasyncpool.nim Outdated Show resolved Hide resolved
let execRes = db.query(statement, NoopRowHandler)
if execRes.isErr():
error "failed to execute migration statement", statement=statement, error=execRes.error
return err("failed to execute migration statement")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: what happen if it fail in the middle of a migration does it need to be rolled back or no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. We are assuming that either all the query works or all the query fails but we are not considering the case where the query partially worked in which case we should make sure the database is rolled back to the initial state. However, I'd consider that optimization for further PRs. I'd like to keep this PR with minimal changes, i.e. only rename files or delete the if statement that you suggested :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, I just said "just rename files" but precisely at this point, I'm adding content from "waku/common/sqlite/migrations.nim", which I removed. If you are not happy with that I can restore back the 'migrations.nim'.

Copy link
Contributor

@SionoiS SionoiS Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally fine to not consider this edge case for now. We just need to not forget about it.

@Ivansete-status Ivansete-status self-assigned this Jun 21, 2023
Copy link
Contributor

@jm-clius jm-clius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor seems reasonable to me, though my preference is for comment underneath to be addressed before merging - i.e. creating barrel imports for db_sqlite and db_postgresql which exports a common module in order to have explicit Database error handling (in this case an explicit DatabaseResult).

@@ -23,7 +22,7 @@ template projectRoot: string = currentSourcePath.rsplit(DirSep, 1)[0] / ".." / "
const PeerStoreMigrationPath: string = projectRoot / "migrations" / "peer_store"


proc migrate*(db: SqliteDatabase, targetVersion = SchemaVersion): DatabaseResult[void] =
proc migrate*(db: SqliteDatabase, targetVersion = SchemaVersion): Result[void, string] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(here and elsewhere)
I think we should keep the explicit DatabaseResult. One way to do it is to define this in a new db_common module (under databases) and then create barrel imports for both sqlite and postgres that imports and exports both the underlying db modules (i.e. either db_sqlite or db_postgres modules) and the new common module. That way modules making use of a specific database would only import db_sqlite or db_postgresql, but get access to the common symbols as well (which includes DatabaseResult.

Copy link
Contributor

@jm-clius jm-clius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for your patience :)

@Ivansete-status Ivansete-status merged commit a44d4bf into master Jun 22, 2023
13 checks passed
@Ivansete-status Ivansete-status deleted the refactor-databases branch June 22, 2023 09:27
SionoiS pushed a commit that referenced this pull request Jun 22, 2023
…ic for sqlite and postgres (#1811)

* Refactoring in sqlite and postgres. Creation of the databases folder.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants