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';
|
2020-08-28 06:43:09 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const async = require('async');
|
|
|
|
const platform = require('./helpers/platform');
|
|
|
|
const should = require('./init');
|
|
|
|
const Schema = require('loopback-datasource-juggler').Schema;
|
2013-06-04 21:34:23 +00:00
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
let db, UserData, StringData, NumberData, DateData, DefaultData, SimpleEmployee;
|
|
|
|
let mysqlVersion;
|
2014-02-13 00:57:06 +00:00
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
describe('migrations', function() {
|
2014-02-13 00:57:06 +00:00
|
|
|
before(setup);
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('should run migration', function(done) {
|
|
|
|
db.automigrate(function() {
|
2014-02-13 00:57:06 +00:00
|
|
|
done();
|
2013-06-04 21:34:23 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
2017-06-16 14:50:16 +00:00
|
|
|
it('allow user specified datatype on PK', function(done) {
|
|
|
|
query('describe SimpleEmployee', function(err, result) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(result);
|
|
|
|
result[0].Key.should.equal('PRI');
|
|
|
|
result[0].Type.should.equal('bigint(20)');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('UserData should have correct columns', function(done) {
|
|
|
|
getFields('UserData', function(err, fields) {
|
2016-09-28 20:32:38 +00:00
|
|
|
if (!fields) return done();
|
2015-05-13 17:17:15 +00:00
|
|
|
fields.should.be.eql({
|
2014-02-13 00:57:06 +00:00
|
|
|
id: {
|
|
|
|
Field: 'id',
|
|
|
|
Type: 'int(11)',
|
|
|
|
Null: 'NO',
|
|
|
|
Key: 'PRI',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: 'auto_increment'},
|
2014-02-13 00:57:06 +00:00
|
|
|
email: {
|
|
|
|
Field: 'email',
|
2016-10-26 15:32:13 +00:00
|
|
|
Type: 'varchar(255)',
|
2014-02-13 00:57:06 +00:00
|
|
|
Null: 'NO',
|
|
|
|
Key: 'MUL',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
2014-02-13 00:57:06 +00:00
|
|
|
name: {
|
|
|
|
Field: 'name',
|
2014-07-08 18:23:45 +00:00
|
|
|
Type: 'varchar(512)',
|
2014-02-13 00:57:06 +00:00
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
2014-02-13 00:57:06 +00:00
|
|
|
bio: {
|
|
|
|
Field: 'bio',
|
|
|
|
Type: 'text',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
2014-02-13 00:57:06 +00:00
|
|
|
birthDate: {
|
|
|
|
Field: 'birthDate',
|
|
|
|
Type: 'datetime',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
2014-02-13 00:57:06 +00:00
|
|
|
pendingPeriod: {
|
|
|
|
Field: 'pendingPeriod',
|
|
|
|
Type: 'int(11)',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
2014-02-13 00:57:06 +00:00
|
|
|
createdByAdmin: {
|
|
|
|
Field: 'createdByAdmin',
|
|
|
|
Type: 'tinyint(1)',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
done();
|
2013-06-04 21:34:23 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('UserData should have correct indexes', function(done) {
|
2014-12-03 22:10:21 +00:00
|
|
|
// Note: getIndexes truncates multi-key indexes to the first member.
|
|
|
|
// Hence index1 is correct.
|
2016-08-10 18:41:03 +00:00
|
|
|
getIndexes('UserData', function(err, fields) {
|
2016-09-28 20:32:38 +00:00
|
|
|
if (!fields) return done();
|
2016-08-05 02:42:28 +00:00
|
|
|
fields.should.match({
|
|
|
|
PRIMARY: {
|
|
|
|
Table: /UserData/i,
|
|
|
|
Non_unique: 0,
|
|
|
|
Key_name: 'PRIMARY',
|
|
|
|
Seq_in_index: 1,
|
|
|
|
Column_name: 'id',
|
|
|
|
Collation: 'A',
|
2016-08-10 01:33:34 +00:00
|
|
|
// XXX: this actually has more to do with whether the table existed or not and
|
|
|
|
// what kind of data is in it that MySQL has analyzed:
|
|
|
|
// https://dev.mysql.com/doc/refman/5.5/en/show-index.html
|
|
|
|
// Cardinality: /^5\.[567]/.test(mysqlVersion) ? 0 : null,
|
2016-08-05 02:42:28 +00:00
|
|
|
Sub_part: null,
|
|
|
|
Packed: null,
|
|
|
|
Null: '',
|
|
|
|
Index_type: 'BTREE',
|
2016-08-10 18:41:03 +00:00
|
|
|
Comment: ''},
|
2016-08-05 02:42:28 +00:00
|
|
|
email: {
|
|
|
|
Table: /UserData/i,
|
2014-02-13 00:57:06 +00:00
|
|
|
Non_unique: 1,
|
|
|
|
Key_name: 'email',
|
|
|
|
Seq_in_index: 1,
|
|
|
|
Column_name: 'email',
|
|
|
|
Collation: 'A',
|
2016-08-10 01:33:34 +00:00
|
|
|
// XXX: this actually has more to do with whether the table existed or not and
|
|
|
|
// what kind of data is in it that MySQL has analyzed:
|
|
|
|
// https://dev.mysql.com/doc/refman/5.5/en/show-index.html
|
|
|
|
// Cardinality: /^5\.[567]/.test(mysqlVersion) ? 0 : null,
|
2016-10-26 15:32:13 +00:00
|
|
|
Sub_part: null,
|
2014-02-13 00:57:06 +00:00
|
|
|
Packed: null,
|
|
|
|
Null: '',
|
|
|
|
Index_type: 'BTREE',
|
2016-08-10 18:41:03 +00:00
|
|
|
Comment: ''},
|
2016-08-05 02:42:28 +00:00
|
|
|
index0: {
|
|
|
|
Table: /UserData/i,
|
2014-02-13 00:57:06 +00:00
|
|
|
Non_unique: 1,
|
|
|
|
Key_name: 'index0',
|
|
|
|
Seq_in_index: 1,
|
|
|
|
Column_name: 'email',
|
|
|
|
Collation: 'A',
|
2016-08-10 01:33:34 +00:00
|
|
|
// XXX: this actually has more to do with whether the table existed or not and
|
|
|
|
// what kind of data is in it that MySQL has analyzed:
|
|
|
|
// https://dev.mysql.com/doc/refman/5.5/en/show-index.html
|
|
|
|
// Cardinality: /^5\.[567]/.test(mysqlVersion) ? 0 : null,
|
2016-10-26 15:32:13 +00:00
|
|
|
Sub_part: null,
|
2014-02-13 00:57:06 +00:00
|
|
|
Packed: null,
|
|
|
|
Null: '',
|
|
|
|
Index_type: 'BTREE',
|
2016-08-10 18:41:03 +00:00
|
|
|
Comment: ''},
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
done();
|
2013-06-04 21:34:23 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('StringData should have correct columns', function(done) {
|
|
|
|
getFields('StringData', function(err, fields) {
|
2015-05-13 17:17:15 +00:00
|
|
|
fields.should.be.eql({
|
2016-08-10 18:41:03 +00:00
|
|
|
idString: {Field: 'idString',
|
2014-04-08 05:16:26 +00:00
|
|
|
Type: 'varchar(255)',
|
|
|
|
Null: 'NO',
|
|
|
|
Key: 'PRI',
|
|
|
|
Default: null,
|
|
|
|
Extra: ''},
|
2016-08-10 18:41:03 +00:00
|
|
|
smallString: {Field: 'smallString',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'char(127)',
|
|
|
|
Null: 'NO',
|
|
|
|
Key: 'MUL',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
|
|
|
mediumString: {Field: 'mediumString',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'varchar(255)',
|
|
|
|
Null: 'NO',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
|
|
|
tinyText: {Field: 'tinyText',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'tinytext',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
|
|
|
giantJSON: {Field: 'giantJSON',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'longtext',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
|
|
|
text: {Field: 'text',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'varchar(1024)',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
done();
|
2013-06-04 21:34:23 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
2013-06-04 21:34:23 +00:00
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('NumberData should have correct columns', function(done) {
|
|
|
|
getFields('NumberData', function(err, fields) {
|
2015-05-13 17:17:15 +00:00
|
|
|
fields.should.be.eql({
|
2016-08-10 18:41:03 +00:00
|
|
|
id: {Field: 'id',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'int(11)',
|
|
|
|
Null: 'NO',
|
|
|
|
Key: 'PRI',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: 'auto_increment'},
|
|
|
|
number: {Field: 'number',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'decimal(10,3) unsigned',
|
|
|
|
Null: 'NO',
|
|
|
|
Key: 'MUL',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
|
|
|
tinyInt: {Field: 'tinyInt',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'tinyint(2)',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
|
|
|
mediumInt: {Field: 'mediumInt',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'mediumint(8) unsigned',
|
2014-12-03 22:10:21 +00:00
|
|
|
Null: 'NO',
|
2014-02-13 00:57:06 +00:00
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
|
|
|
floater: {Field: 'floater',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'double(14,6)',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
done();
|
2013-06-04 21:34:23 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
2013-06-04 21:34:23 +00:00
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('DateData should have correct columns', function(done) {
|
|
|
|
getFields('DateData', function(err, fields) {
|
2015-05-13 17:17:15 +00:00
|
|
|
fields.should.be.eql({
|
2016-08-10 18:41:03 +00:00
|
|
|
id: {Field: 'id',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'int(11)',
|
|
|
|
Null: 'NO',
|
|
|
|
Key: 'PRI',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: 'auto_increment'},
|
|
|
|
dateTime: {Field: 'dateTime',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'datetime',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
|
|
|
timestamp: {Field: 'timestamp',
|
2014-02-13 00:57:06 +00:00
|
|
|
Type: 'timestamp',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
2016-08-10 18:41:03 +00:00
|
|
|
Extra: ''},
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
done();
|
2013-06-04 21:34:23 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('should autoupdate', function(done) {
|
2016-10-26 15:32:13 +00:00
|
|
|
// With an install of MYSQL5.7 on windows, these queries `randomly` fail and raise errors
|
|
|
|
// especially with decimals, number and Date format.
|
|
|
|
if (platform.isWindows) {
|
|
|
|
return done();
|
|
|
|
}
|
2020-08-28 06:43:09 +00:00
|
|
|
const userExists = function(cb) {
|
2016-08-10 18:41:03 +00:00
|
|
|
query('SELECT * FROM UserData', function(err, res) {
|
2014-02-13 00:57:06 +00:00
|
|
|
cb(!err && res[0].email == 'test@example.com');
|
|
|
|
});
|
2016-08-10 18:41:03 +00:00
|
|
|
};
|
2013-06-04 21:34:23 +00:00
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
UserData.create({email: 'test@example.com'}, function(err, user) {
|
2014-02-13 00:57:06 +00:00
|
|
|
assert.ok(!err, 'Could not create user: ' + err);
|
2016-08-10 18:41:03 +00:00
|
|
|
userExists(function(yep) {
|
2014-02-13 00:57:06 +00:00
|
|
|
assert.ok(yep, 'User does not exist');
|
|
|
|
});
|
2016-08-10 18:41:03 +00:00
|
|
|
UserData.defineProperty('email', {type: String});
|
2014-12-03 22:10:21 +00:00
|
|
|
UserData.defineProperty('name', {type: String,
|
|
|
|
dataType: 'char', limit: 50});
|
|
|
|
UserData.defineProperty('newProperty', {type: Number, unsigned: true,
|
|
|
|
dataType: 'bigInt'});
|
|
|
|
// UserData.defineProperty('pendingPeriod', false);
|
|
|
|
// This will not work as expected.
|
2016-08-10 18:41:03 +00:00
|
|
|
db.autoupdate(function(err) {
|
|
|
|
getFields('UserData', function(err, fields) {
|
2014-02-13 00:57:06 +00:00
|
|
|
// change nullable for email
|
|
|
|
assert.equal(fields.email.Null, 'YES', 'Email does not allow null');
|
|
|
|
// change type of name
|
|
|
|
assert.equal(fields.name.Type, 'char(50)', 'Name is not char(50)');
|
|
|
|
// add new column
|
|
|
|
assert.ok(fields.newProperty, 'New column was not added');
|
|
|
|
if (fields.newProperty) {
|
2014-12-03 22:10:21 +00:00
|
|
|
assert.equal(fields.newProperty.Type, 'bigint(20) unsigned',
|
|
|
|
'New column type is not bigint(20) unsigned');
|
2014-02-13 00:57:06 +00:00
|
|
|
}
|
|
|
|
// drop column - will not happen.
|
2014-12-03 22:10:21 +00:00
|
|
|
// assert.ok(!fields.pendingPeriod,
|
|
|
|
// 'Did not drop column pendingPeriod');
|
2014-02-13 00:57:06 +00:00
|
|
|
// user still exists
|
2016-08-10 18:41:03 +00:00
|
|
|
userExists(function(yep) {
|
2014-02-13 00:57:06 +00:00
|
|
|
assert.ok(yep, 'User does not exist');
|
|
|
|
done();
|
|
|
|
});
|
2013-06-04 21:34:23 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
2013-06-04 21:34:23 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('should check actuality of dataSource', function(done) {
|
2016-10-26 15:32:13 +00:00
|
|
|
// With an install of MYSQL5.7 on windows, these queries `randomly` fail and raise errors
|
|
|
|
// with date, number and decimal format
|
|
|
|
if (platform.isWindows) {
|
|
|
|
return done();
|
|
|
|
}
|
2014-02-13 00:57:06 +00:00
|
|
|
// 'drop column'
|
2016-08-10 18:41:03 +00:00
|
|
|
UserData.dataSource.isActual(function(err, ok) {
|
2014-02-13 00:57:06 +00:00
|
|
|
assert.ok(ok, 'dataSource is not actual (should be)');
|
|
|
|
UserData.defineProperty('essay', {type: Schema.Text});
|
|
|
|
// UserData.defineProperty('email', false); Can't undefine currently.
|
2016-08-10 18:41:03 +00:00
|
|
|
UserData.dataSource.isActual(function(err, ok) {
|
2014-02-13 00:57:06 +00:00
|
|
|
assert.ok(!ok, 'dataSource is actual (shouldn\t be)');
|
2016-08-10 18:41:03 +00:00
|
|
|
done();
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
2013-06-06 17:09:39 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
2016-10-26 15:32:13 +00:00
|
|
|
// In MySQL 5.6/5.7 Out of range values are rejected.
|
|
|
|
// Reference: http://dev.mysql.com/doc/refman/5.7/en/integer-types.html
|
|
|
|
it('allows numbers with decimals', function(done) {
|
|
|
|
NumberData.create(
|
|
|
|
{number: 1.1234567, tinyInt: 127, mediumInt: 16777215, floater: 12345678.123456},
|
|
|
|
function(err, obj) {
|
|
|
|
if (err) return (err);
|
|
|
|
NumberData.findById(obj.id, function(err, found) {
|
|
|
|
assert.equal(found.number, 1.123);
|
|
|
|
assert.equal(found.tinyInt, 127);
|
|
|
|
assert.equal(found.mediumInt, 16777215);
|
|
|
|
assert.equal(found.floater, 12345678.123456);
|
|
|
|
done();
|
|
|
|
});
|
2020-08-28 06:43:09 +00:00
|
|
|
},
|
|
|
|
);
|
2016-10-26 15:32:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Reference: http://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html
|
|
|
|
it('rejects out-of-range and overflow values', function(done) {
|
|
|
|
async.series([
|
|
|
|
function(next) {
|
|
|
|
NumberData.create({number: 1.1234567, tinyInt: 128, mediumInt: 16777215}, function(err, obj) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'ER_WARN_DATA_OUT_OF_RANGE');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}, function(next) {
|
|
|
|
NumberData.create({number: 1.1234567, mediumInt: 16777215 + 1}, function(err, obj) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'ER_WARN_DATA_OUT_OF_RANGE');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}, function(next) {
|
2017-07-24 16:48:26 +00:00
|
|
|
// Minimum value for unsigned mediumInt is 0
|
2016-10-26 15:32:13 +00:00
|
|
|
NumberData.create({number: 1.1234567, mediumInt: -8388608}, function(err, obj) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'ER_WARN_DATA_OUT_OF_RANGE');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}, function(next) {
|
|
|
|
NumberData.create({number: 1.1234567, tinyInt: -129, mediumInt: 0}, function(err, obj) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'ER_WARN_DATA_OUT_OF_RANGE');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
2017-08-09 00:40:40 +00:00
|
|
|
it('should take on database default CURRENT_TIMESTAMP, boolean 0 and pending string for columns', function(done) {
|
|
|
|
DefaultData.create({}, function(err, obj) {
|
|
|
|
assert.ok(!err);
|
|
|
|
assert.ok(obj);
|
2020-08-28 06:43:09 +00:00
|
|
|
const now = new Date();
|
2017-08-09 00:40:40 +00:00
|
|
|
DefaultData.findById(obj.id, function(err, found) {
|
|
|
|
now.setSeconds(0);
|
|
|
|
found.dateTime.setSeconds(0);
|
|
|
|
found.timestamp.setSeconds(0);
|
|
|
|
|
|
|
|
assert.equal(found.dateTime.toGMTString(), now.toGMTString());
|
|
|
|
assert.equal(found.timestamp.toGMTString(), now.toGMTString());
|
|
|
|
assert.equal(found.isAdmin, '0');
|
|
|
|
assert.equal(found.number, 256);
|
|
|
|
assert.equal(found.data, null);
|
|
|
|
assert.equal(found.text, null);
|
|
|
|
assert.equal(found.status, 'pending');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('DefaultData should have correct columns', function(done) {
|
|
|
|
getFields('DefaultData', function(err, fields) {
|
|
|
|
fields.should.be.eql({
|
|
|
|
id: {Field: 'id',
|
|
|
|
Type: 'int(11)',
|
|
|
|
Null: 'NO',
|
|
|
|
Key: 'PRI',
|
|
|
|
Default: null,
|
|
|
|
Extra: 'auto_increment'},
|
|
|
|
dateTime: {Field: 'dateTime',
|
|
|
|
Type: 'datetime',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: 'CURRENT_TIMESTAMP',
|
|
|
|
Extra: ''},
|
|
|
|
timestamp: {Field: 'timestamp',
|
|
|
|
Type: 'timestamp',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: 'CURRENT_TIMESTAMP',
|
|
|
|
Extra: ''},
|
|
|
|
isAdmin: {Field: 'isAdmin',
|
|
|
|
Type: 'tinyint(1)',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: '0',
|
|
|
|
Extra: ''},
|
|
|
|
number: {Field: 'number',
|
|
|
|
Type: 'int(10) unsigned',
|
|
|
|
Null: 'NO',
|
|
|
|
Key: 'MUL',
|
|
|
|
Default: '256',
|
|
|
|
Extra: ''},
|
|
|
|
data: {Field: 'data',
|
|
|
|
Type: 'longtext',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
|
|
|
Extra: ''},
|
|
|
|
text: {Field: 'text',
|
|
|
|
Type: 'varchar(1024)',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: null,
|
|
|
|
Extra: ''},
|
|
|
|
status: {Field: 'status',
|
|
|
|
Type: 'varchar(512)',
|
|
|
|
Null: 'YES',
|
|
|
|
Key: '',
|
|
|
|
Default: 'pending',
|
|
|
|
Extra: ''},
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('should allow both kinds of date columns', function(done) {
|
2014-02-13 00:57:06 +00:00
|
|
|
DateData.create({
|
|
|
|
dateTime: new Date('Aug 9 1996 07:47:33 GMT'),
|
2016-08-10 18:41:03 +00:00
|
|
|
timestamp: new Date('Sep 22 2007 17:12:22 GMT'),
|
|
|
|
}, function(err, obj) {
|
2014-02-13 00:57:06 +00:00
|
|
|
assert.ok(!err);
|
|
|
|
assert.ok(obj);
|
2016-08-10 18:41:03 +00:00
|
|
|
DateData.findById(obj.id, function(err, found) {
|
2014-12-03 22:10:21 +00:00
|
|
|
assert.equal(found.dateTime.toGMTString(),
|
|
|
|
'Fri, 09 Aug 1996 07:47:33 GMT');
|
|
|
|
assert.equal(found.timestamp.toGMTString(),
|
|
|
|
'Sat, 22 Sep 2007 17:12:22 GMT');
|
2014-02-13 00:57:06 +00:00
|
|
|
done();
|
|
|
|
});
|
2013-06-04 21:34:23 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
2016-10-26 15:32:13 +00:00
|
|
|
// InMySQL5.7, DATETIME supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
|
|
|
|
// TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC
|
|
|
|
// Reference: http://dev.mysql.com/doc/refman/5.7/en/datetime.html
|
|
|
|
// Out of range values are set to null in windows but rejected elsewhere
|
|
|
|
// the next example is designed for windows while the following 2 are for other platforms
|
2016-08-30 12:58:08 +00:00
|
|
|
it('should map zero dateTime into null', function(done) {
|
2016-10-26 15:32:13 +00:00
|
|
|
if (!platform.isWindows) {
|
2016-09-28 20:32:38 +00:00
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
2016-07-26 11:39:52 +00:00
|
|
|
query('INSERT INTO `DateData` ' +
|
|
|
|
'(`dateTime`, `timestamp`) ' +
|
|
|
|
'VALUES("0000-00-00 00:00:00", "0000-00-00 00:00:00") ',
|
2017-07-24 16:48:26 +00:00
|
|
|
function(err, ret) {
|
|
|
|
should.not.exists(err);
|
|
|
|
DateData.findById(ret.insertId, function(err, dateData) {
|
|
|
|
should(dateData.dateTime)
|
|
|
|
.be.null();
|
|
|
|
should(dateData.timestamp)
|
|
|
|
.be.null();
|
|
|
|
done();
|
2016-07-26 11:39:52 +00:00
|
|
|
});
|
2017-07-24 16:48:26 +00:00
|
|
|
});
|
2016-07-26 11:39:52 +00:00
|
|
|
});
|
2016-10-26 15:32:13 +00:00
|
|
|
|
|
|
|
it('rejects out of range datetime', function(done) {
|
|
|
|
if (platform.isWindows) {
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
query('INSERT INTO `DateData` ' +
|
|
|
|
'(`dateTime`, `timestamp`) ' +
|
|
|
|
'VALUES("0000-00-00 00:00:00", "0000-00-00 00:00:00") ', function(err) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const errMsg = 'ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value: ' +
|
2016-10-26 15:32:13 +00:00
|
|
|
'\'0000-00-00 00:00:00\' for column \'dateTime\' at row 1';
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.message, errMsg);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects out of range timestamp', function(done) {
|
|
|
|
if (platform.isWindows) {
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
query('INSERT INTO `DateData` ' +
|
|
|
|
'(`dateTime`, `timestamp`) ' +
|
|
|
|
'VALUES("1000-01-01 00:00:00", "0000-00-00 00:00:00") ', function(err) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const errMsg = 'ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value: ' +
|
2016-10-26 15:32:13 +00:00
|
|
|
'\'0000-00-00 00:00:00\' for column \'timestamp\' at row 1';
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.message, errMsg);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
|
2014-09-11 19:23:32 +00:00
|
|
|
it('should report errors for automigrate', function() {
|
|
|
|
db.automigrate('XYZ', function(err) {
|
|
|
|
assert(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report errors for autoupdate', function() {
|
|
|
|
db.autoupdate('XYZ', function(err) {
|
|
|
|
assert(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
it('should disconnect when done', function(done) {
|
2014-02-13 00:57:06 +00:00
|
|
|
db.disconnect();
|
2014-04-08 05:16:26 +00:00
|
|
|
done();
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
2013-06-04 21:34:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function setup(done) {
|
2014-02-13 00:57:06 +00:00
|
|
|
require('./init.js');
|
2013-06-04 21:34:23 +00:00
|
|
|
|
2017-07-24 16:48:26 +00:00
|
|
|
db = global.getSchema();
|
2014-02-13 00:57:06 +00:00
|
|
|
|
|
|
|
UserData = db.define('UserData', {
|
2016-08-10 18:41:03 +00:00
|
|
|
email: {type: String, null: false, index: true},
|
2014-02-13 00:57:06 +00:00
|
|
|
name: String,
|
|
|
|
bio: Schema.Text,
|
|
|
|
birthDate: Date,
|
|
|
|
pendingPeriod: Number,
|
|
|
|
createdByAdmin: Boolean,
|
2016-08-10 18:41:03 +00:00
|
|
|
}, {indexes: {
|
2014-02-13 00:57:06 +00:00
|
|
|
index0: {
|
2016-08-10 18:41:03 +00:00
|
|
|
columns: 'email, createdByAdmin',
|
|
|
|
},
|
|
|
|
},
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
StringData = db.define('StringData', {
|
2014-04-08 05:16:26 +00:00
|
|
|
idString: {type: String, id: true},
|
2014-12-03 22:10:21 +00:00
|
|
|
smallString: {type: String, null: false, index: true,
|
|
|
|
dataType: 'char', limit: 127},
|
2014-02-13 00:57:06 +00:00
|
|
|
mediumString: {type: String, null: false, dataType: 'varchar', limit: 255},
|
|
|
|
tinyText: {type: String, dataType: 'tinyText'},
|
|
|
|
giantJSON: {type: Schema.JSON, dataType: 'longText'},
|
2016-08-10 18:41:03 +00:00
|
|
|
text: {type: Schema.Text, dataType: 'varchar', limit: 1024},
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
NumberData = db.define('NumberData', {
|
2014-12-03 22:10:21 +00:00
|
|
|
number: {type: Number, null: false, index: true, unsigned: true,
|
|
|
|
dataType: 'decimal', precision: 10, scale: 3},
|
2014-02-13 00:57:06 +00:00
|
|
|
tinyInt: {type: Number, dataType: 'tinyInt', display: 2},
|
2014-12-03 22:10:21 +00:00
|
|
|
mediumInt: {type: Number, dataType: 'mediumInt', unsigned: true,
|
|
|
|
required: true},
|
2016-08-10 18:41:03 +00:00
|
|
|
floater: {type: Number, dataType: 'double', precision: 14, scale: 6},
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
2017-08-09 00:40:40 +00:00
|
|
|
DefaultData = db.define('DefaultData', {
|
|
|
|
dateTime: {type: Date, dataType: 'datetime', mysql: {default: 'now'}},
|
|
|
|
timestamp: {type: Date, dataType: 'timestamp', mysql: {default: 'CURRENT_TIMESTAMP'}},
|
|
|
|
isAdmin: {type: Boolean, mysql: {default: '0'}},
|
|
|
|
number: {type: Number, null: false, index: true, unsigned: true,
|
|
|
|
dataType: 'int', mysql: {default: 256}},
|
|
|
|
data: {type: Schema.JSON, dataType: 'longText', mysql: {default: 'Not Supported'}},
|
|
|
|
text: {type: Schema.Text, dataType: 'varchar', limit: 1024, mysql: {default: 'Not Supported'}},
|
|
|
|
status: {type: String, dataType: 'varchar', mysql: {default: 'pending'}},
|
|
|
|
});
|
|
|
|
|
2014-02-13 00:57:06 +00:00
|
|
|
DateData = db.define('DateData', {
|
|
|
|
dateTime: {type: Date, dataType: 'datetime'},
|
2016-08-10 18:41:03 +00:00
|
|
|
timestamp: {type: Date, dataType: 'timestamp'},
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
|
2017-06-16 14:50:16 +00:00
|
|
|
SimpleEmployee = db.define('SimpleEmployee', {
|
|
|
|
eId: {type: Number, generated: true, id: true, mysql: {dataType: 'bigint', dataLength: 20}},
|
|
|
|
name: {type: String},
|
|
|
|
});
|
|
|
|
|
2016-08-05 02:20:42 +00:00
|
|
|
query('SELECT VERSION()', function(err, res) {
|
|
|
|
mysqlVersion = res && res[0] && res[0]['VERSION()'];
|
|
|
|
blankDatabase(db, done);
|
|
|
|
});
|
2013-06-04 21:34:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
function query(sql, cb) {
|
2015-05-13 17:17:15 +00:00
|
|
|
db.adapter.execute(sql, cb);
|
2020-08-28 06:43:09 +00:00
|
|
|
}
|
2013-06-04 21:34:23 +00:00
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
function blankDatabase(db, cb) {
|
|
|
|
const dbn = db.settings.database;
|
|
|
|
const cs = db.settings.charset;
|
|
|
|
const co = db.settings.collation;
|
2016-08-10 18:41:03 +00:00
|
|
|
query('DROP DATABASE IF EXISTS ' + dbn, function(err) {
|
2020-08-28 06:43:09 +00:00
|
|
|
let q = 'CREATE DATABASE ' + dbn;
|
2014-02-13 00:57:06 +00:00
|
|
|
if (cs) {
|
|
|
|
q += ' CHARACTER SET ' + cs;
|
|
|
|
}
|
|
|
|
if (co) {
|
|
|
|
q += ' COLLATE ' + co;
|
|
|
|
}
|
2016-08-10 18:41:03 +00:00
|
|
|
query(q, function(err) {
|
2014-02-13 00:57:06 +00:00
|
|
|
query('USE ' + dbn, cb);
|
2013-06-04 21:34:23 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
2020-08-28 06:43:09 +00:00
|
|
|
}
|
2013-06-04 21:34:23 +00:00
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
function getFields(model, cb) {
|
2016-08-10 18:41:03 +00:00
|
|
|
query('SHOW FIELDS FROM ' + model, function(err, res) {
|
2014-02-13 00:57:06 +00:00
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
2020-08-28 06:43:09 +00:00
|
|
|
const fields = {};
|
2016-08-10 18:41:03 +00:00
|
|
|
res.forEach(function(field) {
|
2014-02-13 00:57:06 +00:00
|
|
|
fields[field.Field] = field;
|
|
|
|
});
|
2019-06-28 14:54:45 +00:00
|
|
|
// The returned data are in arrays of type `RowDataPacket`,
|
|
|
|
// which are not objects.
|
|
|
|
cb(err, JSON.parse(JSON.stringify(fields)));
|
2014-02-13 00:57:06 +00:00
|
|
|
}
|
|
|
|
});
|
2020-08-28 06:43:09 +00:00
|
|
|
}
|
2013-06-04 21:34:23 +00:00
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
function getIndexes(model, cb) {
|
2016-08-10 18:41:03 +00:00
|
|
|
query('SHOW INDEXES FROM ' + model, function(err, res) {
|
2014-02-13 00:57:06 +00:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
cb(err);
|
|
|
|
} else {
|
2020-08-28 06:43:09 +00:00
|
|
|
const indexes = {};
|
2014-02-13 00:57:06 +00:00
|
|
|
// Note: this will only show the first key of compound keys
|
2016-08-10 18:41:03 +00:00
|
|
|
res.forEach(function(index) {
|
2014-02-13 00:57:06 +00:00
|
|
|
if (parseInt(index.Seq_in_index, 10) == 1) {
|
2016-08-10 18:41:03 +00:00
|
|
|
indexes[index.Key_name] = index;
|
2013-06-04 21:34:23 +00:00
|
|
|
}
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
cb(err, indexes);
|
|
|
|
}
|
|
|
|
});
|
2020-08-28 06:43:09 +00:00
|
|
|
}
|