Skip to content

Commit

Permalink
fix: add values to enum type
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Aug 25, 2024
1 parent 55ef653 commit a8d0445
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,12 @@ function mixinMigration(MySQL, mysql) {
const colLength = columnMetadata && columnMetadata.dataLength || prop.length || prop.limit;
const colPrecision = columnMetadata && columnMetadata.dataPrecision;
const colScale = columnMetadata && columnMetadata.dataScale;
const colValue = columnMetadata && columnMetadata.value;
// 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 (colValue) return colType + '(' + colValue + ')';
if (colLength) return colType + '(' + colLength + ')';
if (colPrecision && colScale) return colType + '(' + colPrecision + ',' + colScale + ')';
if (colPrecision) return colType + '(' + colPrecision + ')';
Expand Down
31 changes: 30 additions & 1 deletion test/migration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const platform = require('./helpers/platform');
const should = require('./init');
const Schema = require('loopback-datasource-juggler').Schema;

let db, UserData, StringData, NumberData, DateData, DefaultData, SimpleEmployee;
let db, UserData, StringData, NumberData, DateData, DefaultData, SimpleEmployee, SimplePatient;
let mysqlVersion;

describe('migrations', function() {
Expand All @@ -32,6 +32,18 @@ describe('migrations', function() {
});
});

it('Migrating models that has enum', function(done) {
query('describe SimplePatient', function(err, result) {
should.not.exist(err);
should.exist(result);
result[0].Key.should.equal('PRI');
result[0].Type.should.equal('bigint');
result[2].Field.should.equal('type');
result[2].Type.should.equal('enum(\'INPATIENT\',\'OUTPATIENT\')');
done();
});
});

it('UserData should have correct columns', function(done) {
getFields('UserData', function(err, fields) {
if (!fields) return done();
Expand Down Expand Up @@ -603,6 +615,23 @@ function setup(done) {
name: {type: String},
});

SimplePatient = db.define('SimplePatient', {
pid: {type: Number, generated: true, id: true, mysql: {dataType: 'bigint', dataLength: 20}},
name: {type: String},
patient: {
type: String,
mysql: {
columnName: 'type',
dataType: 'enum',
value: "'INPATIENT','OUTPATIENT'",
dataPrecision: null,
dataScale: null,
nullable: 'Y',
generated: false,
},
},
});

query('SELECT VERSION()', function(err, res) {
mysqlVersion = res && res[0] && res[0]['VERSION()'];
blankDatabase(db, done);
Expand Down

0 comments on commit a8d0445

Please sign in to comment.