Skip to content

Commit a8d0445

Browse files
committed
fix: add values to enum type
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 55ef653 commit a8d0445

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/migration.js

+2
Original file line numberDiff line numberDiff line change
@@ -764,10 +764,12 @@ function mixinMigration(MySQL, mysql) {
764764
const colLength = columnMetadata && columnMetadata.dataLength || prop.length || prop.limit;
765765
const colPrecision = columnMetadata && columnMetadata.dataPrecision;
766766
const colScale = columnMetadata && columnMetadata.dataScale;
767+
const colValue = columnMetadata && columnMetadata.value;
767768
// info on setting column specific properties
768769
// i.e dataLength, dataPrecision, dataScale
769770
// https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html
770771
if (colType) {
772+
if (colValue) return colType + '(' + colValue + ')';
771773
if (colLength) return colType + '(' + colLength + ')';
772774
if (colPrecision && colScale) return colType + '(' + colPrecision + ',' + colScale + ')';
773775
if (colPrecision) return colType + '(' + colPrecision + ')';

test/migration.test.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const platform = require('./helpers/platform');
1010
const should = require('./init');
1111
const Schema = require('loopback-datasource-juggler').Schema;
1212

13-
let db, UserData, StringData, NumberData, DateData, DefaultData, SimpleEmployee;
13+
let db, UserData, StringData, NumberData, DateData, DefaultData, SimpleEmployee, SimplePatient;
1414
let mysqlVersion;
1515

1616
describe('migrations', function() {
@@ -32,6 +32,18 @@ describe('migrations', function() {
3232
});
3333
});
3434

35+
it('Migrating models that has enum', function(done) {
36+
query('describe SimplePatient', function(err, result) {
37+
should.not.exist(err);
38+
should.exist(result);
39+
result[0].Key.should.equal('PRI');
40+
result[0].Type.should.equal('bigint');
41+
result[2].Field.should.equal('type');
42+
result[2].Type.should.equal('enum(\'INPATIENT\',\'OUTPATIENT\')');
43+
done();
44+
});
45+
});
46+
3547
it('UserData should have correct columns', function(done) {
3648
getFields('UserData', function(err, fields) {
3749
if (!fields) return done();
@@ -603,6 +615,23 @@ function setup(done) {
603615
name: {type: String},
604616
});
605617

618+
SimplePatient = db.define('SimplePatient', {
619+
pid: {type: Number, generated: true, id: true, mysql: {dataType: 'bigint', dataLength: 20}},
620+
name: {type: String},
621+
patient: {
622+
type: String,
623+
mysql: {
624+
columnName: 'type',
625+
dataType: 'enum',
626+
value: "'INPATIENT','OUTPATIENT'",
627+
dataPrecision: null,
628+
dataScale: null,
629+
nullable: 'Y',
630+
generated: false,
631+
},
632+
},
633+
});
634+
606635
query('SELECT VERSION()', function(err, res) {
607636
mysqlVersion = res && res[0] && res[0]['VERSION()'];
608637
blankDatabase(db, done);

0 commit comments

Comments
 (0)