Skip to content

Commit

Permalink
Merge pull request #16 from anyTV/HD-104089
Browse files Browse the repository at this point in the history
HD-104089 Earnings > IPN Exception > Miss receiving 250 transactions
  • Loading branch information
jkcdarunday committed Nov 23, 2017
2 parents 260fc56 + a40beeb commit e244752
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v0.0.56
-----

* Fixed a bug where mysql key can be changed since it's using a reference on the mysql object

v0.0.55
-----

Expand Down
7 changes: 3 additions & 4 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Connection = function () {
function Connection(handle) {
function Connection(handle, key) {
_classCallCheck(this, Connection);

this.max_retries = 3;
this.handle = handle;
this.retries = 0;
this.connect();
this.connect(key || handle._key);
}

_createClass(Connection, [{
key: 'connect',
value: function connect() {
value: function connect(key) {
var handle = this.handle;
var key = handle._key;
var connection = void 0;

if (handle[key].is_pool) {
Expand Down
15 changes: 9 additions & 6 deletions lib/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var Query = function () {

this.previous_errors = [];
this.mysql = mysql;
this.retryable_errors = this.mysql.retryable_errors || this.mysql[this.mysql._key].config.retryable_errors;
this.key = mysql._key;
this.retryable_errors = this.mysql.retryable_errors || this.mysql[this.key].config.retryable_errors;
this.retries = 0;

args.shift();
Expand Down Expand Up @@ -83,15 +84,17 @@ var Query = function () {
}
}

if (!mysql_handler.current_connection) {
if (this.mysql[this.mysql._key].connection) {
this.mysql.current_connection = this.mysql[this.mysql._key].connection;
if (!connection) {
if (this.mysql[this.key].connection) {
this.mysql.current_connection = this.mysql[this.key].connection;
} else {
new _Connection2.default(mysql_handler);
new _Connection2.default(mysql_handler, this.key);
}

connection = this.mysql.current_connection;
}

mysql_handler.current_connection.query.apply(mysql_handler.current_connection, arguments);
connection.query.apply(connection, arguments);
}
}]);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "anytv-node-mysql",
"version": "0.0.55",
"version": "0.0.56",
"description": "Our version of mysql that makes connecting to mysql simpler and more elegant. Especially made for our awesome expressjs boilerplate.",
"main": "index.js",
"dependencies": {
Expand Down
7 changes: 3 additions & 4 deletions src/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import mysql from 'mysql';

export default class Connection {

constructor (handle) {
constructor (handle, key) {
this.max_retries = 3;
this.handle = handle;
this.retries = 0;
this.connect();
this.connect(key || handle._key);
}

connect () {
connect (key) {
const handle = this.handle;
const key = handle._key;
let connection;

if (handle[key].is_pool) {
Expand Down
19 changes: 10 additions & 9 deletions src/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default class Query {

this.previous_errors = [];
this.mysql = mysql;
this.retryable_errors = this.mysql.retryable_errors || this.mysql[this.mysql._key].config.retryable_errors;
this.key = mysql._key;
this.retryable_errors = this.mysql.retryable_errors || this.mysql[this.key].config.retryable_errors;
this.retries = 0;

args.shift();
Expand Down Expand Up @@ -69,18 +70,18 @@ export default class Query {
}


if (!mysql_handler.current_connection) {
if (this.mysql[this.mysql._key].connection) {
this.mysql.current_connection = this.mysql[this.mysql._key].connection;
if (!connection) {
if (this.mysql[this.key].connection) {
this.mysql.current_connection = this.mysql[this.key].connection;
}
else {
new Connection(mysql_handler);
new Connection(mysql_handler, this.key);
}

connection = this.mysql.current_connection;
}

mysql_handler
.current_connection
.query
.apply(mysql_handler.current_connection, arguments);
connection.query
.apply(connection, arguments);
}
}
30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1013,4 +1013,34 @@ describe('Overall test', () => {
done();
});

it ('mysql.query should use the same key when retrying', (done) => {
const mysql = new CustomMySQL();
const key = 'key';
const key2 = 'key2';

mysql.set_logger(noop_logger)
.add(key, FREE_DB)
.add(key2, FREE_DB2)
.use(key)
.query('CREATE TABLE users(id int NOT NULL);', (err, result) => {

should.equal(err, null);
result.should.exist;

mysql.use(key)
.retry_if(['ER_BAD_NULL_ERROR'])
.build('INSERT INTO users (SELECT IF((@tmp := COALESCE(@tmp, 0) + COALESCE(@tmp, 1))=1, NULL, @tmp))')
.promise()
.then(() => {
mysql.use(key)
.query('DROP TABLE users', done)
.end();
});

mysql.use(key2);
});


});

});

0 comments on commit e244752

Please sign in to comment.