Refactor migration methods
Fix timeout error on tests Signed-off-by: Sakib Hasan <sshasan10@hotmail.com> add showFields and showIndexes functions Extract getTableStatus Extract columnDataType to base connector
This commit is contained in:
parent
030f8f9dc1
commit
ab644d9fea
|
@ -13,6 +13,30 @@ module.exports = mixinMigration;
|
|||
* @param {Object} mysql mysql driver
|
||||
*/
|
||||
function mixinMigration(MySQL, mysql) {
|
||||
MySQL.prototype.showFields = function(model, cb) {
|
||||
var table = this.tableEscaped(model);
|
||||
var sql = 'SHOW FIELDS FROM ' + table;
|
||||
this.execute(sql, function(err, fields) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
} else {
|
||||
cb(err, fields);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
MySQL.prototype.showIndexes = function(model, cb) {
|
||||
var table = this.tableEscaped(model);
|
||||
var sql = 'SHOW INDEXES FROM ' + table;
|
||||
this.execute(sql, function(err, indexes) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
} else {
|
||||
cb(err, indexes);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Perform autoupdate for the given models
|
||||
* @param {String[]} [models] A model name or an array of model names.
|
||||
|
@ -40,13 +64,8 @@ function mixinMigration(MySQL, mysql) {
|
|||
done(new Error(g.f('Model not found: %s', model)));
|
||||
});
|
||||
}
|
||||
var table = self.tableEscaped(model);
|
||||
self.execute('SHOW FIELDS FROM ' + table, function(err, fields) {
|
||||
if (err) console.log('Failed to discover "' + table + '" fields', err);
|
||||
|
||||
self.execute('SHOW INDEXES FROM ' + table, function(err, indexes) {
|
||||
if (err) console.log('Failed to discover "' + table + '" indexes', err);
|
||||
|
||||
self.getTableStatus(model, function(err, fields, indexes) {
|
||||
self.discoverForeignKeys(self.table(model), {}, function(err, foreignKeys) {
|
||||
if (err) console.log('Failed to discover "' + table + '" foreign keys', err);
|
||||
|
||||
|
@ -58,7 +77,6 @@ function mixinMigration(MySQL, mysql) {
|
|||
if (!err && res && res.newFks && res.newFks.length) {
|
||||
foreignKeyStatements.push(res.newFks);
|
||||
}
|
||||
|
||||
done(err);
|
||||
});
|
||||
} else {
|
||||
|
@ -74,13 +92,11 @@ function mixinMigration(MySQL, mysql) {
|
|||
foreignKeyStatements.push(self.getAlterStatement(model, newFks));
|
||||
}
|
||||
}
|
||||
|
||||
done(err);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}, function(err) {
|
||||
if (err) return cb(err);
|
||||
|
||||
|
@ -131,13 +147,7 @@ function mixinMigration(MySQL, mysql) {
|
|||
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) {
|
||||
if (err) console.log('Failed to discover "' + table + '" fields', err);
|
||||
|
||||
self.execute('SHOW INDEXES FROM ' + table, function(err, indexes) {
|
||||
if (err) console.log('Failed to discover "' + table + '" indexes', err);
|
||||
|
||||
self.getTableStatus(model, function(err, fields, indexes) {
|
||||
self.discoverForeignKeys(self.table(model), {}, function(err, foreignKeys) {
|
||||
if (err) console.log('Failed to discover "' + table + '" foreign keys', err);
|
||||
|
||||
|
@ -151,7 +161,6 @@ function mixinMigration(MySQL, mysql) {
|
|||
}, true);
|
||||
});
|
||||
});
|
||||
});
|
||||
}, function(err) {
|
||||
cb(err, !ok);
|
||||
});
|
||||
|
@ -288,7 +297,6 @@ function mixinMigration(MySQL, mysql) {
|
|||
if (indexName === 'PRIMARY' ||
|
||||
(m.properties[indexName] && self.id(model, indexName))) return;
|
||||
|
||||
if (Object.keys(actualFks).indexOf(indexName) > -1) return; //this index is from an FK
|
||||
if (indexNames.indexOf(indexName) === -1 && !m.properties[indexName] ||
|
||||
m.properties[indexName] && !m.properties[indexName].index) {
|
||||
sql.push('DROP INDEX ' + self.client.escapeId(indexName));
|
||||
|
@ -645,24 +653,6 @@ function mixinMigration(MySQL, mysql) {
|
|||
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;
|
||||
|
|
Loading…
Reference in New Issue