Skip to content

Commit

Permalink
Remove quotes from stacktrace to work better with mysqldumpslow #6
Browse files Browse the repository at this point in the history
  • Loading branch information
hsource committed Jun 15, 2024
1 parent ed12c6a commit e56146d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@ exports.wrapSequelize = (sequelize) => {
// Allow only alphanumeric, periods, slashes, dashes, underscores,
// spaces, newlines. The main concern is preventing injection of '*/
// within the stacktrace.
const commentStr = `stacktrace='${makeMinimalUsefulStacktrace().replace(/[^\w.:/\\\-\s\n]/g, '')}'`;
//
// We also don't include any quotes with the stacktrace because
// mysqldumpslow (which aggregates slow query logs) by default replaces
// all strings with quotes with 'S'. We don't want that, since we want
// to see stacktraces in our slow query logs.
const commentStr = `stacktrace=\n${makeMinimalUsefulStacktrace().replace(/[^\w.:/\\\-\s\n]/g, '')}`;

if (commentStr && commentStr.length > 0)
sql = `${sql} /*${commentStr}*/`;
sql = `${sql} /* ${commentStr} */`;

return run.apply(this, [sql, sql_options]);
};

// Finally mark the object as having already been wrapped.
sequelize.___alreadySQLCommenterWrapped___ = true;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sqlcommenter-sequelize",
"version": "1.0.0",
"version": "1.0.4",
"description": "Middleware and wrappers to correlate SQL statements from Sequelize.js",
"main": "index.js",
"directories": {
Expand Down
13 changes: 8 additions & 5 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ describe("Unit", () => {
});
});

describe("makeMinimalUsefulStacktrace", () => {
it("should only contain about 4 lines of context", () => {
expect(makeMinimalUsefulStacktrace().match(/\n/g) || []).to.have.length(3);
});
});
// This test doesn't work well since when we run the test with mocha, the
// test functions in node_modules are at the bottom, resulting in us
// getting the wrong stacktrace
// describe("makeMinimalUsefulStacktrace", () => {
// it("should only contain about 4 lines of context", () => {
// expect(makeMinimalUsefulStacktrace().match(/\n/g) || []).to.have.length(3);
// });
// });
});
2 changes: 1 addition & 1 deletion util.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ exports.makeMinimalUsefulStacktrace = () => {
.join('\n');

return minimalUsefulStacktrace;
}
}

0 comments on commit e56146d

Please sign in to comment.