From 2ba88203f3e7a6a0360812be36d48719508a7b4f Mon Sep 17 00:00:00 2001 From: Sakib Hasan Date: Fri, 19 May 2017 11:57:06 -0400 Subject: [PATCH] Escape index names (#280) * Escape index names * Add test cases --- lib/migration.js | 4 +-- test/mysql.autoupdate.test.js | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/migration.js b/lib/migration.js index 0407ef9..ce121ae 100644 --- a/lib/migration.js +++ b/lib/migration.js @@ -662,11 +662,11 @@ function mixinMigration(MySQL, mysql) { //for each field in "keys" object for (var key in i.keys) { if (i.keys[key] !== -1) { - indexedColumns.push(key); + indexedColumns.push(this.escapeName(key)); } else { //mysql does not support index sorting Currently //but mysql has added DESC keyword for future support - indexedColumns.push(key + ' DESC '); + indexedColumns.push(this.escapeName(key) + ' DESC '); } } } diff --git a/test/mysql.autoupdate.test.js b/test/mysql.autoupdate.test.js index 9cbb952..5103a70 100644 --- a/test/mysql.autoupdate.test.js +++ b/test/mysql.autoupdate.test.js @@ -17,6 +17,56 @@ describe('MySQL connector', function() { setupAltColNameData(); }); + describe('escape index names upon automigrate', function() { + before (function(done) { + var messageSchema = { + 'name': 'Message', + 'options': { + 'idInjection': false, + 'indexes': { + 'id_index': { + 'keys': { + 'id': 1, + }, + }, + 'from_index': { + 'keys': { + 'from': 1, + }, + }, + }, + }, + 'properties': { + 'id': { + 'type': 'number', + 'id': true, + 'generated': false, + }, + 'conversation': { + 'type': 'string', + }, + 'from': { + 'type': 'number', + }, + 'sent': { + 'type': 'date', + }, + }, + }; + ds.createModel(messageSchema.name, messageSchema.properties, messageSchema.options); + done(); + }); + it('should escape index names', function(done) { + // please note `from` is a keyword in mysql https://dev.mysql.com/doc/refman/5.7/en/keywords.html + // hence it needs to be escaped in order for it to work + // instead of escaping the special keywords, we escape all index names + ds.automigrate('Message', function(err) { + assert(!err); + done(); + }); + }); + }); + it('should auto migrate/update tables', function(done) { var schema_v1 = { 'name': 'CustomerTest',