Merge pull request #97 from strongloop/feature/connector-refactor

Refactor the code to use base SqlConnector
This commit is contained in:
Raymond Feng 2015-05-13 12:14:21 -07:00
commit 9dc6353b18
10 changed files with 1064 additions and 1215 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ coverage
*.tgz
*.xml
.loopbackrc
.idea

View File

@ -1,16 +0,0 @@
{
"content": [
{
"title": "LoopBack MySQL Connector API",
"depth": 2
},
"lib/mysql.js",
{
"title": "MySQL Discovery API",
"depth": 2
},
"lib/discovery.js"
],
"codeSectionDepth": 3
}

View File

@ -1,21 +1,46 @@
module.exports = mixinDiscovery;
function mixinDiscovery(MySQL) {
/*!
* @param {MySQL} MySQL connector class
* @param {Object} mysql mysql driver
*/
function mixinDiscovery(MySQL, mysql) {
var async = require('async');
function paginateSQL(sql, orderBy, options) {
options = options || {};
var limit = '';
var limitClause = '';
if (options.offset || options.skip || options.limit) {
limit = ' LIMIT ' + (options.offset || options.skip || 0); // Offset starts from 0
// Offset starts from 0
var offset = Number(options.offset || options.skip || 0);
if (isNaN(offset)) {
offset = 0;
}
limitClause = ' LIMIT ' + offset;
if (options.limit) {
limit = limit + ',' + options.limit;
var limit = Number(options.limit);
if (isNaN(limit)) {
limit = 0;
}
limitClause = limitClause + ',' + limit;
}
}
if (!orderBy) {
sql += ' ORDER BY ' + orderBy;
}
return sql + limit;
return sql + limitClause;
}
/*!
* Build sql for listing schemas (databases in MySQL)
* @params {Object} [options] Options object
* @returns {String} The SQL statement
*/
function querySchemas(options) {
var sql = 'SELECT catalog_name as "catalog",' +
' schema_name as "schema"' +
' FROM information_schema.schemata';
return paginateSQL(sql, 'schema_name', options);
}
/*!
@ -25,17 +50,24 @@ function mixinDiscovery(MySQL) {
*/
function queryTables(options) {
var sqlTables = null;
var owner = options.owner || options.schema;
var schema = options.owner || options.schema;
if (options.all && !owner) {
sqlTables = paginateSQL('SELECT \'table\' AS "type", table_name AS "name", table_schema AS "owner"'
+ ' FROM information_schema.tables', 'table_schema, table_name', options);
} else if (owner) {
sqlTables = paginateSQL('SELECT \'table\' AS "type", table_name AS "name", table_schema AS "owner"'
+ ' FROM information_schema.tables WHERE table_schema=\'' + owner + '\'', 'table_schema, table_name', options);
if (options.all && !schema) {
sqlTables = paginateSQL('SELECT \'table\' AS "type",' +
' table_name AS "name", table_schema AS "owner"' +
' FROM information_schema.tables',
'table_schema, table_name', options);
} else if (schema) {
sqlTables = paginateSQL('SELECT \'table\' AS "type",' +
' table_name AS "name", table_schema AS "schema"' +
' FROM information_schema.tables' +
' WHERE table_schema=' + mysql.esacpe(schema),
'table_schema, table_name', options);
} else {
sqlTables = paginateSQL('SELECT \'table\' AS "type", table_name AS "name",'
+ ' table_schema AS "owner" FROM information_schema.tables WHERE table_schema=SUBSTRING_INDEX(USER(),\'@\',1)',
sqlTables = paginateSQL('SELECT \'table\' AS "type",' +
' table_name AS "name", ' +
' table_schema AS "owner" FROM information_schema.tables' +
' WHERE table_schema=SUBSTRING_INDEX(USER(),\'@\',1)',
'table_name', options);
}
return sqlTables;
@ -50,32 +82,48 @@ function mixinDiscovery(MySQL) {
var sqlViews = null;
if (options.views) {
var owner = options.owner || options.schema;
var schema = options.owner || options.schema;
if (options.all && !owner) {
sqlViews = paginateSQL('SELECT \'view\' AS "type", table_name AS "name",'
+ ' table_schema AS "owner" FROM information_schema.views',
if (options.all && !schema) {
sqlViews = paginateSQL('SELECT \'view\' AS "type",' +
' table_name AS "name",' +
' table_schema AS "owner"' +
' FROM information_schema.views',
'table_schema, table_name', options);
} else if (owner) {
sqlViews = paginateSQL('SELECT \'view\' AS "type", table_name AS "name",'
+ ' table_schema AS "owner" FROM information_schema.views WHERE table_schema=\'' + owner + '\'',
} else if (schema) {
sqlViews = paginateSQL('SELECT \'view\' AS "type",' +
' table_name AS "name",' +
' table_schema AS "owner"' +
' FROM information_schema.views' +
' WHERE table_schema=' + mysql.esacpe(schema),
'table_schema, table_name', options);
} else {
sqlViews = paginateSQL('SELECT \'view\' AS "type", table_name AS "name",'
+ ' table_schema AS "owner" FROM information_schema.views',
sqlViews = paginateSQL('SELECT \'view\' AS "type",' +
' table_name AS "name",' +
' table_schema AS "owner"' +
' FROM information_schema.views',
'table_name', options);
}
}
return sqlViews;
}
MySQL.prototype.discoverDatabaseSchemas = function(options, cb) {
if (!cb && typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};
this.execute(querySchemas(options), cb);
};
/**
* Discover model definitions
*
* @param {Object} options Options for discovery
* @param {Function} [cb] The callback function
*/
MySQL.prototype.discoverModelDefinitions = function (options, cb) {
MySQL.prototype.discoverModelDefinitions = function(options, cb) {
if (!cb && typeof options === 'function') {
cb = options;
options = {};
@ -83,16 +131,16 @@ function mixinDiscovery(MySQL) {
options = options || {};
var self = this;
var calls = [function (callback) {
self.query(queryTables(options), callback);
var calls = [function(callback) {
self.execute(queryTables(options), callback);
}];
if (options.views) {
calls.push(function (callback) {
self.query(queryViews(options), callback);
calls.push(function(callback) {
self.execute(queryViews(options), callback);
});
}
async.parallel(calls, function (err, data) {
async.parallel(calls, function(err, data) {
if (err) {
cb(err, data);
} else {
@ -125,7 +173,7 @@ function mixinDiscovery(MySQL) {
throw new Error('options must be an object: ' + options);
}
return {
owner: options.owner || options.schema,
schema: options.owner || options.schema,
table: table,
options: options,
cb: cb
@ -134,35 +182,37 @@ function mixinDiscovery(MySQL) {
/*!
* Build the sql statement to query columns for a given table
* @param owner
* @param schema
* @param table
* @returns {String} The sql statement
*/
function queryColumns(owner, table) {
function queryColumns(schema, table) {
var sql = null;
if (owner) {
if (schema) {
sql = paginateSQL('SELECT table_schema AS "owner",' +
' table_name AS "tableName", column_name AS "columnName",' +
' data_type AS "dataType",' +
' character_maximum_length AS "dataLength",' +
' numeric_precision AS "dataPrecision",' +
' numeric_scale AS "dataScale",' +
' is_nullable = \'YES\' AS "nullable"' +
' FROM information_schema.columns' +
' WHERE table_schema=\'' + owner + '\'' +
(table ? ' AND table_name=\'' + table + '\'' : ''),
'table_name, ordinal_position', {});
' table_name AS "tableName",' +
' column_name AS "columnName",' +
' data_type AS "dataType",' +
' character_maximum_length AS "dataLength",' +
' numeric_precision AS "dataPrecision",' +
' numeric_scale AS "dataScale",' +
' is_nullable = \'YES\' AS "nullable"' +
' FROM information_schema.columns' +
' WHERE table_schema=' + mysql.escape(schema) +
(table ? ' AND table_name=' + mysql.escape(table) : ''),
'table_name, ordinal_position', {});
} else {
sql = paginateSQL('SELECT table_schema AS "owner",' +
' table_name AS "tableName", column_name AS "columnName",' +
' data_type AS "dataType",' +
' character_maximum_length AS "dataLength",' +
' numeric_precision AS "dataPrecision",' +
' numeric_scale AS "dataScale",' +
' is_nullable = \'YES\' AS "nullable"' +
' FROM information_schema.columns' +
(table ? ' WHERE table_name=\'' + table + '\'' : ''),
'table_name, ordinal_position', {});
' table_name AS "tableName",' +
' column_name AS "columnName",' +
' data_type AS "dataType",' +
' character_maximum_length AS "dataLength",' +
' numeric_precision AS "dataPrecision",' +
' numeric_scale AS "dataScale",' +
' is_nullable = \'YES\' AS "nullable"' +
' FROM information_schema.columns' +
(table ? ' WHERE table_name=' + mysql.escape(table) : ''),
'table_name, ordinal_position', {});
}
return sql;
}
@ -174,51 +224,57 @@ function mixinDiscovery(MySQL) {
* @param {Function} [cb] The callback function
*
*/
MySQL.prototype.discoverModelProperties = function (table, options, cb) {
MySQL.prototype.discoverModelProperties = function(table, options, cb) {
var self = this;
var args = getArgs(table, options, cb);
var owner = args.owner;
if(!owner){
owner = inheritOwnerViaDataSource.call(this);
var schema = args.schema;
if (!schema) {
schema = this.getDefaultSchema();
}
table = args.table;
options = args.options;
cb = args.cb;
var sql = queryColumns(owner, table);
var callback = function (err, results) {
var sql = queryColumns(schema, table);
var callback = function(err, results) {
if (err) {
cb(err, results);
} else {
results.map(function (r) {
r.type = mysqlDataTypeToJSONType(r.dataType, r.dataLength);
results.map(function(r) {
r.type = self.buildPropertyType(r);
r.nullable = r.nullable ? 'Y' : 'N';
});
cb(err, results);
}
};
this.query(sql, callback);
this.execute(sql, callback);
};
/*!
* Build the sql statement for querying primary keys of a given table
* @param owner
* @param schema
* @param table
* @returns {string}
*/
// http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getPrimaryKeys(java.lang.String, java.lang.String, java.lang.String)
function queryForPrimaryKeys(owner, table) {
var sql = 'SELECT table_schema AS "owner", '
+ 'table_name AS "tableName", column_name AS "columnName", ordinal_position AS "keySeq", constraint_name AS "pkName" FROM'
+ ' information_schema.key_column_usage'
+ ' WHERE constraint_name=\'PRIMARY\'';
// http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html
// #getPrimaryKeys(java.lang.String, java.lang.String, java.lang.String)
function queryPrimaryKeys(schema, table) {
var sql = 'SELECT table_schema AS "owner",' +
' table_name AS "tableName",' +
' column_name AS "columnName",' +
' ordinal_position AS "keySeq",' +
' constraint_name AS "pkName"' +
' FROM information_schema.key_column_usage' +
' WHERE constraint_name=\'PRIMARY\'';
if (owner) {
sql += ' AND table_schema=\'' + owner + '\'';
if (schema) {
sql += ' AND table_schema=' + mysql.escape(schema);
}
if (table) {
sql += ' AND table_name=\'' + table + '\'';
sql += ' AND table_name=' + mysql.escape(table);
}
sql += ' ORDER BY table_schema, constraint_name, table_name, ordinal_position';
sql += ' ORDER BY' +
' table_schema, constraint_name, table_name, ordinal_position';
return sql;
}
@ -228,40 +284,44 @@ function mixinDiscovery(MySQL) {
* @param {Object} options The options for discovery
* @param {Function} [cb] The callback function
*/
MySQL.prototype.discoverPrimaryKeys = function (table, options, cb) {
MySQL.prototype.discoverPrimaryKeys = function(table, options, cb) {
var args = getArgs(table, options, cb);
var owner = args.owner;
if(!owner){
owner = inheritOwnerViaDataSource.call(this);
var schema = args.schema;
if (!schema) {
schema = this.getDefaultSchema();
}
table = args.table;
options = args.options;
cb = args.cb;
var sql = queryForPrimaryKeys(owner, table);
this.query(sql, cb);
var sql = queryPrimaryKeys(schema, table);
this.execute(sql, cb);
};
/*!
* Build the sql statement for querying foreign keys of a given table
* @param owner
* @param schema
* @param table
* @returns {string}
*/
function queryForeignKeys(owner, table) {
function queryForeignKeys(schema, table) {
var sql =
'SELECT table_schema AS "fkOwner", constraint_name AS "fkName", table_name AS "fkTableName",'
+ ' column_name AS "fkColumnName", ordinal_position AS "keySeq",'
+ ' referenced_table_schema AS "pkOwner", \'PRIMARY\' AS "pkName", '
+ ' referenced_table_name AS "pkTableName", referenced_column_name AS "pkColumnName"'
+ ' FROM information_schema.key_column_usage'
+ ' WHERE'
+ ' constraint_name!=\'PRIMARY\' and POSITION_IN_UNIQUE_CONSTRAINT IS NOT NULL';
if (owner) {
sql += ' AND table_schema=\'' + owner + '\'';
'SELECT table_schema AS "fkOwner",' +
' constraint_name AS "fkName",' +
' table_name AS "fkTableName",' +
' column_name AS "fkColumnName",' +
' ordinal_position AS "keySeq",' +
' referenced_table_schema AS "pkOwner", \'PRIMARY\' AS "pkName",' +
' referenced_table_name AS "pkTableName",' +
' referenced_column_name AS "pkColumnName"' +
' FROM information_schema.key_column_usage' +
' WHERE constraint_name!=\'PRIMARY\'' +
' AND POSITION_IN_UNIQUE_CONSTRAINT IS NOT NULL';
if (schema) {
sql += ' AND table_schema=' + mysql.escape(schema);
}
if (table) {
sql += ' AND table_name=\'' + table + '\'';
sql += ' AND table_name=' + mysql.escape(table);
}
return sql;
}
@ -272,42 +332,47 @@ function mixinDiscovery(MySQL) {
* @param {Object} options The options for discovery
* @param {Function} [cb] The callback function
*/
MySQL.prototype.discoverForeignKeys = function (table, options, cb) {
MySQL.prototype.discoverForeignKeys = function(table, options, cb) {
var args = getArgs(table, options, cb);
var owner = args.owner;
if(!owner){
owner = inheritOwnerViaDataSource.call(this);
var schema = args.schema;
if (!schema) {
schema = this.getDefaultSchema();
}
table = args.table;
options = args.options;
cb = args.cb;
var sql = queryForeignKeys(owner, table);
this.query(sql, cb);
var sql = queryForeignKeys(schema, table);
this.execute(sql, cb);
};
/*!
* Retrieves a description of the foreign key columns that reference the given table's primary key columns (the foreign keys exported by a table).
* Retrieves a description of the foreign key columns that reference the
* given table's primary key columns (the foreign keys exported by a table).
* They are ordered by fkTableOwner, fkTableName, and keySeq.
* @param owner
* @param schema
* @param table
* @returns {string}
*/
function queryExportedForeignKeys(owner, table) {
var sql = 'SELECT a.constraint_name AS "fkName", a.table_schema AS "fkOwner", a.table_name AS "fkTableName",'
+ ' a.column_name AS "fkColumnName", a.ordinal_position AS "keySeq",'
+ ' NULL AS "pkName", a.referenced_table_schema AS "pkOwner",'
+ ' a.referenced_table_name AS "pkTableName", a.referenced_column_name AS "pkColumnName"'
+ ' FROM'
+ ' information_schema.key_column_usage a'
+ ' WHERE a.position_in_unique_constraint IS NOT NULL';
if (owner) {
sql += ' and a.referenced_table_schema=\'' + owner + '\'';
function queryExportedForeignKeys(schema, table) {
var sql = 'SELECT a.constraint_name AS "fkName",' +
' a.table_schema AS "fkOwner",' +
' a.table_name AS "fkTableName",' +
' a.column_name AS "fkColumnName",' +
' a.ordinal_position AS "keySeq",' +
' NULL AS "pkName",' +
' a.referenced_table_schema AS "pkOwner",' +
' a.referenced_table_name AS "pkTableName",' +
' a.referenced_column_name AS "pkColumnName"' +
' FROM information_schema.key_column_usage a' +
' WHERE a.position_in_unique_constraint IS NOT NULL';
if (schema) {
sql += ' AND a.referenced_table_schema=' + mysql.escape(schema);
}
if (table) {
sql += ' and a.referenced_table_name=\'' + table + '\'';
sql += ' AND a.referenced_table_name=' + mysql.escape(table);
}
sql += ' order by a.table_schema, a.table_name, a.ordinal_position';
sql += ' ORDER BY a.table_schema, a.table_name, a.ordinal_position';
return sql;
}
@ -318,21 +383,24 @@ function mixinDiscovery(MySQL) {
* @param {Object} options The options for discovery
* @param {Function} [cb] The callback function
*/
MySQL.prototype.discoverExportedForeignKeys = function (table, options, cb) {
MySQL.prototype.discoverExportedForeignKeys = function(table, options, cb) {
var args = getArgs(table, options, cb);
var owner = args.owner;
if(!owner){
owner = inheritOwnerViaDataSource.call(this);
var schema = args.schema;
if (!schema) {
schema = this.getDefaultSchema();
}
table = args.table;
options = args.options;
cb = args.cb;
var sql = queryExportedForeignKeys(owner, table);
this.query(sql, cb);
var sql = queryExportedForeignKeys(schema, table);
this.execute(sql, cb);
};
function mysqlDataTypeToJSONType(mysqlType, dataLength) {
MySQL.prototype.buildPropertyType = function (columnDefinition) {
var mysqlType = columnDefinition.dataType;
var dataLength = columnDefinition.dataLength;
var type = mysqlType.toUpperCase();
switch (type) {
case 'CHAR':
@ -379,10 +447,11 @@ function mixinDiscovery(MySQL) {
}
}
function inheritOwnerViaDataSource(){
if(this.dataSource && this.dataSource.settings && this.dataSource.settings.database){
MySQL.prototype.getDefaultSchema = function() {
if (this.dataSource && this.dataSource.settings &&
this.dataSource.settings.database) {
return this.dataSource.settings.database;
}
return undefined;
}
};
}

View File

@ -1,4 +1,4 @@
var EnumFactory = function () {
var EnumFactory = function() {
if (arguments.length > 0) {
var Enum = function Enum(arg) {
if (typeof arg === 'number' && arg % 1 == 0) {
@ -14,14 +14,31 @@ var EnumFactory = function () {
}
};
var dxList = [];
dxList.push(''); // Want empty value to be at index 0 to match MySQL Enum values and MySQL non-strict behavior.
// Want empty value to be at index 0 to match MySQL Enum values and
// MySQL non-strict behavior.
dxList.push('');
for (var arg in arguments) {
arg = String(arguments[arg]);
Object.defineProperty(Enum, arg.toUpperCase(), {configurable: false, enumerable: true, value: arg, writable: false});
Object.defineProperty(Enum, arg.toUpperCase(), {
configurable: false,
enumerable: true,
value: arg,
writable: false
});
dxList.push(arg);
}
Object.defineProperty(Enum, '_values', {configurable: false, enumerable: false, value: dxList, writable: false});
Object.defineProperty(Enum, '_string', {configurable: false, enumerable: false, value: stringified(Enum), writable: false});
Object.defineProperty(Enum, '_values', {
configurable: false,
enumerable: false,
value: dxList,
writable: false
});
Object.defineProperty(Enum, '_string', {
configurable: false,
enumerable: false,
value: stringified(Enum),
writable: false
});
Object.freeze(Enum);
return Enum;
} else {

616
lib/migration.js Normal file
View File

@ -0,0 +1,616 @@
var async = require('async');
module.exports = mixinMigration;
/*!
* @param {MySQL} MySQL connector class
* @param {Object} mysql mysql driver
*/
function mixinMigration(MySQL, mysql) {
/**
* Perform autoupdate for the given models
* @param {String[]} [models] A model name or an array of model names.
* If not present, apply to all models
* @param {Function} [cb] The callback function
*/
MySQL.prototype.autoupdate = function(models, cb) {
var self = this;
if ((!cb) && ('function' === typeof models)) {
cb = models;
models = undefined;
}
// First argument is a model name
if ('string' === typeof models) {
models = [models];
}
models = models || Object.keys(this._models);
async.each(models, function(model, done) {
if (!(model in self._models)) {
return process.nextTick(function() {
done(new Error('Model not found: ' + model));
});
}
var table = self.tableEscaped(model);
self.execute('SHOW FIELDS FROM ' + table, function(err, fields) {
self.execute('SHOW INDEXES FROM ' + table, function(err, indexes) {
if (!err && fields && fields.length) {
self.alterTable(model, fields, indexes, done);
} else {
self.createTable(model, done);
}
});
});
}, cb);
};
/*!
* Create a DB table for the given model
* @param {string} model Model name
* @param cb
*/
MySQL.prototype.createTable = function(model, cb) {
var metadata = this.getModelDefinition(model).settings[this.name];
var engine = metadata && metadata.engine;
var sql = 'CREATE TABLE ' + this.tableEscaped(model) +
' (\n ' + this.buildColumnDefinitions(model) + '\n)';
if (engine) {
sql += 'ENGINE=' + engine + '\n';
}
this.execute(sql, cb);
};
/**
* Check if the models exist
* @param {String[]} [models] A model name or an array of model names. If not
* present, apply to all models
* @param {Function} [cb] The callback function
*/
MySQL.prototype.isActual = function(models, cb) {
var self = this;
var ok = false;
if ((!cb) && ('function' === typeof models)) {
cb = models;
models = undefined;
}
// First argument is a model name
if ('string' === typeof models) {
models = [models];
}
models = models || Object.keys(this._models);
async.each(models, function(model, done) {
var table = self.tableEscaped(model);
self.execute('SHOW FIELDS FROM ' + table, function(err, fields) {
self.execute('SHOW INDEXES FROM ' + table, function(err, indexes) {
self.alterTable(model, fields, indexes, function(err, needAlter) {
if (err) {
return done(err);
} else {
ok = ok || needAlter;
done(err);
}
}, true);
});
});
}, function(err) {
if (err) {
return err;
}
cb(null, !ok);
});
};
MySQL.prototype.alterTable = function(model, actualFields, actualIndexes, done, checkOnly) {
var self = this;
var m = this.getModelDefinition(model);
var propNames = Object.keys(m.properties).filter(function(name) {
return !!m.properties[name];
});
var indexes = m.settings.indexes || {};
var indexNames = Object.keys(indexes).filter(function(name) {
return !!m.settings.indexes[name];
});
var sql = [];
var ai = {};
if (actualIndexes) {
actualIndexes.forEach(function(i) {
var name = i.Key_name;
if (!ai[name]) {
ai[name] = {
info: i,
columns: []
};
}
ai[name].columns[i.Seq_in_index - 1] = i.Column_name;
});
}
var aiNames = Object.keys(ai);
// change/add new fields
propNames.forEach(function(propName) {
if (m.properties[propName] && self.id(model, propName)) return;
var found;
if (actualFields) {
actualFields.forEach(function(f) {
if (f.Field === propName) {
found = f;
}
});
}
if (found) {
actualize(propName, found);
} else {
sql.push('ADD COLUMN ' + self.client.escapeId(propName) + ' ' +
self.buildColumnDefinition(model, propName));
}
});
// drop columns
if (actualFields) {
actualFields.forEach(function(f) {
var notFound = !~propNames.indexOf(f.Field);
if (m.properties[f.Field] && self.id(model, f.Field)) return;
if (notFound || !m.properties[f.Field]) {
sql.push('DROP COLUMN ' + self.client.escapeId(f.Field));
}
});
}
// remove indexes
aiNames.forEach(function(indexName) {
if (indexName === 'PRIMARY' ||
(m.properties[indexName] && self.id(model, indexName))) return;
if (indexNames.indexOf(indexName) === -1 && !m.properties[indexName] ||
m.properties[indexName] && !m.properties[indexName].index) {
sql.push('DROP INDEX ' + self.client.escapeId(indexName));
} else {
// first: check single (only type and kind)
if (m.properties[indexName] && !m.properties[indexName].index) {
// TODO
return;
}
// second: check multiple indexes
var orderMatched = true;
if (indexNames.indexOf(indexName) !== -1) {
m.settings.indexes[indexName].columns.split(/,\s*/).forEach(
function(columnName, i) {
if (ai[indexName].columns[i] !== columnName) orderMatched = false;
});
}
if (!orderMatched) {
sql.push('DROP INDEX ' + self.client.escapeId(indexName));
delete ai[indexName];
}
}
});
// add single-column indexes
propNames.forEach(function(propName) {
var i = m.properties[propName].index;
if (!i) {
return;
}
var found = ai[propName] && ai[propName].info;
if (!found) {
var pName = self.client.escapeId(propName);
var type = '';
var kind = '';
if (i.type) {
type = 'USING ' + i.type;
}
if (kind && type) {
sql.push('ADD ' + kind + ' INDEX ' + pName +
' (' + pName + ') ' + type);
} else {
if (typeof i === 'object' && i.unique && i.unique === true) {
kind = "UNIQUE";
}
sql.push('ADD ' + kind + ' INDEX ' + pName + ' ' + type +
' (' + pName + ') ');
}
}
});
// add multi-column indexes
indexNames.forEach(function(indexName) {
var i = m.settings.indexes[indexName];
var found = ai[indexName] && ai[indexName].info;
if (!found) {
var iName = self.client.escapeId(indexName);
var type = '';
var kind = '';
if (i.type) {
type = 'USING ' + i.type;
}
if (i.kind) {
kind = i.kind;
}
if (kind && type) {
sql.push('ADD ' + kind + ' INDEX ' + iName +
' (' + i.columns + ') ' + type);
} else {
sql.push('ADD ' + kind + ' INDEX ' + type + ' ' + iName +
' (' + i.columns + ')');
}
}
});
if (sql.length) {
var query = 'ALTER TABLE ' + self.tableEscaped(model) + ' ' +
sql.join(',\n');
if (checkOnly) {
done(null, true, {statements: sql, query: query});
} else {
this.execute(query, done);
}
} else {
done();
}
function actualize(propName, oldSettings) {
var newSettings = m.properties[propName];
if (newSettings && changed(newSettings, oldSettings)) {
var pName = self.client.escapeId(propName);
sql.push('CHANGE COLUMN ' + pName + ' ' + pName + ' ' +
self.buildColumnDefinition(model, propName));
}
}
function changed(newSettings, oldSettings) {
if (oldSettings.Null === 'YES') {
// Used to allow null and does not now.
if (!self.isNullable(newSettings)) {
return true;
}
}
if (oldSettings.Null === 'NO') {
// Did not allow null and now does.
if (self.isNullable(newSettings)) {
return true;
}
}
if (oldSettings.Type.toUpperCase() !==
self.buildColumnType(newSettings).toUpperCase()) {
return true;
}
return false;
}
};
MySQL.prototype.buildColumnDefinitions =
MySQL.prototype.propertiesSQL = function(model) {
var self = this;
var pks = this.idNames(model).map(function(i) {
return self.columnEscaped(model, i);
});
var definition = this.getModelDefinition(model);
var sql = [];
if (pks.length === 1) {
var idName = this.idName(model);
var idProp = this.getModelDefinition(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.buildColumnDefinition(model, idName) + ' PRIMARY KEY');
}
}
Object.keys(definition.properties).forEach(function(prop) {
if (self.id(model, prop) && pks.length === 1) {
return;
}
var colName = self.columnEscaped(model, prop);
sql.push(colName + ' ' + self.buildColumnDefinition(model, prop));
});
if (pks.length > 1) {
sql.push('PRIMARY KEY(' + pks.join(',') + ')');
}
var indexes = self.buildIndexes(model);
indexes.forEach(function(i) {
sql.push(i);
});
return sql.join(',\n ');
};
MySQL.prototype.buildIndex = function(model, property) {
var prop = this.getModelDefinition(model).properties[property];
var i = prop && prop.index;
if (!i) {
return '';
}
var type = '';
var kind = '';
if (i.type) {
type = 'USING ' + i.type;
}
if (i.kind) {
kind = i.kind;
}
var columnName = this.columnEscaped(model, property);
if (kind && type) {
return (kind + ' INDEX ' + columnName + ' (' + columnName + ') ' + type);
} else {
if (typeof i === 'object' && i.unique && i.unique === true) {
kind = "UNIQUE";
}
return (kind + ' INDEX ' + columnName + ' ' + type + ' (' + columnName + ') ');
}
};
MySQL.prototype.buildIndexes = function(model) {
var self = this;
var indexClauses = [];
var definition = this.getModelDefinition(model);
var indexes = definition.settings.indexes || {};
// Build model level indexes
for (var index in indexes) {
var i = indexes[index];
var type = '';
var kind = '';
if (i.type) {
type = 'USING ' + i.type;
}
if (i.kind) {
kind = i.kind;
}
var indexedColumns = [];
var indexName = this.escapeName(index);
if (Array.isArray(i.keys)) {
indexedColumns = i.keys.map(function(key) {
return self.columnEscaped(model, key);
});
}
var columns = indexedColumns.join(',') || i.columns;
if (kind && type) {
indexClauses.push(kind + ' INDEX ' + indexName + ' (' + columns + ') ' + type);
} else {
indexClauses.push(kind + ' INDEX ' + type + ' ' + indexName + ' (' + columns + ')');
}
}
// Define index for each of the properties
for (var p in definition.properties) {
var propIndex = self.buildIndex(model, p);
if (propIndex) {
indexClauses.push(propIndex);
}
}
return indexClauses;
};
MySQL.prototype.buildColumnDefinition = function(model, prop) {
var p = this.getModelDefinition(model).properties[prop];
var line = this.columnDataType(model, prop) + ' ' +
(this.isNullable(p) ? 'NULL' : 'NOT NULL');
return line;
};
MySQL.prototype.columnDataType = function(model, property) {
var columnMetadata = this.columnMetadata(model, property);
var colType = columnMetadata && columnMetadata.dataType;
if (colType) {
colType = colType.toUpperCase();
}
var prop = this.getModelDefinition(model).properties[property];
if (!prop) {
return null;
}
var colLength = columnMetadata && columnMetadata.dataLength ||
prop.length || prop.limit;
if (colType && colLength) {
return colType + '(' + colLength + ')';
}
return this.buildColumnType(prop);
};
MySQL.prototype.buildColumnType = function buildColumnType(propertyDefinition) {
var dt = '';
var p = propertyDefinition;
switch (p.type.name) {
default:
case 'JSON':
case 'Object':
case 'Any':
case 'Text':
dt = columnType(p, 'TEXT');
dt = stringOptionsByType(p, dt);
break;
case 'String':
dt = columnType(p, 'VARCHAR');
dt = stringOptionsByType(p, dt);
break;
case 'Number':
dt = columnType(p, 'INT');
dt = numericOptionsByType(p, dt);
break;
case 'Date':
dt = columnType(p, 'DATETIME'); // Currently doesn't need options.
break;
case 'Boolean':
dt = 'TINYINT(1)';
break;
case 'Point':
case 'GeoPoint':
dt = 'POINT';
break;
case 'Enum':
dt = 'ENUM(' + p.type._string + ')';
dt = stringOptions(p, dt); // Enum columns can have charset/collation.
break;
}
return dt;
};
function columnType(p, defaultType) {
var dt = defaultType;
if (p.dataType) {
dt = String(p.dataType);
}
return dt;
}
function stringOptionsByType(p, columnType) {
switch (columnType.toLowerCase()) {
default:
case 'varchar':
// The maximum length for an ID column is 1000 bytes
// The maximum row size is 64K
var len = p.length || p.limit ||
((p.type !== String) ? 4096 : p.id ? 255 : 512);
columnType += '(' + len + ')';
break;
case 'char':
len = p.length || p.limit || 255;
columnType += '(' + len + ')';
break;
case 'text':
case 'tinytext':
case 'mediumtext':
case 'longtext':
break;
}
columnType = stringOptions(p, columnType);
return columnType;
}
function stringOptions(p, columnType) {
if (p.charset) {
columnType += " CHARACTER SET " + p.charset;
}
if (p.collation) {
columnType += " COLLATE " + p.collation;
}
return columnType;
}
function numericOptionsByType(p, columnType) {
switch (columnType.toLowerCase()) {
default:
case 'tinyint':
case 'smallint':
case 'mediumint':
case 'int':
case 'integer':
case 'bigint':
columnType = integerOptions(p, columnType);
break;
case 'decimal':
case 'numeric':
columnType = fixedPointOptions(p, columnType);
break;
case 'float':
case 'double':
columnType = floatingPointOptions(p, columnType);
break;
}
columnType = unsigned(p, columnType);
return columnType;
}
function floatingPointOptions(p, columnType) {
var precision = 16;
var scale = 8;
if (p.precision) {
precision = Number(p.precision);
}
if (p.scale) {
scale = Number(p.scale);
}
if (p.precision && p.scale) {
columnType += '(' + precision + ',' + scale + ')';
} else if (p.precision) {
columnType += '(' + precision + ')';
}
return columnType;
}
/* @TODO: Change fixed point to use an arbitrary precision arithmetic library. */
/* Currently fixed point will lose precision because it's turned to non-fixed in */
/* JS. Also, defaulting column to (9,2) and not allowing non-specified 'DECIMAL' */
/* declaration which would default to DECIMAL(10,0). Instead defaulting to (9,2). */
function fixedPointOptions(p, columnType) {
var precision = 9;
var scale = 2;
if (p.precision) {
precision = Number(p.precision);
}
if (p.scale) {
scale = Number(p.scale);
}
columnType += '(' + precision + ',' + scale + ')';
return columnType;
}
function integerOptions(p, columnType) {
var tmp = 0;
if (p.display || p.limit) {
tmp = Number(p.display || p.limit);
}
if (tmp > 0) {
columnType += '(' + tmp + ')';
} else if (p.unsigned) {
switch (columnType.toLowerCase()) {
default:
case 'int':
columnType += '(10)';
break;
case 'mediumint':
columnType += '(8)';
break;
case 'smallint':
columnType += '(5)';
break;
case 'tinyint':
columnType += '(3)';
break;
case 'bigint':
columnType += '(20)';
break;
}
} else {
switch (columnType.toLowerCase()) {
default:
case 'int':
columnType += '(11)';
break;
case 'mediumint':
columnType += '(9)';
break;
case 'smallint':
columnType += '(6)';
break;
case 'tinyint':
columnType += '(4)';
break;
case 'bigint':
columnType += '(20)';
break;
}
}
return columnType;
}
function unsigned(p, columnType) {
if (p.unsigned) {
columnType += ' UNSIGNED';
}
return columnType;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@ var assert = require('assert');
var db, DummyModel, odb;
describe('migrations', function () {
describe('connections', function () {
before(function () {
require('./init.js');
@ -23,9 +23,9 @@ describe('migrations', function () {
});
it('should disconnect first db', function (done) {
db.client.end(function () {
odb = getSchema();
done()
db.disconnect(function () {
odb = getDataSource();
done();
});
});
@ -40,8 +40,8 @@ describe('migrations', function () {
});
it('should drop db and disconnect all', function (done) {
db.connector.query('DROP DATABASE IF EXISTS ' + db.settings.database, function (err) {
db.client.end(function () {
db.connector.execute('DROP DATABASE IF EXISTS ' + db.settings.database, function (err) {
db.disconnect(function () {
done();
});
});
@ -52,16 +52,18 @@ function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done) {
query('DROP DATABASE IF EXISTS ' + odb.settings.database, function (err) {
assert.ok(!err);
odb.client.end(function () {
odb.disconnect(function () {
db = getSchema({collation: test_set_collo, createDatabase: true});
db = getDataSource({collation: test_set_collo, createDatabase: true});
DummyModel = db.define('DummyModel', {string: String});
db.automigrate(function () {
var q = 'SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ' + db.client.escape(db.settings.database) + ' LIMIT 1';
db.connector.query(q, function (err, r) {
var q = 'SELECT DEFAULT_COLLATION_NAME' +
' FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ' +
db.driver.escape(db.settings.database) + ' LIMIT 1';
db.connector.execute(q, function (err, r) {
assert.ok(!err);
assert.ok(r[0].DEFAULT_COLLATION_NAME.match(test_collo));
db.connector.query('SHOW VARIABLES LIKE "character_set%"', function (err, r) {
db.connector.execute('SHOW VARIABLES LIKE "character_set%"', function (err, r) {
assert.ok(!err);
var hit_all = 0;
for (var result in r) {
@ -72,7 +74,7 @@ function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done) {
}
assert.equal(hit_all, 4);
});
db.connector.query('SHOW VARIABLES LIKE "collation%"', function (err, r) {
db.connector.execute('SHOW VARIABLES LIKE "collation%"', function (err, r) {
assert.ok(!err);
var hit_all = 0;
for (var result in r) {
@ -90,7 +92,7 @@ function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done) {
}
function matchResult(result, variable_name, match) {
if (result.Variable_name == variable_name) {
if (result.Variable_name === variable_name) {
assert.ok(result.Value.match(match));
return 1;
}
@ -98,7 +100,7 @@ function matchResult(result, variable_name, match) {
}
var query = function (sql, cb) {
odb.connector.query(sql, cb);
odb.connector.execute(sql, cb);
};

View File

@ -95,7 +95,7 @@ function setup(done) {
}
var query = function (sql, cb) {
db.adapter.query(sql, cb);
db.adapter.execute(sql, cb);
};
var blankDatabase = function (db, cb) {

View File

@ -16,7 +16,7 @@ describe('migrations', function () {
it('UserData should have correct columns', function (done) {
getFields('UserData', function (err, fields) {
assert.deepEqual(fields, {
fields.should.be.eql({
id: {
Field: 'id',
Type: 'int(11)',
@ -75,7 +75,7 @@ describe('migrations', function () {
// Note: getIndexes truncates multi-key indexes to the first member.
// Hence index1 is correct.
getIndexes('UserData', function (err, fields) {
assert.deepEqual(fields, { PRIMARY: { Table: 'UserData',
fields.should.be.eql({ PRIMARY: { Table: 'UserData',
Non_unique: 0,
Key_name: 'PRIMARY',
Seq_in_index: 1,
@ -118,7 +118,7 @@ describe('migrations', function () {
it('StringData should have correct columns', function (done) {
getFields('StringData', function (err, fields) {
assert.deepEqual(fields, {
fields.should.be.eql({
idString: { Field: "idString",
Type: 'varchar(255)',
Null: 'NO',
@ -162,7 +162,7 @@ describe('migrations', function () {
it('NumberData should have correct columns', function (done) {
getFields('NumberData', function (err, fields) {
assert.deepEqual(fields, {
fields.should.be.eql({
id: { Field: 'id',
Type: 'int(11)',
Null: 'NO',
@ -200,7 +200,7 @@ describe('migrations', function () {
it('DateData should have correct columns', function (done) {
getFields('DateData', function (err, fields) {
assert.deepEqual(fields, {
fields.should.be.eql({
id: { Field: 'id',
Type: 'int(11)',
Null: 'NO',
@ -381,7 +381,7 @@ function setup(done) {
}
var query = function (sql, cb) {
db.adapter.query(sql, cb);
db.adapter.execute(sql, cb);
};
var blankDatabase = function (db, cb) {

View File

@ -12,6 +12,17 @@ before(function () {
});
describe('discoverModels', function () {
describe('Discover database schemas', function() {
it('should return an array of db schemas', function(done) {
db.connector.discoverDatabaseSchemas(function(err, schemas) {
if (err) return done(err);
schemas.should.be.an.array;
schemas.length.should.be.above(0);
done();
});
});
});
describe('Discover models including views', function () {
it('should return an array of tables and views', function (done) {