From fa003bb2676d0d5dd68074c68417ee160757270d Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 7 Apr 2014 22:16:26 -0700 Subject: [PATCH] Fix the table generation for string ids --- lib/mysql.js | 36 +++++++++++++++++++++++------------- package.json | 6 ++---- test/migration.test.js | 18 ++++++++++-------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index b04e190..2e2eb12 100644 --- a/lib/mysql.js +++ b/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,7 @@ exports.initialize = function initializeDataSource(dataSource, callback) { user: s.username || s.user, password: s.password, timezone: s.timezone, - debug: s.debug, + // debug: s.debug || debug.enabled, socketPath: s.socketPath, charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd. supportBigNumbers: s.supportBigNumbers, @@ -61,8 +63,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 +112,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 +127,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 +618,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 +797,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 +1096,7 @@ function unsigned(p, dt) { */ MySQL.prototype.disconnect = function () { if (this.debug) { - console.log('disconnect'); + debug('disconnect'); } if (this.client) { this.client.end(); diff --git a/package.json b/package.json index 39a5f45..5946b4e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/migration.test.js b/test/migration.test.js index 137e552..4ae80ed 100644 --- a/test/migration.test.js +++ b/test/migration.test.js @@ -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'},