Single-column indexes in mysql (autoupdate)

This commit is contained in:
Anatoliy Chakkaev 2012-08-17 23:39:22 +04:00
parent 62178c26c9
commit 62d0e459c0
2 changed files with 63 additions and 7 deletions

View File

@ -293,13 +293,15 @@ MySQL.prototype.autoupdate = function (cb) {
Object.keys(this._models).forEach(function (model) { Object.keys(this._models).forEach(function (model) {
wait += 1; wait += 1;
self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) { self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) {
self.query('SHOW INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) {
if (!err && fields.length) { if (!err && fields.length) {
self.alterTable(model, fields, done); self.alterTable(model, fields, indexes, done);
} else { } else {
self.createTable(model, done); self.createTable(model, done);
} }
}); });
}); });
});
function done(err) { function done(err) {
if (err) { if (err) {
@ -318,7 +320,7 @@ MySQL.prototype.isActual = function (cb) {
Object.keys(this._models).forEach(function (model) { Object.keys(this._models).forEach(function (model) {
wait += 1; wait += 1;
self.query('SHOW FIELDS FROM ' + model, function (err, fields) { self.query('SHOW FIELDS FROM ' + model, function (err, fields) {
self.alterTable(model, fields, done, true); self.alterTable(model, fields, null, done, true);
}); });
}); });
@ -333,7 +335,7 @@ MySQL.prototype.isActual = function (cb) {
} }
}; };
MySQL.prototype.alterTable = function (model, actualFields, done, checkOnly) { MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done, checkOnly) {
var self = this; var self = this;
var m = this._models[model]; var m = this._models[model];
var propNames = Object.keys(m.properties).filter(function (name) { var propNames = Object.keys(m.properties).filter(function (name) {
@ -366,6 +368,35 @@ MySQL.prototype.alterTable = function (model, actualFields, done, checkOnly) {
} }
}); });
// add single-column indexes
propNames.forEach(function (propName) {
if (!m.properties[propName].index) {
return;
}
var found;
if (actualIndexes) {
actualIndexes.forEach(function (f) {
if (f.Column_name === propName) {
found = f;
}
});
}
if (!found) {
sql.push('ADD INDEX `' + propName + '` (`' + propName + '`)');
}
});
// remove single-column indexes
if (actualIndexes) {
actualIndexes.forEach(function (f) {
var propName = f.Key_name;
if (propName === 'id') return;
if (m.properties[propName] && !m.properties[propName].index) {
sql.push('DROP INDEX `' + propName + '`');
}
});
}
if (sql.length) { if (sql.length) {
if (checkOnly) { if (checkOnly) {
done(null, true); done(null, true);

View File

@ -39,6 +39,15 @@ getFields = (model, cb) ->
res.forEach (field) -> fields[field.Field] = field res.forEach (field) -> fields[field.Field] = field
cb err, fields cb err, fields
getIndexes = (model, cb) ->
query 'SHOW INDEXES FROM ' + model, (err, res) ->
if err
cb err
else
indexes = {}
res.forEach (index) -> indexes[index.Key_name] = index
cb err, indexes
it 'should run migration', (test) -> it 'should run migration', (test) ->
withBlankDatabase (err) -> withBlankDatabase (err) ->
schema.automigrate -> schema.automigrate ->
@ -143,6 +152,22 @@ it 'should check actuality of schema', (test) ->
test.ok not ok test.ok not ok
test.done() test.done()
it 'should add single-column index', (test) ->
User.defineProperty 'email', type: String, index: true
User.schema.autoupdate (err) ->
return console.log(err) if err
getIndexes 'User', (err, ixs) ->
test.ok ixs.email && ixs.email.Column_name == 'email'
test.done()
it 'should remove single-column index', (test) ->
User.defineProperty 'email', type: String, index: false
User.schema.autoupdate (err) ->
return console.log(err) if err
getIndexes 'User', (err, ixs) ->
test.ok !ixs.email
test.done()
it 'should disconnect when done', (test) -> it 'should disconnect when done', (test) ->
schema.disconnect() schema.disconnect()
test.done() test.done()