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