Skip to content

Commit

Permalink
fix: normalize attribute defaultValue
Browse files Browse the repository at this point in the history
... to ignore defaults like `CURRENT_TIMESTAMP`
  • Loading branch information
cyjake committed Mar 1, 2022
1 parent 6c8c347 commit 4341ec4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ class Bone {
for (const name in attributes) {
const value = this.attribute(name);
const { defaultValue } = attributes[name];
// console.log(attributes[name], name, defaultValue);
if (value != null) {
data[name] = value;
} else if (value === undefined && defaultValue != null) {
Expand Down
10 changes: 9 additions & 1 deletion src/drivers/abstract/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,23 @@ class Attribute {
}
const type = createType(DataTypes, params);
const dataType = params.dataType || type.dataType;
let { defaultValue = null } = params;
try {
// normalize column defaults like `'0'` or `CURRENT_TIMESTAMP`
defaultValue = type.cast(type.uncast(defaultValue));
} catch {
defaultValue = null;
}

Object.assign(this, {
name,
columnName,
primaryKey: false,
defaultValue: null,
allowNull: !params.primaryKey,
columnType: type.toSqlString().toLowerCase(),
...params,
type,
defaultValue,
dataType,
jsType: findJsType(DataTypes, type, dataType),
});
Expand Down
1 change: 1 addition & 0 deletions test/dumpfile.sql
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ DROP TABLE IF EXISTS `likes`;
CREATE TABLE `likes` (
`id` bigint(20) AUTO_INCREMENT PRIMARY KEY,
`gmt_create` timestamp(3) NULL,
`gmt_modified` timestamp DEFAULT CURRENT_TIMESTAMP,
`article_id` bigint(20) NOT NULL,
`user_id` bigint(20) NOT NULL,
`gmt_deleted` timestamp(3) NULL
Expand Down
12 changes: 6 additions & 6 deletions test/unit/adapters/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,7 @@ describe('Transaction', function() {

describe('mysql only', () => {
const Spine = sequelize(Bone);

class Post extends Spine {
static get table() {
return 'articles';
Expand All @@ -1940,9 +1940,9 @@ describe('mysql only', () => {
});

describe('Model.update with order, limit (mysql only)', () => {

it('should work', async () => {

let i = 0;
while (i <= 5) {
await Post.create({ title: 'Throne' });
Expand All @@ -1959,7 +1959,7 @@ describe('mysql only', () => {
assert.equal(allPosts[1].title, 'Game');
assert.equal(allPosts[2].title, 'Throne');
assert.equal(allPosts[3].title, 'Throne');

await Post.bulkUpdate({ title: 'Pilot' }, {
where: {},
limit: 2,
Expand All @@ -1976,9 +1976,9 @@ describe('mysql only', () => {
});

describe('Model.destroy with order, limit (mysql only)', () => {

it('should work', async () => {

let i = 0;
const posts = [];
while (i <= 5) {
Expand Down
12 changes: 10 additions & 2 deletions test/unit/drivers/mysql/attribute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const assert = require('assert').strict;
const Attribute = require('../../../../src/drivers/mysql/attribute');
const { BOOLEAN, JSONB } = Attribute.DataTypes;
const { BOOLEAN, DATE, JSONB } = Attribute.DataTypes;

describe('=> Attribute (mysql)', function() {
it('should support TINYINT(1)', async function() {
const attribute= new Attribute('has_image', {
const attribute = new Attribute('has_image', {
type: BOOLEAN,
defaultValue: false,
});
Expand All @@ -17,4 +17,12 @@ describe('=> Attribute (mysql)', function() {
const attribute = new Attribute('params', { type: JSONB });
assert.equal(attribute.toSqlString(), '`params` JSON');
});

it('should normalize attribute defaultValue', async function() {
const attribute = new Attribute('createdAt', {
type: DATE,
defaultValue: 'CURRENT_TIMESTAMP',
});
assert.equal(attribute.defaultValue, null);
});
});

0 comments on commit 4341ec4

Please sign in to comment.