Skip to content

Commit

Permalink
Single-column indexes in mysql (autoupdate)
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Aug 17, 2012
1 parent 62178c2 commit 62d0e45
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
45 changes: 38 additions & 7 deletions lib/adapters/mysql.js
Expand Up @@ -293,11 +293,13 @@ MySQL.prototype.autoupdate = function (cb) {
Object.keys(this._models).forEach(function (model) {
wait += 1;
self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) {
if (!err && fields.length) {
self.alterTable(model, fields, done);
} else {
self.createTable(model, done);
}
self.query('SHOW INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) {
if (!err && fields.length) {
self.alterTable(model, fields, indexes, done);
} else {
self.createTable(model, done);
}
});
});
});

Expand All @@ -318,7 +320,7 @@ MySQL.prototype.isActual = function (cb) {
Object.keys(this._models).forEach(function (model) {
wait += 1;
self.query('SHOW FIELDS FROM ' + model, function (err, fields) {
self.alterTable(model, fields, done, true);
self.alterTable(model, fields, null, done, true);
});
});

Expand All @@ -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 m = this._models[model];
var propNames = Object.keys(m.properties).filter(function (name) {
Expand Down Expand Up @@ -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 (checkOnly) {
done(null, true);
Expand Down
25 changes: 25 additions & 0 deletions test/migration_test.coffee
Expand Up @@ -39,6 +39,15 @@ getFields = (model, cb) ->
res.forEach (field) -> fields[field.Field] = field
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) ->
withBlankDatabase (err) ->
schema.automigrate ->
Expand Down Expand Up @@ -143,6 +152,22 @@ it 'should check actuality of schema', (test) ->
test.ok not ok
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) ->
schema.disconnect()
test.done()
Expand Down

0 comments on commit 62d0e45

Please sign in to comment.