Skip to content

incorrect_length_validation-check: Consider check constraints with column lenght limits#218

Open
fatkodima wants to merge 1 commit into
gregnavis:masterfrom
fatkodima:incorrect_length_validation-check-constraints
Open

incorrect_length_validation-check: Consider check constraints with column lenght limits#218
fatkodima wants to merge 1 commit into
gregnavis:masterfrom
fatkodima:incorrect_length_validation-check-constraints

Conversation

@fatkodima

Copy link
Copy Markdown
Contributor

In postgres, when adding a limit to some existing text column, it will exclusively lock the table (preventing reads and writes) until the column data is verified and the column is changed. This is problematic for large tables and a different approach is usually used - check constraints with length limit (e.g. CHECK (length(email) <= 64)).

This PR extends existing checker to account for this.

@gregnavis gregnavis left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I love the idea! I left a suggestion on how to improve it: support columns with both column and check limits + add support for <.

end

def column_limit(table, column)
return column.limit if column.limit

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

There's an edge case we need to handle: a column can have both a limit and a check constraint. I suggest we add a method check_constraint_length_limit and pick the minimum of column.limit and check_constraint_length_limit(table, column).

return column.limit if column.limit

# Example: char_length(name::text) <= 64
pattern = /(char_|character_)?length\(['"`]?#{column.name}(::text)?['"`]?\)\s*<=\s*(?<limit>\d+)/i

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I suggest we add support for both < and <= and adjust the limit accordingly, i.e. < 100 means the limit is <= 99.

@fatkodima fatkodima force-pushed the incorrect_length_validation-check-constraints branch from fc2fe26 to b3e2816 Compare April 24, 2025 12:37
@fatkodima

Copy link
Copy Markdown
Contributor Author

Made changes.

@gregnavis gregnavis removed the waiting label Aug 2, 2025
@gregnavis gregnavis self-requested a review August 5, 2025 16:30
@gregnavis gregnavis added the false positive An error is reported when it shouldn't label Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

false positive An error is reported when it shouldn't

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants