sqldb: fix SQLStr helper#10193
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @ellemouton, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request addresses a subtle but important bug in the SQLStr helper function within the sqldb package. The fix ensures that empty strings are properly converted to sql.NullString instances where the Valid field is set to true, thereby allowing for the correct representation of non-NULL, empty string values in database operations.
Highlights
- Bug Fix: The
SQLStrhelper function has been fixed to correctly handle empty strings. Previously, an empty string input would result in asql.NullStringwithValidset tofalse, incorrectly treating it as a NULL value. Now, it consistently setsValidtotrue, allowing empty strings to be represented as valid, non-NULL values. - Consistency: This change aligns
SQLStrwith the intended behavior of otherSQL*helper functions, which are designed to always set theValidfield totruefor their respectivesql.Null*types.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request fixes the SQLStr helper to correctly handle empty strings by always setting the Valid field to true on the returned sql.NullString. This makes it consistent with other SQL* helpers and allows storing valid, empty strings in the database. The change is correct and improves the codebase. I've added one suggestion to improve the function's comment to be more descriptive about its behavior, in line with the repository's style guide for exported functions.
ViktorT-11
left a comment
There was a problem hiding this comment.
Leaving some potential useful extra info for regarding this change in the sqldb package :).
| if s == "" { | ||
| return sql.NullString{} | ||
| } |
There was a problem hiding this comment.
Should this change maybe be optional when using the this function, or have another helper function that does adds this change?
As we are moving to sqldb/v2 this change will be present in that package as well, and when we are utalizing this functionality in other repos we will need to update the logic for cases where we use this function otherwise.
Such as here:
https://github.com/lightninglabs/lightning-terminal/blob/bce4c049a95a8484b04eddbf856c9356c8a64833/session/sql_migration.go#L349
We could also utalize this function for a lot of cases in litd if we do not change the current logic, such as here:
https://github.com/lightninglabs/lightning-terminal/blob/bce4c049a95a8484b04eddbf856c9356c8a64833/firewalldb/actions_sql.go#L74-L88
I think we potentially also need to change the logic here in lnd with this change?
Lines 229 to 231 in ea6cc81
There was a problem hiding this comment.
yeah it's tricky cause i think it should never have been like this in the first place.
but yes, good idea. I'll leave the existing one as is so as not to break existing callers & then i'll add a new function with the desired behaviour.
|
Also noticed that So when |
The SQL* helpers are meant to always set the `Valid` field of the sql.Null* type to true. Otherwise they cannot be used to set a valid, empty field. However, we dont want to break the behaviour of the existing SQLStr helper and so this commit adds a new helper with the desired functionality.
cb5c342 to
40b2796
Compare
|
Why don't we keep the null string instead of creating a DB entry which is an empty string. Is the reason because some nodes might send us some data where this data is set to an empty string and the signature covers this empty data ? |
|
@ziggie1984 - yes exactly. we use the same |
ViktorT-11
left a comment
There was a problem hiding this comment.
Thanks a lot for the changes 🎉!
The SQL* helpers are meant to always set the
Validfield of the sql.Null* type to true. Otherwise they cannot be used to set a valid, empty field. In this commit, the SQLStr helper is fixed so that we can represent a valid/set empty string field.This is important for something like storing node announcement data where fields like
Aliasmight be present but might be a zero length string. The difference is important so that when we construct a pure TLV message, we know wether to include the field or not.We need this for #9795