Repository navigation
fix(log): show bind parameters in logs instead of placeholders - #11267
Conversation
placeholders
modified: test/integration/sequelize.test.js modified: test/unit/dialects/abstract/query.test.js
Codecov Report
@@ Coverage Diff @@
## master #11267 +/- ##
==========================================
+ Coverage 94.19% 96.27% +2.07%
==========================================
Files 94 94
Lines 9152 9161 +9
==========================================
+ Hits 8621 8820 +199
+ Misses 531 341 -190
Continue to review full report at Codecov.
|
|
Hi @supperchong, thank you very much once again. I see you are still adding commits, so when you think this is ready please comment here to make it clear, so maintainers can look. |
|
@papb I'm sorry, I omit something~It's ok now. |
| @@ -221,13 +221,16 @@ class Query extends AbstractQuery { | |||
| const conn = this.connection; | |||
There was a problem hiding this comment.
No need for these changes, _logQuery should work with undefined parameters
There was a problem hiding this comment.
These changes is necessary. When method is 'exec', the sql will be formated and does't contain $ | ? , but parameters is not undefined.
There was a problem hiding this comment.
Can't we handle undefined parameters at _logQuery?
There was a problem hiding this comment.
emmm ,for example
run(sql, parameters) {
const conn = this.connection;
this.sql = sql;
const method = this.getDatabaseMethod();
let complete;
if (method === 'exec') {
console.log('prevSql', sql);
//INSERT OR IGNORE INTO `Users` (`username`,`mood`) VALUES ($1,$2);
// exec does not support bind parameter
sql = AbstractQuery.formatBindParameters(sql, this.options.bind, this.options.dialect || 'sqlite', { skipUnescape: true })[0];
this.sql = sql;
console.log('parameters', parameters);
//{ '$1': 'Toni', '$2': 'happy' }
console.log('sql', sql);
//INSERT OR IGNORE INTO `Users` (`username`,`mood`,`createdAt`,`updatedAt`) VALUES ('Toni','happy'); )
complete = this._logQuery(sql, debug);
} else {
complete = this._logQuery(sql, debug, parameters);
}There was a problem hiding this comment.
Ah understood, keep these changes then
| }); | ||
| }).then(()=>{ | ||
| if ( ['mysql', 'mariadb'].includes(dialect)) { | ||
| expect(createSql).to.equal('Executing (default): INSERT INTO `Users` (`id`,`username`,`emailAddress`) VALUES (DEFAULT,?,?);["john","[email protected]"]'); |
There was a problem hiding this comment.
I think all dialects should follow same pattern.
SELECT * FROM table_name WHERE id = $1 AND name = $2; 3323323,abcdefghThere was a problem hiding this comment.
You only need to focus on parameter appending part, ? / $ used for bind parameters are database dependent so no need to change them. Just append parameters as an array after sql statement
| logParameter=';'; | ||
| } | ||
| if (parameters) { | ||
| logParameter+=JSON.stringify(parameters); |
There was a problem hiding this comment.
As I said in comments below, we want one standard style for appending parameters after SQL. I think comma separated values works best.
Also please use space before/after = :)
There was a problem hiding this comment.
@sushantdhiman Thanks a lot. The parameters are object for mssql and sqlite. Should remain them in log ?
like
INSERT INTO `Users` (`id`,`username`,`emailAddress`) VALUES (NULL,$1,$2);{"$1":"john","$2":"[email protected]"}and
INSERT INTO `Users` (`id`,`username`,`emailAddress`) VALUES (NULL,$one,$two);{"$one":"john","$two":"[email protected]"}There was a problem hiding this comment.
No they should be appended as array to final sql. Like SQL SQL SQL; value1, value2, value3. You will have to convert object to array in _logQuery function
There was a problem hiding this comment.
@sushantdhiman So the append values have the same order and repeat with placeholders ?
There was a problem hiding this comment.
Hmm object property ordering could be an issue, when it is an array append it, when it is an object stringify it.
I think we might want to hide this behind extra flag like sequelize.options.loggingParameters= false
There was a problem hiding this comment.
1 and 2 is ok. Please check it. Furthermore, although options.bindParams is an array, but sql may contain $one etc.
There was a problem hiding this comment.
No problem, at-least all bounded variables will be logged in same format by using parameters from options.bindParams
There was a problem hiding this comment.
Yeah, i know, i will modify it.
There was a problem hiding this comment.
There is not options.bindParams . options.bind and options.replacements can be object.
|
Hi @supperchong, is this ready in your opinion? Or are you still working on this? |
|
@papb Hi, I hope to resolve this problem. I'm waiting for @sushantdhiman to reply. |
|
@supperchong Hide this feature behind a config flag, like |
modified: lib/sequelize.js modified: types/lib/sequelize.d.ts
|
@sushantdhiman @papb Thanks for your suggestion. I add logQueryParameters options, is this ok? |
|
🎉 This PR is included in version 5.19.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
not convenient to rerun sql... |
|
@superlbr What do you mean? Can you be more specific? |
|
@papb sql in error is different after: |
|
Hmm, I'd say the new way is better though 😬 you can even rerun the query with bind parameters now, it has much better security! |
Closes #11151
Closes #10067
Pull Request check-list
Please make sure to review and check all of these items:
npm run testornpm run test-DIALECTpass with this change (including linting)?Description of change