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

mssql: Improve estimate of max comment length #4362

Merged

Conversation

jeremy-w
Copy link
Contributor

@jeremy-w jeremy-w commented Mar 10, 2021

Before, table comments used a limit of 60 chars (seemingly inherited from the MySQL dialect), while columns warned about exceeding a limit of 255 chars. This led to spurious warnings.

Now, warnings are only emitted if it seems possible for the comment to exceed the actual, documented 7,500 byte limit:

[ @value= ] { 'value'}
Is the value to be associated with the property. value is sql_variant, with a default of NULL. The size of value cannot be more than 7,500 bytes. (sp_addextendedproperty (Transact-SQL), emphasis added)

That limit proved to be when the comment.length exceeded 7500/2, as demonstrated by testing.

(This is a small step towards #4204.)

Questions for Reviewers

  • Making the warning more precise:

    • Would it be OK for a schema compiler to run queries to sort out stuff like "what is the database collation"? That would theoretically allow us to actually compute the byte length, not make a pessimistic estimate. (But my guess is "no".)
    • If not, would it be better to use an optimistic estimate of 1 char == 1 byte?
  • Should we even bother warning at all? When we get around to actually adding the comments, MSSQL will raise an error if the length is exceeded for us:

    Msg 15097, Level 16, State 1, Server 49df3e21e80c, Procedure sp_updateextendedproperty, Line 16
    The size associated with an extended property cannot be more than 7,500 bytes.

@kibertoad
Copy link
Collaborator

@jeremy-w Considering that The size associated with an extended property cannot be more than 7,500 bytes. doesn't mention comments anywhere, warning is still helpful to point people experiencing the issue in the right direction. Could you also add two integration tests, one of them being slightly below the threshold, and another one being above the threshold and actually causing MSSQL to fail?

@jeremy-w
Copy link
Contributor Author

jeremy-w commented Mar 11, 2021

@kibertoad would the warning still be useful if we trapped the error during adding the comment and converted it into a more knex-relevant error? or would that do to replace the error?

since the "length exceeded" error has Level 16 (greater than 10, less than 20), it looks like try…catch would work.

@kibertoad
Copy link
Collaborator

@jeremy-w While we already have plenty of magic for handling MSSQL edge-cases, I would prefer to avoid adding even more unless strictly necessary. Just improving error messages doesn't sound like a big enough reason to complicate generated queries, so I would still prefer just a knex warning in this case.

@jeremy-w jeremy-w force-pushed the jsherman/mssql/update-comment-length-limits branch from 50ba27e to 6906532 Compare March 17, 2021 19:31
The tests in this file were not being picked up, because the file did
not match the *.spec.js glob used by
test/mocha-integration-config-test.js.
@jeremy-w jeremy-w force-pushed the jsherman/mssql/update-comment-length-limits branch from 6906532 to ba78b59 Compare March 17, 2021 19:31
@jeremy-w
Copy link
Contributor Author

@kibertoad Updated with the requested tests. Also added a test to validate our "7500/4" estimate for when to warn is a good one.

This also gets us most of the way to being able to generate comments as part of the schema builder, so things are looking good for #4204. :)

Copy link
Contributor Author

@jeremy-w jeremy-w left a comment

Choose a reason for hiding this comment

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

thought of a couple issues overnight. i bet we can allow a 2x longer char count before warning due to how JS counts character length when characters outside the BMP get involved.

test/integration2/dialects/mssql.spec.js Outdated Show resolved Hide resolved
test/integration2/dialects/mssql.spec.js Show resolved Hide resolved
Turns out they have up to 7500/2 characters (as JavaScript's
String.length counts them) before we have to worry about the encoded
comment exceeding the 7500-byte limit.
@jeremy-w jeremy-w force-pushed the jsherman/mssql/update-comment-length-limits branch from ba78b59 to f487809 Compare March 19, 2021 01:16
@jeremy-w
Copy link
Contributor Author

OK, I addressed the issues I thought of, and I was able to provide a lot more headroom - comments have to be longer than 3,750 characters before comment length begins to be a concern!

@kibertoad kibertoad merged commit 0d43c1c into knex:master Mar 25, 2021
@kibertoad
Copy link
Collaborator

Thank you!

@kibertoad
Copy link
Collaborator

Released in 0.95.3.

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

2 participants