Merge pull request #21 from strongloop/feature/fix-string-id
Fix the table generation for string ids
This commit is contained in:
commit
99f9151d4f
35
lib/mysql.js
35
lib/mysql.js
|
@ -6,6 +6,8 @@ var mysql = require('mysql');
|
|||
var juggler = require('loopback-datasource-juggler');
|
||||
var EnumFactory = require('./enumFactory').EnumFactory;
|
||||
|
||||
var debug = require('debug')('loopback:connector:mysql');
|
||||
|
||||
/**
|
||||
* @module loopback-connector-mysql
|
||||
*
|
||||
|
@ -41,7 +43,6 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
|
|||
user: s.username || s.user,
|
||||
password: s.password,
|
||||
timezone: s.timezone,
|
||||
debug: s.debug,
|
||||
socketPath: s.socketPath,
|
||||
charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd.
|
||||
supportBigNumbers: s.supportBigNumbers,
|
||||
|
@ -61,8 +62,8 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
|
|||
dataSource.connecting = false;
|
||||
});
|
||||
|
||||
if (s.debug) {
|
||||
console.log('Settings: ', s);
|
||||
if (debug.enabled) {
|
||||
debug('Settings: %j', s);
|
||||
}
|
||||
|
||||
dataSource.connector = new MySQL(dataSource.client, s);
|
||||
|
@ -110,12 +111,12 @@ MySQL.prototype.query = function (sql, callback) {
|
|||
}
|
||||
var client = this.client;
|
||||
var time = Date.now();
|
||||
var debug = this.settings.debug;
|
||||
var debugEnabled = debug.enabled;
|
||||
var db = this.settings.database;
|
||||
var log = this.log;
|
||||
if (typeof callback !== 'function') throw new Error('callback should be a function');
|
||||
if (debug) {
|
||||
console.log('SQL:', sql);
|
||||
if (debugEnabled) {
|
||||
debug('SQL: %s', sql);
|
||||
}
|
||||
|
||||
function releaseConnectionAndCallback(connection, err, result) {
|
||||
|
@ -125,13 +126,15 @@ MySQL.prototype.query = function (sql, callback) {
|
|||
|
||||
function runQuery(connection) {
|
||||
connection.query(sql, function (err, data) {
|
||||
if (debug) {
|
||||
if (debugEnabled) {
|
||||
if (err) {
|
||||
console.error('Error:', err);
|
||||
console.error('Error: ', err);
|
||||
}
|
||||
console.log('Data:', data);
|
||||
debug('Data: ', data);
|
||||
}
|
||||
if (log) {
|
||||
log(sql, time);
|
||||
}
|
||||
if (log) log(sql, time);
|
||||
releaseConnectionAndCallback(connection, err, data);
|
||||
});
|
||||
}
|
||||
|
@ -614,7 +617,7 @@ MySQL.prototype.isActual = function (cb) {
|
|||
|
||||
function done(err, needAlter) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
debug(err);
|
||||
}
|
||||
ok = ok || needAlter;
|
||||
if (--wait === 0 && cb) {
|
||||
|
@ -793,7 +796,13 @@ MySQL.prototype.propertiesSQL = function (model) {
|
|||
var sql = [];
|
||||
if (pks.length === 1) {
|
||||
var idName = this.idName(model);
|
||||
sql.push(self.columnEscaped(model, idName) + ' INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY');
|
||||
var idProp = this._models[model].properties[idName];
|
||||
if(idProp.generated) {
|
||||
sql.push(self.columnEscaped(model, idName) + ' INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY');
|
||||
} else {
|
||||
idProp.nullable = false;
|
||||
sql.push(self.columnEscaped(model, idName) + ' ' + self.propertySettingsSQL(model, idName) + ' PRIMARY KEY');
|
||||
}
|
||||
}
|
||||
Object.keys(this._models[model].properties).forEach(function (prop) {
|
||||
if (self.id(model, prop) && pks.length === 1) {
|
||||
|
@ -1086,7 +1095,7 @@ function unsigned(p, dt) {
|
|||
*/
|
||||
MySQL.prototype.disconnect = function () {
|
||||
if (this.debug) {
|
||||
console.log('disconnect');
|
||||
debug('disconnect');
|
||||
}
|
||||
if (this.client) {
|
||||
this.client.end();
|
||||
|
|
|
@ -8,10 +8,8 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"mysql": "~2.1.1",
|
||||
"async": "~0.2.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"loopback-datasource-juggler": "1.x.x"
|
||||
"async": "~0.7.0",
|
||||
"debug": "~0.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"loopback-datasource-juggler": "1.x.x",
|
||||
|
|
|
@ -118,12 +118,13 @@ describe('migrations', function () {
|
|||
|
||||
it('StringData should have correct columns', function (done) {
|
||||
getFields('StringData', function (err, fields) {
|
||||
assert.deepEqual(fields, { id: { Field: 'id',
|
||||
Type: 'int(11)',
|
||||
Null: 'NO',
|
||||
Key: 'PRI',
|
||||
Default: null,
|
||||
Extra: 'auto_increment' },
|
||||
assert.deepEqual(fields, {
|
||||
idString: { Field: "idString",
|
||||
Type: 'varchar(255)',
|
||||
Null: 'NO',
|
||||
Key: 'PRI',
|
||||
Default: null,
|
||||
Extra: ''},
|
||||
smallString: { Field: 'smallString',
|
||||
Type: 'char(127)',
|
||||
Null: 'NO',
|
||||
|
@ -223,7 +224,7 @@ describe('migrations', function () {
|
|||
});
|
||||
});
|
||||
|
||||
it('should autoupgrade', function (done) {
|
||||
it('should autoupdate', function (done) {
|
||||
var userExists = function (cb) {
|
||||
query('SELECT * FROM UserData', function (err, res) {
|
||||
cb(!err && res[0].email == 'test@example.com');
|
||||
|
@ -306,7 +307,7 @@ describe('migrations', function () {
|
|||
|
||||
it('should disconnect when done', function (done) {
|
||||
db.disconnect();
|
||||
done()
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -332,6 +333,7 @@ function setup(done) {
|
|||
});
|
||||
|
||||
StringData = db.define('StringData', {
|
||||
idString: {type: String, id: true},
|
||||
smallString: {type: String, null: false, index: true, dataType: 'char', limit: 127},
|
||||
mediumString: {type: String, null: false, dataType: 'varchar', limit: 255},
|
||||
tinyText: {type: String, dataType: 'tinyText'},
|
||||
|
|
Loading…
Reference in New Issue