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

Commit dd8782ce by Jan Aagaard Meier

Merge pull request #1458 from tomchentw/hotfix/hstore_checking_fn

fix(dao): HSTORE checking like ENUM
2 parents 7e82661d e97fb601
...@@ -295,8 +295,8 @@ module.exports = (function() { ...@@ -295,8 +295,8 @@ module.exports = (function() {
for (var attrName in self.Model.rawAttributes) { for (var attrName in self.Model.rawAttributes) {
if (self.Model.rawAttributes.hasOwnProperty(attrName)) { if (self.Model.rawAttributes.hasOwnProperty(attrName)) {
var definition = self.Model.rawAttributes[attrName] var definition = self.Model.rawAttributes[attrName]
, isHstore = !!definition.type && !!definition.type.type && definition.type.type === DataTypes.HSTORE.type , isHstore = !!definition.type && (definition.type.toString() === DataTypes.HSTORE.toString())
, isEnum = definition.type && (definition.type.toString() === DataTypes.ENUM.toString()) , isEnum = !!definition.type && (definition.type.toString() === DataTypes.ENUM.toString())
, isMySQL = ['mysql', 'mariadb'].indexOf(self.Model.daoFactoryManager.sequelize.options.dialect) !== -1 , isMySQL = ['mysql', 'mariadb'].indexOf(self.Model.daoFactoryManager.sequelize.options.dialect) !== -1
, ciCollation = !!self.Model.options.collate && self.Model.options.collate.match(/_ci$/i) , ciCollation = !!self.Model.options.collate && self.Model.options.collate.match(/_ci$/i)
, valueOutOfScope , valueOutOfScope
......
...@@ -34,9 +34,14 @@ module.exports = { ...@@ -34,9 +34,14 @@ module.exports = {
} }
}, },
parse: function(string) { parse: function(string) {
var self = this var self = this,
object = { }
if (('string' !== typeof string) || (0 === string.length)) {
return object;
}
const rx = /\"((?:\\\"|[^"])*)\"\s*\=\>\s*((?:true|false|NULL|\d+|\d+\.\d+|\"((?:\\\"|[^"])*)\"))/g const rx = /\"((?:\\\"|[^"])*)\"\s*\=\>\s*((?:true|false|NULL|\d+|\d+\.\d+|\"((?:\\\"|[^"])*)\"))/g
var object = { }
string.replace(rx, function(match, key, value, innerValue) { string.replace(rx, function(match, key, value, innerValue) {
switch(value) { switch(value) {
......
...@@ -137,7 +137,7 @@ module.exports = (function() { ...@@ -137,7 +137,7 @@ module.exports = (function() {
for (var key in rows[0]) { for (var key in rows[0]) {
if (rows[0].hasOwnProperty(key)) { if (rows[0].hasOwnProperty(key)) {
var record = rows[0][key] var record = rows[0][key]
if (!!this.callee.Model && !!this.callee.Model.rawAttributes && !!this.callee.Model.rawAttributes[key] && !!this.callee.Model.rawAttributes[key].type && !!this.callee.Model.rawAttributes[key].type.type && this.callee.Model.rawAttributes[key].type.type === DataTypes.HSTORE.type) { if (!!this.callee.Model && !!this.callee.Model.rawAttributes && !!this.callee.Model.rawAttributes[key] && !!this.callee.Model.rawAttributes[key].type && this.callee.Model.rawAttributes[key].type.toString() === DataTypes.HSTORE.toString()) {
record = hstore.parse(record) record = hstore.parse(record)
} }
this.callee.dataValues[key] = record this.callee.dataValues[key] = record
...@@ -152,7 +152,7 @@ module.exports = (function() { ...@@ -152,7 +152,7 @@ module.exports = (function() {
for (var key in rows[0]) { for (var key in rows[0]) {
if (rows[0].hasOwnProperty(key)) { if (rows[0].hasOwnProperty(key)) {
var record = rows[0][key] var record = rows[0][key]
if (!!this.callee.Model && !!this.callee.Model.rawAttributes && !!this.callee.Model.rawAttributes[key] && !!this.callee.Model.rawAttributes[key].type && !!this.callee.Model.rawAttributes[key].type.type && this.callee.Model.rawAttributes[key].type.type === DataTypes.HSTORE.type) { if (!!this.callee.Model && !!this.callee.Model.rawAttributes && !!this.callee.Model.rawAttributes[key] && !!this.callee.Model.rawAttributes[key].type && this.callee.Model.rawAttributes[key].type.toString() === DataTypes.HSTORE.toString()) {
record = hstore.parse(record) record = hstore.parse(record)
} }
this.callee.dataValues[key] = record this.callee.dataValues[key] = record
......
...@@ -14,6 +14,7 @@ if (dialect.match(/^postgres/)) { ...@@ -14,6 +14,7 @@ if (dialect.match(/^postgres/)) {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
username: DataTypes.STRING, username: DataTypes.STRING,
email: {type: DataTypes.ARRAY(DataTypes.TEXT)}, email: {type: DataTypes.ARRAY(DataTypes.TEXT)},
settings: DataTypes.HSTORE,
document: {type: DataTypes.HSTORE, defaultValue: '"default"=>"value"'} document: {type: DataTypes.HSTORE, defaultValue: '"default"=>"value"'}
}) })
this.User.sync({ force: true }).success(function() { this.User.sync({ force: true }).success(function() {
...@@ -48,13 +49,28 @@ if (dialect.match(/^postgres/)) { ...@@ -48,13 +49,28 @@ if (dialect.match(/^postgres/)) {
}) })
}) })
it('describeTable should tell me that a column is hstore and not USER-DEFINED', function(done) { describe('hstore', function() {
it('should tell me that a column is hstore and not USER-DEFINED', function(done) {
this.sequelize.queryInterface.describeTable('Users').success(function(table) { this.sequelize.queryInterface.describeTable('Users').success(function(table) {
expect(table.settings.type).to.equal('HSTORE')
expect(table.document.type).to.equal('HSTORE') expect(table.document.type).to.equal('HSTORE')
done() done()
}) })
}) })
it('should stringify hstore with insert', function(done) {
this.User.create({
username: 'bob',
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)
done()
})
})
})
describe('enums', function() { describe('enums', function() {
it('should be able to ignore enum types that already exist', function(done) { it('should be able to ignore enum types that already exist', function(done) {
var User = this.sequelize.define('UserEnums', { var User = this.sequelize.define('UserEnums', {
...@@ -242,21 +258,19 @@ if (dialect.match(/^postgres/)) { ...@@ -242,21 +258,19 @@ if (dialect.match(/^postgres/)) {
var self = this var self = this
this.User this.User
.create({ username: 'user', email: ['foo@bar.com'], document: { created: { test: '"value"' }}}) .create({ username: 'user', email: ['foo@bar.com'], settings: { created: { test: '"value"' }}})
.success(function(newUser) { .success(function(newUser) {
expect(newUser.document).to.deep.equal({ created: { test: '"value"' }}) // Check to see if the default value for an hstore field works
expect(newUser.document).to.deep.equal({default: 'value'})
expect(newUser.settings).to.deep.equal({ created: { test: '"value"' }})
// Check to see if updating an hstore field works // Check to see if updating an hstore field works
newUser.updateAttributes({document: {should: 'update', to: 'this', first: 'place'}}).success(function(oldUser){ newUser.updateAttributes({settings: {should: 'update', to: 'this', first: 'place'}}).success(function(oldUser){
// Postgres always returns keys in alphabetical order (ascending) // Postgres always returns keys in alphabetical order (ascending)
expect(oldUser.document).to.deep.equal({first: 'place', should: 'update', to: 'this'}) expect(oldUser.settings).to.deep.equal({first: 'place', should: 'update', to: 'this'})
// Check to see if the default value for an hstore field works
self.User.create({ username: 'user2', email: ['bar@baz.com']}).success(function(defaultUser){
expect(defaultUser.document).to.deep.equal({default: 'value'})
done() done()
}) })
}) })
})
.error(console.log) .error(console.log)
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!