不要怂,就是干,撸起袖子干!

Commit a2ae2b9b by Mick Hansen

Merge branch 'bulkan-bug/postgres-array-of-json'

2 parents 02290ce8 d3a391ee
......@@ -6,6 +6,7 @@
- [FEATURE] Now possible to pass `createdAt` and `updatedAt` values to `Model.create`/`Model.bulkCreate` when using silent: true (when importing datasets with existing timestamps)
- [FEATURE] `instance.update()` using default fields will now automatically also save values provided via `beforeUpdate` hooks
- [BUG] Fixed bad SQL when updating a JSON attribute with a different `field`
- [BUG] Fixed issue with creating and updating values of a `DataTypes.ARRAY(DataTypes.JSON)` attribute
#### Backwards compatability changes
- `instance.update()` using default fields will now automatically also save values provided via `beforeUpdate` hooks
......
......@@ -345,6 +345,11 @@ module.exports = {
*/
BIGINT: BIGINT,
/**
* A time column
* @property TIME
*/
TIME: 'TIME',
/**
* A datetime column
* @property DATE
*/
......
......@@ -891,13 +891,17 @@ module.exports = (function() {
}
if (Utils._.isObject(value) && field && (field.type === DataTypes.HSTORE || field.type === DataTypes.ARRAY(DataTypes.HSTORE))) {
if(field.type === DataTypes.HSTORE){
if (field.type === DataTypes.HSTORE){
return "'" + hstore.stringify(value) + "'";
}else if (field.type === DataTypes.ARRAY(DataTypes.HSTORE)){
} else if (field.type === DataTypes.ARRAY(DataTypes.HSTORE)){
return "ARRAY[" + Utils._.map(value, function(v){return "'" + hstore.stringify(v) + "'::hstore";}).join(",") + "]::HSTORE[]";
}
} else if (Utils._.isObject(value) && field && (field.type === DataTypes.JSON || field.type === DataTypes.JSONB)) {
value = JSON.stringify(value);
} else if (Array.isArray(value) && field.type === DataTypes.ARRAY(DataTypes.JSON)) {
return "ARRAY[" + value.map(function (v) {
return SqlString.escape(JSON.stringify(v), false, this.options.timezone, this.dialect, field);
}, this).join(",") + "]::JSON[]";
}
return SqlString.escape(value, false, this.options.timezone, this.dialect, field);
......
......@@ -61,6 +61,7 @@ describe(Support.getTestDialectTeaser('DataTypes'), function() {
[Sequelize.TEXT, 'TEXT', 'TEXT'],
[Sequelize.DATE, 'DATE', 'DATETIME'],
[Sequelize.TIME, 'TIME', 'TIME'],
[Sequelize.NOW, 'NOW', 'NOW'],
[Sequelize.UUID, 'UUID', 'UUID'],
[Sequelize.BOOLEAN, 'BOOLEAN', 'TINYINT(1)'],
......
......@@ -20,7 +20,11 @@ if (dialect.match(/^postgres/)) {
settings: DataTypes.HSTORE,
document: { type: DataTypes.HSTORE, defaultValue: { default: "'value'" } },
phones: DataTypes.ARRAY(DataTypes.HSTORE),
emergency_contact: DataTypes.JSON
emergency_contact: DataTypes.JSON,
friends: {
type: DataTypes.ARRAY(DataTypes.JSON),
defaultValue: []
}
});
this.User.sync({ force: true }).success(function() {
done();
......@@ -34,11 +38,35 @@ if (dialect.match(/^postgres/)) {
it('should be able to search within an array', function(done) {
this.User.all({where: {email: ['hello', 'world']}}).on('sql', function(sql) {
expect(sql).to.equal('SELECT "id", "username", "email", "settings", "document", "phones", "emergency_contact", "createdAt", "updatedAt" FROM "Users" AS "User" WHERE "User"."email" = ARRAY[\'hello\',\'world\']::TEXT[];');
expect(sql).to.equal('SELECT "id", "username", "email", "settings", "document", "phones", "emergency_contact", "friends", "createdAt", "updatedAt" FROM "Users" AS "User" WHERE "User"."email" = ARRAY[\'hello\',\'world\']::TEXT[];');
done();
});
});
it('should be able to update a field with type ARRAY(JSON)', function(){
return this.User.create({
username: 'bob',
email: ['myemail@email.com'],
friends: [{
name: 'John Smith'
}]
}).then(function(userInstance){
expect(userInstance.friends).to.have.length(1);
expect(userInstance.friends[0].name).to.equal('John Smith');
return userInstance.update({
friends: [{
name: 'John Smythe'
}]
});
})
.get('friends')
.tap(function(friends){
expect(friends).to.have.length(1);
expect(friends[0].name).to.equal('John Smythe');
});
});
it('should be able to find a record while searching in an array', function(done) {
var self = this;
this.User.bulkCreate([
......@@ -66,9 +94,11 @@ if (dialect.match(/^postgres/)) {
return this.User.create({
username: 'bob',
emergency_contact: { name: 'joe', phones: [1337, 42] }
}, {
fields: ['id', 'username', 'document', 'emergency_contact']
}).on('sql', function(sql) {
var expected = 'INSERT INTO "Users" ("id","username","document","emergency_contact","createdAt","updatedAt") VALUES (DEFAULT,\'bob\',\'"default"=>"\'\'value\'\'"\',\'{"name":"joe","phones":[1337,42]}\'';
expect(sql.indexOf(expected)).to.equal(0);
var expected = '\'{"name":"joe","phones":[1337,42]}\'';
expect(sql.indexOf(expected)).not.to.equal(-1);
});
});
......@@ -226,8 +256,8 @@ if (dialect.match(/^postgres/)) {
email: ['myemail@email.com'],
settings: {mailing: false, push: 'facebook', frequency: 3}
}).on('sql', function(sql) {
var expected = 'INSERT INTO "Users" ("id","username","email","settings","document","createdAt","updatedAt") VALUES (DEFAULT,\'bob\',ARRAY[\'myemail@email.com\']::TEXT[],\'"mailing"=>"false","push"=>"facebook","frequency"=>"3"\',\'"default"=>"\'\'value\'\'"\'';
expect(sql.indexOf(expected)).to.equal(0);
var expected = '\'"mailing"=>"false","push"=>"facebook","frequency"=>"3"\',\'"default"=>"\'\'value\'\'"\'';
expect(sql.indexOf(expected)).not.to.equal(-1);
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!