Skip to content

[sql-31] firewalldb: Privacy Mapper schemas, queries and CRUD #1043

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

Merged
merged 4 commits into from
Apr 22, 2025

Conversation

ellemouton
Copy link
Member

This PR adds the schemas and queries for the privacy mapper DB.
It also adds the SQL implementation of the privacy mapper and plugs it into the unit tests and itests.

Part of #917

@ellemouton ellemouton added no-changelog This PR is does not require a release notes entry sql-ize labels Apr 17, 2025
@ellemouton ellemouton self-assigned this Apr 17, 2025
@ellemouton ellemouton force-pushed the sql31 branch 2 times, most recently from 1c732b8 to 262416f Compare April 17, 2025 11:25
Copy link
Contributor

@ViktorTigerstrom ViktorTigerstrom left a comment

Choose a reason for hiding this comment

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

Looks great 🔥🚀! Leaving two msall comments.

group_id BIGINT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,

-- The key of the privacy pair.
real TEXT NOT NULL,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:
real is seen as reserved keyword/datatype by many sql syntax highlighting tools (as also seen here in the Github uI), so would potentially choose another name as it gets a bit confusing.

Copy link
Member Author

Choose a reason for hiding this comment

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

will update 👍

CREATE TABLE IF NOT EXISTS privacy_pairs (
-- The group ID of the session that this privacy pair is associated
-- with.
group_id BIGINT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
Copy link
Contributor

Choose a reason for hiding this comment

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

does this enforce enough or should we use sessions(group_id) here, since we could use a second session's id in a group which would not be allowed?

Copy link
Member Author

Choose a reason for hiding this comment

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

you cannot reference a field that is not the primary key unless it is unique.

We will just need to force this on the CRUD layer

Copy link
Contributor

Choose a reason for hiding this comment

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

I figured that, would have been great to enforce this constraint on the db layer, but ok to do it on the CRUD layer, if it's not easily possible

//
//nolint:lll
type SQLPrivacyPairQueries interface {
SQLSessionQueries
Copy link
Contributor

Choose a reason for hiding this comment

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

could we remove this if we add a foreign key constraint to the group id?

Copy link
Member Author

Choose a reason for hiding this comment

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

see above comment

Copy link
Member Author

Choose a reason for hiding this comment

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

btw - i do think we can eventually move the check to PrivacyDB method instead - that way we only need to check this once. But for the sake of keeping the interfaces matching the kvdb style for now, let's check for each call.

Comment on lines 56 to 76
_, err = p.queries.GetPseudoForReal(ctx, sqlc.GetPseudoForRealParams{
GroupID: groupID,
Real: real,
})
if err == nil {
return ErrDuplicateRealValue
} else if !errors.Is(err, sql.ErrNoRows) {
return err
}

_, err = p.queries.GetRealForPseudo(ctx, sqlc.GetRealForPseudoParams{
GroupID: groupID,
Pseudo: pseudo,
})
if err == nil {
return ErrDuplicatePseudoValue
} else if !errors.Is(err, sql.ErrNoRows) {
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

wouldn't it be better to rely on the db to enforce the constraints on insert? the downside is that we don't perfectly know which constraint was violated, or is there a way to extract that info?

Copy link
Member Author

@ellemouton ellemouton Apr 17, 2025

Choose a reason for hiding this comment

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

exactly - I tried that but couldnt get it to work nicely: the reason was cause a unique constraint error here aborts the entire transaction that NewPairs falls under. So then it isnt possible to catch an error outside of this call (but under the same tx) and then continue making other calls with the transaction

Define the privacy mapper schemas and queries.
In this commit, we introduce the SQL impl of the privacy mapper store.

The privacy mapper unit tests can now be run against the SQL impl.
Remove the `NewPrivacyMapDB` type alias. It is not needed.
Copy link
Contributor

@bitromortac bitromortac left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

@ellemouton ellemouton merged commit 9abe0ab into lightninglabs:master Apr 22, 2025
16 of 21 checks passed
@ellemouton ellemouton deleted the sql31 branch April 22, 2025 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-changelog This PR is does not require a release notes entry sql-ize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants