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() {
|
|
|
|
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',
|
|
|
|
},
|
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,
|
|
|
|
},
|
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',
|
|
|
|
},
|
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,
|
|
|
|
},
|
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) {
|
2014-12-05 19:48:34 +00:00
|
|
|
assert.equal(props.length, 4);
|
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');
|
|
|
|
|
|
|
|
ds.createModel(schema_v2.name, schema_v2.properties, schema_v2.options);
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
ds.autoupdate(function(err, result) {
|
|
|
|
ds.discoverModelProperties('customer_test', function(err, props) {
|
2014-12-05 19:48:34 +00:00
|
|
|
assert.equal(props.length, 4);
|
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(names[0], 'id');
|
|
|
|
assert.equal(names[1], 'email');
|
|
|
|
assert.equal(names[2], 'firstName');
|
|
|
|
assert.equal(names[3], 'lastName');
|
|
|
|
done(err, result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|