Skip to content

fix(log): show bind parameters in logs instead of placeholders - #11267

Merged
sushantdhiman merged 13 commits into
sequelize:masterfrom
supperchong:log
Sep 19, 2019
Merged

sushantdhiman merged 13 commits into
sequelize:masterfrom
supperchong:log

Conversation

@supperchong

Copy link
Copy Markdown
Contributor

Closes #11151
Closes #10067

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you update the typescript typings accordingly (if applicable)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

@codecov

codecov Bot commented Aug 1, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #11267 into master will increase coverage by 2.07%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
lib/sequelize.js 95.95% <ø> (+3.11%) ⬆️
lib/dialects/mariadb/query.js 87.96% <100%> (ø) ⬆️
lib/dialects/postgres/query.js 97.77% <100%> (ø) ⬆️
lib/dialects/mysql/query.js 98.31% <100%> (+83.19%) ⬆️
lib/dialects/sqlite/query.js 98.65% <100%> (ø) ⬆️
lib/dialects/mssql/query.js 95.53% <100%> (ø) ⬆️
lib/dialects/abstract/query.js 91.98% <100%> (+0.53%) ⬆️
lib/dialects/abstract/query-generator.js 97.33% <0%> (+0.43%) ⬆️
lib/dialects/mysql/query-generator.js 97.79% <0%> (+1.32%) ⬆️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 555dbe1...26cbb2a. Read the comment docs.

@papb

papb commented Aug 1, 2019

Copy link
Copy Markdown
Member

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 papb added dialect: all status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. type: other DEPRECATED: replace with the "meta" issue type labels Aug 1, 2019
@supperchong

supperchong commented Aug 1, 2019 •

Copy link
Copy Markdown
Contributor Author

@papb I'm sorry, I omit something~It's ok now.

@papb papb added status: awaiting maintainer and removed status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. labels Aug 1, 2019
@@ -221,13 +221,16 @@ class Query extends AbstractQuery {
const conn = this.connection;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No need for these changes, _logQuery should work with undefined parameters

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These changes is necessary. When method is 'exec', the sql will be formated and does't contain $ | ? , but parameters is not undefined.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't we handle undefined parameters at _logQuery?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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);
    }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah understood, keep these changes then

Comment thread test/integration/sequelize.test.js Outdated
});
}).then(()=>{
if ( ['mysql', 'mariadb'].includes(dialect)) {
expect(createSql).to.equal('Executing (default): INSERT INTO `Users` (`id`,`username`,`emailAddress`) VALUES (DEFAULT,?,?);["john","[email protected]"]');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think all dialects should follow same pattern.

SELECT * FROM table_name WHERE id = $1 AND name = $2; 3323323,abcdefgh

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread lib/dialects/abstract/query.js Outdated
logParameter=';';
}
if (parameters) {
logParameter+=JSON.stringify(parameters);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 = :)

@supperchong supperchong Aug 4, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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]"}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@sushantdhiman So the append values have the same order and repeat with placeholders ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

1 and 2 is ok. Please check it. Furthermore, although options.bindParams is an array, but sql may contain $one etc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No problem, at-least all bounded variables will be logged in same format by using parameters from options.bindParams

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, i know, i will modify it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

options.bind?

@supperchong supperchong Aug 7, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is not options.bindParams . options.bind and options.replacements can be object.

@sushantdhiman sushantdhiman added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action and removed status: awaiting maintainer labels Aug 4, 2019
@papb

papb commented Aug 20, 2019

Copy link
Copy Markdown
Member

Hi @supperchong, is this ready in your opinion? Or are you still working on this?

@supperchong

Copy link
Copy Markdown
Contributor Author

@papb Hi, I hope to resolve this problem. I'm waiting for @sushantdhiman to reply.

@papb papb added status: awaiting maintainer and removed status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action labels Aug 22, 2019
@sushantdhiman

Copy link
Copy Markdown
Contributor

@supperchong Hide this feature behind a config flag, like sequelize.config.logQueryParameters, otherwise LGTM

@sushantdhiman sushantdhiman added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action and removed status: awaiting maintainer labels Aug 28, 2019
@supperchong

supperchong commented Sep 3, 2019 •

Copy link
Copy Markdown
Contributor Author

@sushantdhiman @papb Thanks for your suggestion. I add logQueryParameters options, is this ok?

@papb papb removed the dialect: all label Sep 17, 2019
@sushantdhiman
sushantdhiman merged commit 992ddf7 into sequelize:master Sep 19, 2019
@sushantdhiman

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 5.19.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@superlbr

Copy link
Copy Markdown

not convenient to rerun sql...

@papb

papb commented Sep 20, 2019

Copy link
Copy Markdown
Member

@superlbr What do you mean? Can you be more specific?

@superlbr

Copy link
Copy Markdown

@papb sql in error is different
before:

original: {
  "code":"ER_DATA_TOO_LONG",
  "sql":"INSERT INTO `crm_member` (`id`,`road_name`) VALUES (DEFAULT,'xxx');"
}

after:

original: {
  "code":"ER_DATA_TOO_LONG",
  "sql":"INSERT INTO `crm_member` (`id`,`road_name`) VALUES (DEFAULT,?);"
  "parameters": [DEFAULT, 'xxx']
}

@papb

papb commented Sep 20, 2019

Copy link
Copy Markdown
Member

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!

@papb papb removed the status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action label Jan 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released type: other DEPRECATED: replace with the "meta" issue type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants