Fix bug property declared index being recreate

This commit is contained in:
HugoPoi 2018-12-04 16:18:31 +01:00 committed by jannyHou
parent 681bff4586
commit 24fe635de5
2 changed files with 62 additions and 51 deletions

View File

@ -364,19 +364,20 @@ function mixinMigration(MySQL, mysql) {
if (!found) { if (!found) {
var colName = expectedColNameForModel(propName, m); var colName = expectedColNameForModel(propName, m);
var pName = self.client.escapeId(colName); var pName = self.client.escapeId(colName);
var indexName = self.client.escapeId(propName);
var type = ''; var type = '';
var kind = ''; var kind = '';
if (i.type) { if (i.type) {
type = 'USING ' + i.type; type = 'USING ' + i.type;
} }
if (kind && type) { if (kind && type) {
sql.push('ADD ' + kind + ' INDEX ' + pName + sql.push('ADD ' + kind + ' INDEX ' + indexName +
' (' + pName + ') ' + type); ' (' + pName + ') ' + type);
} else { } else {
if (typeof i === 'object' && i.unique && i.unique === true) { if (typeof i === 'object' && i.unique && i.unique === true) {
kind = 'UNIQUE'; kind = 'UNIQUE';
} }
sql.push('ADD ' + kind + ' INDEX ' + pName + ' ' + type + sql.push('ADD ' + kind + ' INDEX ' + indexName + ' ' + type +
' (' + pName + ') '); ' (' + pName + ') ');
} }
} }

View File

@ -215,59 +215,69 @@ describe('MySQL connector', function() {
ds.createModel(schema_v1.name, schema_v1.properties, schema_v1.options); ds.createModel(schema_v1.name, schema_v1.properties, schema_v1.options);
ds.automigrate(function() { ds.automigrate(function(err) {
ds.discoverModelProperties('customer_test', function(err, props) { if (err) return done(err);
assert.equal(props.length, 5); ds.isActual(function(err, isActual) {
var names = props.map(function(p) { if (err) return done(err);
return p.columnName; assert(isActual, 'isActual should return true after automigrate');
}); ds.discoverModelProperties('customer_test', function(err, props) {
assert.equal(props[0].nullable, 'N'); assert.equal(props.length, 5);
assert.equal(props[1].nullable, 'Y'); var names = props.map(function(p) {
assert.equal(props[2].nullable, 'N'); return p.columnName;
assert.equal(props[3].nullable, 'Y'); });
assert.equal(names[0], 'id'); assert.equal(props[0].nullable, 'N');
assert.equal(names[1], 'name'); assert.equal(props[1].nullable, 'Y');
assert.equal(names[2], 'email'); assert.equal(props[2].nullable, 'N');
assert.equal(names[3], 'age'); assert.equal(props[3].nullable, 'Y');
assert.equal(names[4], 'customer_discount'); assert.equal(names[0], 'id');
assert.equal(names[1], 'name');
assert.equal(names[2], 'email');
assert.equal(names[3], 'age');
assert.equal(names[4], 'customer_discount');
ds.connector.execute('SHOW INDEXES FROM customer_test', function(err, indexes) { ds.connector.execute('SHOW INDEXES FROM customer_test', function(err, indexes) {
if (err) return done(err);
assert(indexes);
assert(indexes.length.should.be.above(1));
assert.equal(indexes[1].Key_name, 'name_index');
assert.equal(indexes[1].Non_unique, 0);
ds.createModel(schema_v2.name, schema_v2.properties, schema_v2.options);
ds.autoupdate(function(err, result) {
if (err) return done(err); if (err) return done(err);
ds.discoverModelProperties('customer_test', function(err, props) { assert(indexes);
assert(indexes.length.should.be.above(1));
assert.equal(indexes[1].Key_name, 'name_index');
assert.equal(indexes[1].Non_unique, 0);
ds.createModel(schema_v2.name, schema_v2.properties, schema_v2.options);
ds.autoupdate(function(err, result) {
if (err) return done(err); if (err) return done(err);
assert.equal(props.length, 7); ds.isActual(function(err, isActual) {
var names = props.map(function(p) {
return p.columnName;
});
assert.equal(names[0], 'id');
assert.equal(names[1], 'email');
assert.equal(names[2], 'customer_discount');
assert.equal(names[3], 'firstName');
assert.equal(names[4], 'lastName');
assert.equal(names[5], 'customer_address');
assert.equal(names[6], 'customer_code');
ds.connector.execute('SHOW INDEXES FROM customer_test', function(err, updatedindexes) {
if (err) return done(err); if (err) return done(err);
assert(updatedindexes); assert(isActual, 'isActual should return true after autoupdate');
assert(updatedindexes.length.should.be.above(3)); ds.discoverModelProperties('customer_test', function(err, props) {
assert.equal(updatedindexes[1].Key_name, 'customer_code'); if (err) return done(err);
assert.equal(updatedindexes[2].Key_name, 'updated_name_index'); assert.equal(props.length, 7);
assert.equal(updatedindexes[3].Key_name, 'updated_name_index'); var names = props.map(function(p) {
// Mysql supports only index sorting in ascending; DESC is ignored return p.columnName;
assert.equal(updatedindexes[1].Collation, 'A'); });
assert.equal(updatedindexes[2].Collation, 'A'); assert.equal(names[0], 'id');
assert.equal(updatedindexes[3].Collation, 'A'); assert.equal(names[1], 'email');
assert.equal(updatedindexes[1].Non_unique, 0); assert.equal(names[2], 'customer_discount');
assert.equal(updatedindexes[2].Non_unique, 0); assert.equal(names[3], 'firstName');
assert.equal(updatedindexes[3].Non_unique, 0); assert.equal(names[4], 'lastName');
done(err, result); assert.equal(names[5], 'customer_address');
assert.equal(names[6], 'customer_code');
ds.connector.execute('SHOW INDEXES FROM customer_test', function(err, updatedindexes) {
if (err) return done(err);
assert(updatedindexes);
assert(updatedindexes.length.should.be.above(3));
assert.equal(updatedindexes[1].Key_name, 'code');
assert.equal(updatedindexes[1].Column_name, 'customer_code');
assert.equal(updatedindexes[2].Key_name, 'updated_name_index');
assert.equal(updatedindexes[3].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[3].Collation, 'A');
assert.equal(updatedindexes[1].Non_unique, 0);
assert.equal(updatedindexes[2].Non_unique, 0);
assert.equal(updatedindexes[3].Non_unique, 0);
done(err, result);
});
});
}); });
}); });
}); });