fix: enable migration for enum property

Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
This commit is contained in:
Muhammad Aaqil 2024-04-14 23:23:42 +05:00
parent 8812d51862
commit aa62346c40
2 changed files with 20 additions and 1 deletions

View File

@ -770,10 +770,21 @@ function mixinMigration(MySQL, mysql) {
const colLength = columnMetadata && columnMetadata.dataLength || prop.length || prop.limit; const colLength = columnMetadata && columnMetadata.dataLength || prop.length || prop.limit;
const colPrecision = columnMetadata && columnMetadata.dataPrecision; const colPrecision = columnMetadata && columnMetadata.dataPrecision;
const colScale = columnMetadata && columnMetadata.dataScale; const colScale = columnMetadata && columnMetadata.dataScale;
let enumList = '';
if (colType && colType === 'ENUM') {
if (prop.jsonSchema && prop.jsonSchema.enum) {
prop.jsonSchema.enum.forEach(item => {
enumList += `'${item}',`;
});
// remove trailing comma
enumList = enumList.substring(0, enumList.length - 1);
}
}
// info on setting column specific properties // info on setting column specific properties
// i.e dataLength, dataPrecision, dataScale // i.e dataLength, dataPrecision, dataScale
// https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html // https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html
if (colType) { if (colType) {
if (colType === 'ENUM') return colType + '(' + enumList + ')';
if (colLength) return colType + '(' + colLength + ')'; if (colLength) return colType + '(' + colLength + ')';
if (colPrecision && colScale) return colType + '(' + colPrecision + ',' + colScale + ')'; if (colPrecision && colScale) return colType + '(' + colPrecision + ',' + colScale + ')';
if (colPrecision) return colType + '(' + colPrecision + ')'; if (colPrecision) return colType + '(' + colPrecision + ')';

View File

@ -85,6 +85,13 @@ describe('migrations', function() {
Key: '', Key: '',
Default: null, Default: null,
Extra: ''}, Extra: ''},
typings: {
Field: 'typings',
Type: "enum('A','B')",
Null: 'YES',
Key: '',
Default: null,
Extra: ''},
}); });
done(); done();
}); });
@ -548,9 +555,10 @@ function setup(done) {
require('./init.js'); require('./init.js');
db = global.getSchema(); db = global.getSchema();
const customType = db.EnumFactory('A', 'B');
UserData = db.define('UserData', { UserData = db.define('UserData', {
email: {type: String, null: false, index: true}, email: {type: String, null: false, index: true},
typings: {type: customType},
name: String, name: String,
bio: Schema.Text, bio: Schema.Text,
birthDate: Date, birthDate: Date,