Merge pull request #182 from strongloop/issue109indexconfig
Fix to configure model index in keys field
This commit is contained in:
commit
2c4b2f86b1
|
@ -185,10 +185,28 @@ function mixinMigration(MySQL, mysql) {
|
||||||
// second: check multiple indexes
|
// second: check multiple indexes
|
||||||
var orderMatched = true;
|
var orderMatched = true;
|
||||||
if (indexNames.indexOf(indexName) !== -1) {
|
if (indexNames.indexOf(indexName) !== -1) {
|
||||||
m.settings.indexes[indexName].columns.split(/,\s*/).forEach(
|
//check if indexes are configured as "columns"
|
||||||
function(columnName, i) {
|
if (m.settings.indexes[indexName].columns) {
|
||||||
if (ai[indexName].columns[i] !== columnName) orderMatched = false;
|
m.settings.indexes[indexName].columns.split(/,\s*/).forEach(
|
||||||
});
|
function(columnName, i) {
|
||||||
|
if (ai[indexName].columns[i] !== columnName) orderMatched = false;
|
||||||
|
});
|
||||||
|
} else if (m.settings.indexes[indexName].keys) {
|
||||||
|
//if indexes are configured as "keys"
|
||||||
|
var index = 0;
|
||||||
|
for (var key in m.settings.indexes[indexName].keys) {
|
||||||
|
var sortOrder = m.settings.indexes[indexName].keys[key];
|
||||||
|
if (ai[indexName].columns[index] !== key) {
|
||||||
|
orderMatched = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
//if number of columns differ between new and old index
|
||||||
|
if (index !== ai[indexName].columns.length) {
|
||||||
|
orderMatched = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!orderMatched) {
|
if (!orderMatched) {
|
||||||
sql.push('DROP INDEX ' + self.client.escapeId(indexName));
|
sql.push('DROP INDEX ' + self.client.escapeId(indexName));
|
||||||
|
@ -237,13 +255,35 @@ function mixinMigration(MySQL, mysql) {
|
||||||
}
|
}
|
||||||
if (i.kind) {
|
if (i.kind) {
|
||||||
kind = i.kind;
|
kind = i.kind;
|
||||||
|
} else if (i.options && i.options.unique && i.options.unique == true) {
|
||||||
|
//if index unique indicator is configured
|
||||||
|
kind = 'UNIQUE';
|
||||||
|
}
|
||||||
|
|
||||||
|
var indexedColumns = [];
|
||||||
|
var columns = '';
|
||||||
|
//if indexes are configured as "keys"
|
||||||
|
if (i.keys) {
|
||||||
|
for (var key in i.keys) {
|
||||||
|
if (i.keys[key] !== -1) {
|
||||||
|
indexedColumns.push(key);
|
||||||
|
} else {
|
||||||
|
indexedColumns.push(key + ' DESC ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (indexedColumns.length > 0) {
|
||||||
|
columns = indexedColumns.join(',');
|
||||||
|
} else if (i.columns) {
|
||||||
|
//if indexes are configured as "columns"
|
||||||
|
columns = i.columns;
|
||||||
}
|
}
|
||||||
if (kind && type) {
|
if (kind && type) {
|
||||||
sql.push('ADD ' + kind + ' INDEX ' + iName +
|
sql.push('ADD ' + kind + ' INDEX ' + iName +
|
||||||
' (' + i.columns + ') ' + type);
|
' (' + columns + ') ' + type);
|
||||||
} else {
|
} else {
|
||||||
sql.push('ADD ' + kind + ' INDEX ' + type + ' ' + iName +
|
sql.push('ADD ' + kind + ' INDEX ' + type + ' ' + iName +
|
||||||
' (' + i.columns + ')');
|
' (' + columns + ')');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -370,23 +410,43 @@ function mixinMigration(MySQL, mysql) {
|
||||||
type = 'USING ' + i.type;
|
type = 'USING ' + i.type;
|
||||||
}
|
}
|
||||||
if (i.kind) {
|
if (i.kind) {
|
||||||
|
//if index uniqueness is configured as "kind"
|
||||||
kind = i.kind;
|
kind = i.kind;
|
||||||
|
} else if (i.options && i.options.unique && i.options.unique == true) {
|
||||||
|
//if index unique indicator is configured
|
||||||
|
kind = 'UNIQUE';
|
||||||
}
|
}
|
||||||
var indexedColumns = [];
|
var indexedColumns = [];
|
||||||
var indexName = this.escapeName(index);
|
var indexName = this.escapeName(index);
|
||||||
if (Array.isArray(i.keys)) {
|
var columns = '';
|
||||||
indexedColumns = i.keys.map(function(key) {
|
//if indexes are configured as "keys"
|
||||||
return self.columnEscaped(model, key);
|
if (i.keys) {
|
||||||
});
|
//for each field in "keys" object
|
||||||
|
for (var key in i.keys) {
|
||||||
|
if (i.keys[key] !== -1) {
|
||||||
|
indexedColumns.push(key);
|
||||||
|
} else {
|
||||||
|
//mysql does not support index sorting Currently
|
||||||
|
//but mysql has added DESC keyword for future support
|
||||||
|
indexedColumns.push(key + ' DESC ');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var columns = indexedColumns.join(',') || i.columns;
|
if (indexedColumns.length) {
|
||||||
if (kind && type) {
|
columns = indexedColumns.join(',');
|
||||||
indexClauses.push(kind + ' INDEX ' + indexName + ' (' + columns + ') ' + type);
|
} else if (i.columns) {
|
||||||
} else {
|
columns = i.columns;
|
||||||
indexClauses.push(kind + ' INDEX ' + type + ' ' + indexName + ' (' + columns + ')');
|
}
|
||||||
|
if (columns.length) {
|
||||||
|
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
|
// Define index for each of the properties
|
||||||
for (var p in definition.properties) {
|
for (var p in definition.properties) {
|
||||||
var propIndex = self.buildIndex(model, p);
|
var propIndex = self.buildIndex(model, p);
|
||||||
|
|
|
@ -23,6 +23,16 @@ describe('MySQL connector', function() {
|
||||||
'schema': 'myapp_test',
|
'schema': 'myapp_test',
|
||||||
'table': 'customer_test',
|
'table': 'customer_test',
|
||||||
},
|
},
|
||||||
|
'indexes': {
|
||||||
|
'name_index': {
|
||||||
|
'keys': {
|
||||||
|
'name': 1,
|
||||||
|
},
|
||||||
|
'options': {
|
||||||
|
'unique': true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
'properties': {
|
'properties': {
|
||||||
'id': {
|
'id': {
|
||||||
|
@ -56,6 +66,17 @@ describe('MySQL connector', function() {
|
||||||
'schema': 'myapp_test',
|
'schema': 'myapp_test',
|
||||||
'table': 'customer_test',
|
'table': 'customer_test',
|
||||||
},
|
},
|
||||||
|
'indexes': {
|
||||||
|
'updated_name_index': {
|
||||||
|
'keys': {
|
||||||
|
'firstName': 1,
|
||||||
|
'lastName': -1,
|
||||||
|
},
|
||||||
|
'options': {
|
||||||
|
'unique': true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
'properties': {
|
'properties': {
|
||||||
'id': {
|
'id': {
|
||||||
|
@ -104,19 +125,38 @@ describe('MySQL connector', function() {
|
||||||
assert.equal(names[2], 'email');
|
assert.equal(names[2], 'email');
|
||||||
assert.equal(names[3], 'age');
|
assert.equal(names[3], 'age');
|
||||||
|
|
||||||
ds.createModel(schema_v2.name, schema_v2.properties, schema_v2.options);
|
ds.connector.execute('SHOW INDEXES FROM customer_test', function(err, indexes) {
|
||||||
|
if (err) return done (err);
|
||||||
ds.autoupdate(function(err, result) {
|
assert(indexes);
|
||||||
ds.discoverModelProperties('customer_test', function(err, props) {
|
assert(indexes.length.should.be.above(1));
|
||||||
assert.equal(props.length, 4);
|
assert.equal(indexes[1].Key_name, 'name_index');
|
||||||
var names = props.map(function(p) {
|
assert.equal(indexes[1].Non_unique, 0);
|
||||||
return p.columnName;
|
ds.createModel(schema_v2.name, schema_v2.properties, schema_v2.options);
|
||||||
|
ds.autoupdate(function(err, result) {
|
||||||
|
if (err) return done (err);
|
||||||
|
ds.discoverModelProperties('customer_test', function(err, props) {
|
||||||
|
if (err) return done (err);
|
||||||
|
assert.equal(props.length, 4);
|
||||||
|
var names = props.map(function(p) {
|
||||||
|
return p.columnName;
|
||||||
|
});
|
||||||
|
assert.equal(names[0], 'id');
|
||||||
|
assert.equal(names[1], 'email');
|
||||||
|
assert.equal(names[2], 'firstName');
|
||||||
|
assert.equal(names[3], 'lastName');
|
||||||
|
ds.connector.execute('SHOW INDEXES FROM customer_test', function(err, updatedindexes) {
|
||||||
|
if (err) return done (err);
|
||||||
|
assert(updatedindexes);
|
||||||
|
assert(updatedindexes.length.should.be.above(2));
|
||||||
|
assert.equal(updatedindexes[1].Key_name, 'updated_name_index');
|
||||||
|
assert.equal(updatedindexes[2].Key_name, 'updated_name_index');
|
||||||
|
//Mysql supports only index sorting in ascending; DESC is ignored
|
||||||
|
assert.equal(updatedindexes[1].Collation, 'A');
|
||||||
|
assert.equal(updatedindexes[2].Collation, 'A');
|
||||||
|
assert.equal(updatedindexes[1].Non_unique, 0);
|
||||||
|
done(err, result);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
assert.equal(names[0], 'id');
|
|
||||||
assert.equal(names[1], 'email');
|
|
||||||
assert.equal(names[2], 'firstName');
|
|
||||||
assert.equal(names[3], 'lastName');
|
|
||||||
done(err, result);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue