2020-02-08 22:23:33 +00:00
|
|
|
// Copyright IBM Corp. 2013,2019. All Rights Reserved.
|
2016-05-03 23:52:03 +00:00
|
|
|
// Node module: loopback-connector-mysql
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
'use strict';
|
2013-08-23 21:31:43 +00:00
|
|
|
require('./init.js');
|
2020-08-28 06:43:09 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const should = require('should');
|
|
|
|
const DataSource = require('loopback-datasource-juggler').DataSource;
|
|
|
|
const mysqlConnector = require('../');
|
|
|
|
const url = require('url');
|
2013-06-13 16:34:31 +00:00
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
let db, DummyModel, odb, config;
|
2013-06-13 16:34:31 +00:00
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
describe('connections', function() {
|
|
|
|
before(function() {
|
2014-02-13 00:57:06 +00:00
|
|
|
require('./init.js');
|
2013-06-13 16:34:31 +00:00
|
|
|
|
2016-03-24 17:20:56 +00:00
|
|
|
config = global.getConfig();
|
|
|
|
|
2017-07-24 16:48:26 +00:00
|
|
|
odb = global.getDataSource({collation: 'utf8_general_ci',
|
|
|
|
createDatabase: true});
|
2014-02-13 00:57:06 +00:00
|
|
|
db = odb;
|
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('should pass with valid settings', function(done) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const db = new DataSource(mysqlConnector, config);
|
2016-03-24 17:20:56 +00:00
|
|
|
db.ping(done);
|
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('ignores all other settings when url is present', function(done) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const formatedUrl = generateURL(config);
|
|
|
|
const dbConfig = {
|
2016-03-24 17:20:56 +00:00
|
|
|
url: formatedUrl,
|
|
|
|
host: 'invalid-hostname',
|
|
|
|
port: 80,
|
|
|
|
database: 'invalid-database',
|
|
|
|
username: 'invalid-username',
|
|
|
|
password: 'invalid-password',
|
|
|
|
};
|
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
const db = new DataSource(mysqlConnector, dbConfig);
|
2016-03-24 17:20:56 +00:00
|
|
|
db.ping(done);
|
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('should use utf8 charset', function(done) {
|
2023-06-18 18:04:31 +00:00
|
|
|
const test_set = /utf8mb4/;
|
|
|
|
const test_collo = /utf8mb4_0900_ai_ci/;
|
|
|
|
const test_set_str = 'utf8mb4';
|
|
|
|
const test_set_collo = 'utf8mb4_0900_ai_ci';
|
2014-02-13 00:57:06 +00:00
|
|
|
charsetTest(test_set, test_collo, test_set_str, test_set_collo, done);
|
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('should disconnect first db', function(done) {
|
|
|
|
db.disconnect(function() {
|
2017-07-24 16:48:26 +00:00
|
|
|
odb = global.getDataSource();
|
2015-05-13 17:17:15 +00:00
|
|
|
done();
|
2013-06-13 16:34:31 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
2019-05-09 11:26:56 +00:00
|
|
|
it('should disconnect then connect and ORM should work', function() {
|
2020-08-28 06:43:09 +00:00
|
|
|
const ds = new DataSource(mysqlConnector, config);
|
|
|
|
const Student = ds.define('Student', {
|
2019-05-09 11:26:56 +00:00
|
|
|
name: {type: String, length: 255},
|
|
|
|
age: {type: Number},
|
|
|
|
}, {
|
|
|
|
forceId: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
return ds.connect()
|
|
|
|
.then(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
return ds.automigrate(['Student']);
|
|
|
|
})
|
|
|
|
.then(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should(ds.connected).be.True();
|
|
|
|
return ds.disconnect();
|
|
|
|
})
|
|
|
|
.then(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should(ds.connected).be.False();
|
|
|
|
return ds.connect();
|
|
|
|
})
|
|
|
|
.then(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should(ds.connected).be.True();
|
|
|
|
return Student.count();
|
|
|
|
})
|
|
|
|
.then(function(count) {
|
|
|
|
should(count).be.a.Number();
|
|
|
|
return ds.disconnect();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('should use latin1 charset', function(done) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const test_set = /latin1/;
|
|
|
|
const test_collo = /latin1_general_ci/;
|
|
|
|
const test_set_str = 'latin1';
|
|
|
|
const test_set_collo = 'latin1_general_ci';
|
2014-02-13 00:57:06 +00:00
|
|
|
charsetTest(test_set, test_collo, test_set_str, test_set_collo, done);
|
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('should drop db and disconnect all', function(done) {
|
|
|
|
db.connector.execute('DROP DATABASE IF EXISTS ' + db.settings.database, function(err) {
|
|
|
|
db.disconnect(function() {
|
2014-02-13 00:57:06 +00:00
|
|
|
done();
|
|
|
|
});
|
2013-06-13 16:34:31 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
2016-05-09 02:37:19 +00:00
|
|
|
|
|
|
|
describe('lazyConnect', function() {
|
|
|
|
it('should skip connect phase (lazyConnect = true)', function(done) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const dbConfig = {
|
2016-05-09 02:37:19 +00:00
|
|
|
host: '127.0.0.1',
|
|
|
|
port: 4,
|
|
|
|
lazyConnect: true,
|
|
|
|
};
|
2020-08-28 06:43:09 +00:00
|
|
|
const ds = new DataSource(mysqlConnector, dbConfig);
|
2016-05-09 02:37:19 +00:00
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
const errTimeout = setTimeout(function() {
|
2016-05-09 02:37:19 +00:00
|
|
|
done();
|
|
|
|
}, 2000);
|
|
|
|
ds.on('error', function(err) {
|
|
|
|
clearTimeout(errTimeout);
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report connection error (lazyConnect = false)', function(done) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const dbConfig = {
|
2016-05-09 02:37:19 +00:00
|
|
|
host: '127.0.0.1',
|
|
|
|
port: 4,
|
|
|
|
lazyConnect: false,
|
|
|
|
};
|
2020-08-28 06:43:09 +00:00
|
|
|
const ds = new DataSource(mysqlConnector, dbConfig);
|
2016-05-09 02:37:19 +00:00
|
|
|
|
|
|
|
ds.on('error', function(err) {
|
|
|
|
err.message.should.containEql('ECONNREFUSED');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-06-13 16:34:31 +00:00
|
|
|
});
|
|
|
|
|
2014-02-13 00:57:06 +00:00
|
|
|
function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done) {
|
2016-08-10 18:41:03 +00:00
|
|
|
query('DROP DATABASE IF EXISTS ' + odb.settings.database, function(err) {
|
2014-02-13 00:57:06 +00:00
|
|
|
assert.ok(!err);
|
2016-08-10 18:41:03 +00:00
|
|
|
odb.disconnect(function() {
|
2017-07-24 16:48:26 +00:00
|
|
|
db = global.getDataSource({collation: test_set_collo,
|
|
|
|
createDatabase: true});
|
2014-02-13 00:57:06 +00:00
|
|
|
DummyModel = db.define('DummyModel', {string: String});
|
2016-08-10 18:41:03 +00:00
|
|
|
db.automigrate(function() {
|
2020-08-28 06:43:09 +00:00
|
|
|
const q = 'SELECT DEFAULT_COLLATION_NAME' +
|
2015-05-13 17:17:15 +00:00
|
|
|
' FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ' +
|
|
|
|
db.driver.escape(db.settings.database) + ' LIMIT 1';
|
2016-08-10 18:41:03 +00:00
|
|
|
db.connector.execute(q, function(err, r) {
|
2014-02-13 00:57:06 +00:00
|
|
|
assert.ok(!err);
|
2016-08-06 02:31:42 +00:00
|
|
|
should(r[0].DEFAULT_COLLATION_NAME).match(test_collo);
|
2016-08-10 18:41:03 +00:00
|
|
|
db.connector.execute('SHOW VARIABLES LIKE "character_set%"', function(err, r) {
|
2014-02-13 00:57:06 +00:00
|
|
|
assert.ok(!err);
|
2020-08-28 06:43:09 +00:00
|
|
|
let hit_all = 0;
|
|
|
|
for (const result in r) {
|
2014-02-13 00:57:06 +00:00
|
|
|
hit_all += matchResult(r[result], 'character_set_connection', test_set);
|
|
|
|
hit_all += matchResult(r[result], 'character_set_database', test_set);
|
|
|
|
hit_all += matchResult(r[result], 'character_set_results', test_set);
|
|
|
|
hit_all += matchResult(r[result], 'character_set_client', test_set);
|
|
|
|
}
|
|
|
|
assert.equal(hit_all, 4);
|
|
|
|
});
|
2016-08-10 18:41:03 +00:00
|
|
|
db.connector.execute('SHOW VARIABLES LIKE "collation%"', function(err, r) {
|
2014-02-13 00:57:06 +00:00
|
|
|
assert.ok(!err);
|
2020-08-28 06:43:09 +00:00
|
|
|
let hit_all = 0;
|
|
|
|
for (const result in r) {
|
2014-02-13 00:57:06 +00:00
|
|
|
hit_all += matchResult(r[result], 'collation_connection', test_set);
|
|
|
|
hit_all += matchResult(r[result], 'collation_database', test_set);
|
|
|
|
}
|
|
|
|
assert.equal(hit_all, 2);
|
|
|
|
done();
|
|
|
|
});
|
2013-06-13 16:34:31 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
2013-06-13 16:34:31 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
2013-06-13 16:34:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function matchResult(result, variable_name, match) {
|
2015-05-13 17:17:15 +00:00
|
|
|
if (result.Variable_name === variable_name) {
|
2014-02-13 00:57:06 +00:00
|
|
|
assert.ok(result.Value.match(match));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-06-13 16:34:31 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
function query(sql, cb) {
|
2015-05-13 17:17:15 +00:00
|
|
|
odb.connector.execute(sql, cb);
|
2020-08-28 06:43:09 +00:00
|
|
|
}
|
2013-06-13 16:34:31 +00:00
|
|
|
|
2016-03-24 17:20:56 +00:00
|
|
|
function generateURL(config) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const urlObj = {
|
2016-03-24 17:20:56 +00:00
|
|
|
protocol: 'mysql',
|
2016-08-05 00:48:26 +00:00
|
|
|
auth: config.username || '',
|
2016-03-24 17:20:56 +00:00
|
|
|
hostname: config.host,
|
|
|
|
pathname: config.database,
|
2016-08-10 18:41:03 +00:00
|
|
|
slashes: true,
|
2016-03-24 17:20:56 +00:00
|
|
|
};
|
2016-08-05 00:48:26 +00:00
|
|
|
if (config.password) {
|
|
|
|
urlObj.auth += ':' + config.password;
|
|
|
|
}
|
2020-08-28 06:43:09 +00:00
|
|
|
const formatedUrl = url.format(urlObj);
|
2016-03-24 17:20:56 +00:00
|
|
|
return formatedUrl;
|
|
|
|
}
|