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: enable migration for enum property #582

Merged
merged 1 commit into from
Sep 1, 2024
Merged
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
11 changes: 11 additions & 0 deletions lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,21 @@ 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}',`;
});
// remove trailing comma
enumList = enumList.substring(0, enumList.length - 1);
}
}
// 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 + ')';
Expand Down
10 changes: 9 additions & 1 deletion test/migration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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,
Expand Down