2016-05-03 23:52:03 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// 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';
|
2014-12-05 19:48:34 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
require('./init');
|
|
|
|
var ds;
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
before(function() {
|
2014-12-05 19:48:34 +00:00
|
|
|
ds = getDataSource();
|
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
describe('MySQL connector', function() {
|
2016-08-30 12:58:08 +00:00
|
|
|
before(function() {
|
2016-08-29 05:07:03 +00:00
|
|
|
setupAltColNameData();
|
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('should auto migrate/update tables', function(done) {
|
2014-12-05 19:48:34 +00:00
|
|
|
var schema_v1 =
|
2016-08-10 18:41:03 +00:00
|
|
|
{
|
|
|
|
'name': 'CustomerTest',
|
|
|
|
'options': {
|
|
|
|
'idInjection': false,
|
|
|
|
'mysql': {
|
|
|
|
'schema': 'myapp_test',
|
|
|
|
'table': 'customer_test',
|
|
|
|
},
|
2016-08-03 15:20:54 +00:00
|
|
|
'indexes': {
|
|
|
|
'name_index': {
|
|
|
|
'keys': {
|
|
|
|
'name': 1,
|
|
|
|
},
|
|
|
|
'options': {
|
|
|
|
'unique': true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-12-05 19:48:34 +00:00
|
|
|
},
|
2016-08-10 18:41:03 +00:00
|
|
|
'properties': {
|
|
|
|
'id': {
|
|
|
|
'type': 'String',
|
|
|
|
'length': 20,
|
|
|
|
'id': 1,
|
|
|
|
},
|
|
|
|
'name': {
|
|
|
|
'type': 'String',
|
|
|
|
'required': false,
|
|
|
|
'length': 40,
|
|
|
|
},
|
|
|
|
'email': {
|
|
|
|
'type': 'String',
|
|
|
|
'required': true,
|
|
|
|
'length': 40,
|
|
|
|
},
|
|
|
|
'age': {
|
|
|
|
'type': 'Number',
|
|
|
|
'required': false,
|
|
|
|
},
|
2016-12-09 23:35:53 +00:00
|
|
|
'discount': {
|
|
|
|
'type': 'Number',
|
|
|
|
'required': false,
|
|
|
|
'dataType': 'decimal',
|
|
|
|
'precision': 10,
|
|
|
|
'scale': 2,
|
|
|
|
'mysql': {
|
|
|
|
'columnName': 'customer_discount',
|
|
|
|
'dataType': 'decimal',
|
|
|
|
'dataPrecision': 10,
|
|
|
|
'dataScale': 2,
|
|
|
|
},
|
|
|
|
},
|
2014-12-05 19:48:34 +00:00
|
|
|
},
|
2016-08-10 18:41:03 +00:00
|
|
|
};
|
2014-12-05 19:48:34 +00:00
|
|
|
|
|
|
|
var schema_v2 =
|
2016-08-10 18:41:03 +00:00
|
|
|
{
|
|
|
|
'name': 'CustomerTest',
|
|
|
|
'options': {
|
|
|
|
'idInjection': false,
|
|
|
|
'mysql': {
|
|
|
|
'schema': 'myapp_test',
|
|
|
|
'table': 'customer_test',
|
|
|
|
},
|
2016-08-03 15:20:54 +00:00
|
|
|
'indexes': {
|
|
|
|
'updated_name_index': {
|
|
|
|
'keys': {
|
|
|
|
'firstName': 1,
|
|
|
|
'lastName': -1,
|
|
|
|
},
|
|
|
|
'options': {
|
|
|
|
'unique': true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-12-05 19:48:34 +00:00
|
|
|
},
|
2016-08-10 18:41:03 +00:00
|
|
|
'properties': {
|
|
|
|
'id': {
|
|
|
|
'type': 'String',
|
|
|
|
'length': 20,
|
|
|
|
'id': 1,
|
|
|
|
},
|
|
|
|
'email': {
|
|
|
|
'type': 'String',
|
|
|
|
'required': false,
|
|
|
|
'length': 60,
|
|
|
|
'mysql': {
|
|
|
|
'columnName': 'email',
|
|
|
|
'dataType': 'varchar',
|
|
|
|
'dataLength': 60,
|
|
|
|
'nullable': 'YES',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'firstName': {
|
|
|
|
'type': 'String',
|
|
|
|
'required': false,
|
|
|
|
'length': 40,
|
|
|
|
},
|
|
|
|
'lastName': {
|
|
|
|
'type': 'String',
|
|
|
|
'required': false,
|
|
|
|
'length': 40,
|
|
|
|
},
|
2016-12-09 23:35:53 +00:00
|
|
|
// remove age
|
|
|
|
// change data type details with column name
|
|
|
|
'discount': {
|
|
|
|
'type': 'Number',
|
|
|
|
'required': false,
|
|
|
|
'dataType': 'decimal',
|
|
|
|
'precision': 12,
|
|
|
|
'scale': 5,
|
|
|
|
'mysql': {
|
|
|
|
'columnName': 'customer_discount',
|
|
|
|
'dataType': 'decimal',
|
|
|
|
'dataPrecision': 12,
|
|
|
|
'dataScale': 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// add new column with column name
|
|
|
|
'address': {
|
|
|
|
'type': 'String',
|
|
|
|
'required': false,
|
|
|
|
'length': 10,
|
|
|
|
'mysql': {
|
|
|
|
'columnName': 'customer_address',
|
|
|
|
'dataType': 'varchar',
|
|
|
|
'length': 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// add new column with index & column name
|
|
|
|
'code': {
|
|
|
|
'type': 'String',
|
|
|
|
'required': true,
|
|
|
|
'length': 12,
|
|
|
|
'index': {
|
|
|
|
unique: true,
|
|
|
|
},
|
|
|
|
'mysql': {
|
|
|
|
'columnName': 'customer_code',
|
|
|
|
'dataType': 'varchar',
|
|
|
|
'length': 12,
|
|
|
|
},
|
|
|
|
},
|
2014-12-05 19:48:34 +00:00
|
|
|
},
|
2016-08-10 18:41:03 +00:00
|
|
|
};
|
2014-12-05 19:48:34 +00:00
|
|
|
|
|
|
|
ds.createModel(schema_v1.name, schema_v1.properties, schema_v1.options);
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
ds.automigrate(function() {
|
|
|
|
ds.discoverModelProperties('customer_test', function(err, props) {
|
2016-12-09 23:35:53 +00:00
|
|
|
assert.equal(props.length, 5);
|
2016-08-10 18:41:03 +00:00
|
|
|
var names = props.map(function(p) {
|
2014-12-05 19:48:34 +00:00
|
|
|
return p.columnName;
|
|
|
|
});
|
|
|
|
assert.equal(props[0].nullable, 'N');
|
|
|
|
assert.equal(props[1].nullable, 'Y');
|
|
|
|
assert.equal(props[2].nullable, 'N');
|
|
|
|
assert.equal(props[3].nullable, 'Y');
|
|
|
|
assert.equal(names[0], 'id');
|
|
|
|
assert.equal(names[1], 'name');
|
|
|
|
assert.equal(names[2], 'email');
|
|
|
|
assert.equal(names[3], 'age');
|
2016-12-09 23:35:53 +00:00
|
|
|
assert.equal(names[4], 'customer_discount');
|
2014-12-05 19:48:34 +00:00
|
|
|
|
2016-08-03 15:20:54 +00:00
|
|
|
ds.connector.execute('SHOW INDEXES FROM customer_test', function(err, indexes) {
|
|
|
|
if (err) return done (err);
|
|
|
|
assert(indexes);
|
|
|
|
assert(indexes.length.should.be.above(1));
|
|
|
|
assert.equal(indexes[1].Key_name, 'name_index');
|
|
|
|
assert.equal(indexes[1].Non_unique, 0);
|
|
|
|
ds.createModel(schema_v2.name, schema_v2.properties, schema_v2.options);
|
|
|
|
ds.autoupdate(function(err, result) {
|
|
|
|
if (err) return done (err);
|
|
|
|
ds.discoverModelProperties('customer_test', function(err, props) {
|
|
|
|
if (err) return done (err);
|
2016-12-09 23:35:53 +00:00
|
|
|
assert.equal(props.length, 7);
|
2016-08-03 15:20:54 +00:00
|
|
|
var names = props.map(function(p) {
|
|
|
|
return p.columnName;
|
|
|
|
});
|
|
|
|
assert.equal(names[0], 'id');
|
|
|
|
assert.equal(names[1], 'email');
|
2016-12-09 23:35:53 +00:00
|
|
|
assert.equal(names[2], 'customer_discount');
|
|
|
|
assert.equal(names[3], 'firstName');
|
|
|
|
assert.equal(names[4], 'lastName');
|
|
|
|
assert.equal(names[5], 'customer_address');
|
|
|
|
assert.equal(names[6], 'customer_code');
|
2016-08-03 15:20:54 +00:00
|
|
|
ds.connector.execute('SHOW INDEXES FROM customer_test', function(err, updatedindexes) {
|
|
|
|
if (err) return done (err);
|
|
|
|
assert(updatedindexes);
|
2016-12-09 23:35:53 +00:00
|
|
|
assert(updatedindexes.length.should.be.above(3));
|
|
|
|
assert.equal(updatedindexes[1].Key_name, 'customer_code');
|
2016-08-03 15:20:54 +00:00
|
|
|
assert.equal(updatedindexes[2].Key_name, 'updated_name_index');
|
2016-12-09 23:35:53 +00:00
|
|
|
assert.equal(updatedindexes[3].Key_name, 'updated_name_index');
|
2016-08-03 15:20:54 +00:00
|
|
|
//Mysql supports only index sorting in ascending; DESC is ignored
|
|
|
|
assert.equal(updatedindexes[1].Collation, 'A');
|
|
|
|
assert.equal(updatedindexes[2].Collation, 'A');
|
2016-12-09 23:35:53 +00:00
|
|
|
assert.equal(updatedindexes[3].Collation, 'A');
|
2016-08-03 15:20:54 +00:00
|
|
|
assert.equal(updatedindexes[1].Non_unique, 0);
|
2016-12-09 23:35:53 +00:00
|
|
|
assert.equal(updatedindexes[2].Non_unique, 0);
|
|
|
|
assert.equal(updatedindexes[3].Non_unique, 0);
|
2016-08-03 15:20:54 +00:00
|
|
|
done(err, result);
|
|
|
|
});
|
2014-12-05 19:48:34 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-29 05:07:03 +00:00
|
|
|
function setupAltColNameData() {
|
2016-08-30 12:58:08 +00:00
|
|
|
var schema = {
|
|
|
|
name: 'ColRenameTest',
|
|
|
|
options: {
|
|
|
|
idInjection: false,
|
|
|
|
mysql: {
|
|
|
|
schema: 'myapp_test',
|
|
|
|
table: 'col_rename_test',
|
|
|
|
},
|
2016-08-29 05:07:03 +00:00
|
|
|
},
|
2016-08-30 12:58:08 +00:00
|
|
|
properties: {
|
|
|
|
firstName: {
|
|
|
|
type: 'String',
|
|
|
|
required: false,
|
|
|
|
length: 40,
|
|
|
|
mysql: {
|
|
|
|
columnName: 'first_name',
|
|
|
|
dataType: 'varchar',
|
|
|
|
dataLength: 40,
|
|
|
|
},
|
2016-08-29 05:07:03 +00:00
|
|
|
},
|
2016-08-30 12:58:08 +00:00
|
|
|
lastName: {
|
|
|
|
type: 'String',
|
|
|
|
required: false,
|
|
|
|
length: 40,
|
|
|
|
},
|
|
|
|
},
|
2016-08-29 05:07:03 +00:00
|
|
|
};
|
|
|
|
ds.createModel(schema.name, schema.properties, schema.options);
|
|
|
|
}
|
|
|
|
|
2014-12-05 19:48:34 +00:00
|
|
|
it('should report errors for automigrate', function(done) {
|
|
|
|
ds.automigrate('XYZ', function(err) {
|
|
|
|
assert(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report errors for autoupdate', function(done) {
|
|
|
|
ds.autoupdate('XYZ', function(err) {
|
|
|
|
assert(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-08-29 05:07:03 +00:00
|
|
|
|
2016-08-30 12:58:08 +00:00
|
|
|
it('"mysql.columnName" is updated with correct name on create table', function(done) {
|
2016-08-29 05:07:03 +00:00
|
|
|
// first autoupdate call uses create table
|
|
|
|
verifyMysqlColumnNameAutoupdate(done);
|
|
|
|
});
|
|
|
|
|
2016-08-30 12:58:08 +00:00
|
|
|
it('"mysql.columnName" is updated without changing column name on alter table', function(done) {
|
2016-08-29 05:07:03 +00:00
|
|
|
// second autoupdate call uses alter table
|
|
|
|
verifyMysqlColumnNameAutoupdate(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
function verifyMysqlColumnNameAutoupdate(done) {
|
2016-08-30 12:58:08 +00:00
|
|
|
ds.autoupdate('ColRenameTest', function(err) {
|
|
|
|
ds.discoverModelProperties('col_rename_test', function(err, props) {
|
2016-08-29 05:07:03 +00:00
|
|
|
assert.equal(props[0].columnName, 'first_name');
|
|
|
|
assert.equal(props[1].columnName, 'lastName');
|
|
|
|
assert.equal(props.length, 2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-12-05 19:48:34 +00:00
|
|
|
});
|