Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add values to enum type #598

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading