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) {
var colName = expectedColNameForModel(propName, m);
var pName = self.client.escapeId(colName);
var indexName = self.client.escapeId(propName);
var type = '';
var kind = '';
if (i.type) {
type = 'USING ' + i.type;
}
if (kind && type) {
sql.push('ADD ' + kind + ' INDEX ' + pName +
sql.push('ADD ' + kind + ' INDEX ' + indexName +
' (' + pName + ') ' + type);
} else {
if (typeof i === 'object' && i.unique && i.unique === true) {
kind = 'UNIQUE';
}
sql.push('ADD ' + kind + ' INDEX ' + pName + ' ' + type +
sql.push('ADD ' + kind + ' INDEX ' + indexName + ' ' + type +
' (' + pName + ') ');
}
}

View File

@ -215,59 +215,69 @@ describe('MySQL connector', function() {
ds.createModel(schema_v1.name, schema_v1.properties, schema_v1.options);
ds.automigrate(function() {
ds.discoverModelProperties('customer_test', function(err, props) {
assert.equal(props.length, 5);
var names = props.map(function(p) {
return p.columnName;
});
assert.equal(props[0].nullable, 'N');
assert.equal(props[1].nullable, 'Y');
assert.equal(props[2].nullable, 'N');
assert.equal(props[3].nullable, 'Y');
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.automigrate(function(err) {
if (err) return done(err);
ds.isActual(function(err, isActual) {
if (err) return done(err);
assert(isActual, 'isActual should return true after automigrate');
ds.discoverModelProperties('customer_test', function(err, props) {
assert.equal(props.length, 5);
var names = props.map(function(p) {
return p.columnName;
});
assert.equal(props[0].nullable, 'N');
assert.equal(props[1].nullable, 'Y');
assert.equal(props[2].nullable, 'N');
assert.equal(props[3].nullable, 'Y');
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) {
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) {
ds.connector.execute('SHOW INDEXES FROM customer_test', function(err, indexes) {
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);
assert.equal(props.length, 7);
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) {
ds.isActual(function(err, isActual) {
if (err) return done(err);
assert(updatedindexes);
assert(updatedindexes.length.should.be.above(3));
assert.equal(updatedindexes[1].Key_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);
assert(isActual, 'isActual should return true after autoupdate');
ds.discoverModelProperties('customer_test', function(err, props) {
if (err) return done(err);
assert.equal(props.length, 7);
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);
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);
});
});
});
});
});