Merge pull request #243 from strongloop/refactor-mysql-migration

Refactor mysql migration
This commit is contained in:
Sadman Sakib Hasan 2017-03-02 13:18:21 -08:00 committed by GitHub
commit 9f1affdad3
1 changed files with 66 additions and 76 deletions

View File

@ -13,12 +13,36 @@ module.exports = mixinMigration;
* @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.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.
* If not present, apply to all models
* @param {Function} [cb] The callback function
*/
MySQL.prototype.autoupdate = function(models, cb) {
var self = this;
var foreignKeyStatements = [];
@ -40,45 +64,37 @@ 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);
self.discoverForeignKeys(self.table(model), {}, function(err, foreignKeys) {
if (err) console.log('Failed to discover "' + table + '" foreign keys', err);
if (!err && fields && fields.length) {
//if we already have a definition, update this table
self.alterTable(model, fields, indexes, foreignKeys, function(err, changed, res) {
//check to see if there were any new foreign keys for this table.
//If so, we'll create them once all tables have been updated
if (!err && res && res.newFks && res.newFks.length) {
foreignKeyStatements.push(res.newFks);
}
done(err);
});
} else {
//if there is not yet a definition, create this table
var res = self.createTable(model, function(err) {
if (!err) {
//get a list of the alter statements needed to add the defined foreign keys
var newFks = self.getForeignKeySQL(model, foreignKeys);
if (!err && fields && fields.length) {
//if we already have a definition, update this table
self.alterTable(model, fields, indexes, foreignKeys, function(err, changed, res) {
//check to see if there were any new foreign keys for this table.
//If so, we'll create them once all tables have been updated
if (!err && res && res.newFks && res.newFks.length) {
foreignKeyStatements.push(res.newFks);
if (newFks && newFks.length) {
foreignKeyStatements.push(self.getAlterStatement(model, newFks));
}
done(err);
});
} else {
//if there is not yet a definition, create this table
var res = self.createTable(model, function(err) {
if (!err) {
//get a list of the alter statements needed to add the defined foreign keys
var newFks = self.getForeignKeySQL(model, foreignKeys);
//check to see if there were any new foreign keys for this table.
//If so, we'll create them once all tables have been updated
if (newFks && newFks.length) {
foreignKeyStatements.push(self.getAlterStatement(model, newFks));
}
}
done(err);
});
}
});
}
done(err);
});
}
});
});
}, function(err) {
@ -131,25 +147,18 @@ 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.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);
self.execute('SHOW INDEXES FROM ' + table, function(err, indexes) {
if (err) console.log('Failed to discover "' + table + '" indexes', err);
self.discoverForeignKeys(self.table(model), {}, function(err, foreignKeys) {
if (err) console.log('Failed to discover "' + table + '" foreign keys', err);
self.alterTable(model, fields, indexes, foreignKeys, function(err, needAlter) {
if (err) {
return done(err);
} else {
ok = ok || needAlter;
done(err);
}
}, true);
});
self.alterTable(model, fields, indexes, foreignKeys, function(err, needAlter) {
if (err) {
return done(err);
} else {
ok = ok || needAlter;
done(err);
}
}, true);
});
});
}, function(err) {
@ -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;