Skip to content

Commit

Permalink
Remove single quotes from stacktrace so it works better with mysqldum…
Browse files Browse the repository at this point in the history
…pslow
  • Loading branch information
hsource committed Jun 15, 2024
1 parent ed12c6a commit 9b3188f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 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;
}
}
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 9b3188f

Please sign in to comment.