Exclude text arrays from the Rubocop for adding limits to texts
Problem description
Adding a limit to a text array using add_text_limit
results to an error because it tries to: char_length(text[]) <= #{limit}
At the moment our Cops for enforcing strings and limits on texts both do not take into account that case:
- The first one requires the migration to use
text[]
instead ofstring[]
- The second one complains if the
text[]
has not anadd_text_limit
for it
Additional Info
Adding a limit to a array (i.e. how many elements it can include) is a no-op in Postgres: 8.15.1. Declaration of Array Types
[..] However, the current implementation ignores any supplied array size limits, i.e., the behavior is the same as for arrays of unspecified length.
The current implementation does not enforce the declared number of dimensions either. Arrays of a particular element type are all considered to be of the same type, regardless of size or number of dimensions. So, declaring the array size or number of dimensions in CREATE TABLE is simply documentation; it does not affect run-time behavior. [..]
Additionally, adding a character length limit on each element of a text array requires a pretty involved (and expensive) process:
- Create a function that unnests the text array and checks each element
- Add a check constraint using that function
Solution
I am not sure whether we have a use case where we want to impose a text limit on the individual elements of a text array.
Given the complicated nature of adding the function above, I thing that until such a need arises, we should:
-
Update the Cops that require texts instead of strings and enforce limits on text columns to not complain when arrays are added
-
Add a section on the documentation for adding textual data types in migrations with what we should do when we want to add a string/text array
This page may contain information related to upcoming products, features and functionality. It is important to note that the information presented is for informational purposes only, so please do not rely on the information for purchasing or planning purposes. Just like with all projects, the items mentioned on the page are subject to change or delay, and the development, release, and timing of any products, features, or functionality remain at the sole discretion of GitLab Inc.