From 3d0dd5f6a097e1e852c368a4daaca1521cba6061 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sun, 14 Apr 2024 23:23:42 +0500 Subject: [PATCH] fix: enable migration for enum property Signed-off-by: Muhammad Aaqil --- lib/migration.js | 9 +++++++++ test/migration.test.js | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/migration.js b/lib/migration.js index 6e69174a..2d700a29 100644 --- a/lib/migration.js +++ b/lib/migration.js @@ -764,10 +764,19 @@ function mixinMigration(MySQL, mysql) { const colLength = columnMetadata && columnMetadata.dataLength || prop.length || prop.limit; const colPrecision = columnMetadata && columnMetadata.dataPrecision; const colScale = columnMetadata && columnMetadata.dataScale; + let enumList = ''; + if (colType && colType === 'ENUM') { + if (prop.jsonSchema && prop.jsonSchema.enum) { + prop.jsonSchema.enum.forEach(item => { + enumList += `'${item}'`; + }); + } + } // info on setting column specific properties // i.e dataLength, dataPrecision, dataScale // https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html if (colType) { + if (colType === 'ENUM') return colType + '(' + enumList + ')'; if (colLength) return colType + '(' + colLength + ')'; if (colPrecision && colScale) return colType + '(' + colPrecision + ',' + colScale + ')'; if (colPrecision) return colType + '(' + colPrecision + ')'; diff --git a/test/migration.test.js b/test/migration.test.js index 622f1e95..b7345ce7 100644 --- a/test/migration.test.js +++ b/test/migration.test.js @@ -85,6 +85,13 @@ describe('migrations', function() { Key: '', Default: null, Extra: ''}, + typings: { + Field: 'typings', + Type: "enum('A','B')", + Null: 'YES', + Key: '', + Default: null, + Extra: ''}, }); done(); }); @@ -548,9 +555,10 @@ function setup(done) { require('./init.js'); db = global.getSchema(); - + const customType = db.EnumFactory('A', 'B'); UserData = db.define('UserData', { email: {type: String, null: false, index: true}, + typings: {type: customType}, name: String, bio: Schema.Text, birthDate: Date,