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

Commit 802d4c77 by Daniel Durante

This commit fixes paranoid: true option, .find, .all, and .count will now abide …

…by paranoid's rules. This does not test for associated models with paranoid true but it's the first step.
1 parent abd0f717
Showing with 83 additions and 10 deletions
...@@ -339,6 +339,16 @@ module.exports = (function() { ...@@ -339,6 +339,16 @@ module.exports = (function() {
this.options.whereCollection = options.where || null this.options.whereCollection = options.where || null
} }
if (this.options.paranoid === true) {
options = options || {}
options.where = options.where || {}
if (typeof options.where === "string") {
options.where += ' AND ' + this.QueryInterface.quoteIdentifier(this.options.deletedAt) + ' IS NULL '
} else {
options.where[this.options.deletedAt] = null
}
}
return this.QueryInterface.select(this, this.tableName, options, Utils._.defaults({ return this.QueryInterface.select(this, this.tableName, options, Utils._.defaults({
type: 'SELECT', type: 'SELECT',
hasJoin: hasJoin hasJoin: hasJoin
...@@ -430,6 +440,16 @@ module.exports = (function() { ...@@ -430,6 +440,16 @@ module.exports = (function() {
} }
} }
if (this.options.paranoid === true) {
options = options || {}
options.where = options.where || {}
if (typeof options.where === "string") {
options.where += ' AND ' + this.QueryInterface.quoteIdentifier(this.options.deletedAt) + ' IS NULL '
} else {
options.where[this.options.deletedAt] = null
}
}
options.limit = 1 options.limit = 1
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
...@@ -444,6 +464,16 @@ module.exports = (function() { ...@@ -444,6 +464,16 @@ module.exports = (function() {
options.attributes.push(['count(*)', 'count']) options.attributes.push(['count(*)', 'count'])
options.parseInt = true options.parseInt = true
if (this.options.paranoid === true) {
options = options || {}
options.where = options.where || {}
if (typeof options.where === "string") {
options.where += ' AND ' + this.QueryInterface.quoteIdentifier(this.options.deletedAt) + ' IS NULL '
} else {
options.where[this.options.deletedAt] = null
}
}
return this.QueryInterface.rawSelect(this.getTableName(), options, 'count') return this.QueryInterface.rawSelect(this.getTableName(), options, 'count')
} }
......
...@@ -382,10 +382,11 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -382,10 +382,11 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
, pad = function (number) { , pad = function (number) {
if (number > 9) { if (number > 9) {
return number return number
} }
return '0' + number return '0' + number
} }
expect(user.year).to.equal(now.getFullYear() + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate()))
expect(user.year).to.equal(now.getUTCFullYear() + '-' + pad(now.getUTCMonth() + 1) + '-' + pad(now.getUTCDate()))
done() done()
}) })
}) })
...@@ -1119,6 +1120,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1119,6 +1120,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it('sets deletedAt to the current timestamp if paranoid is true', function(done) { it('sets deletedAt to the current timestamp if paranoid is true', function(done) {
var self = this var self = this
, ident = self.sequelize.queryInterface.QueryGenerator.quoteIdentifier
, escape = self.sequelize.queryInterface.QueryGenerator.quote
, ParanoidUser = self.sequelize.define('ParanoidUser', { , ParanoidUser = self.sequelize.define('ParanoidUser', {
username: Sequelize.STRING, username: Sequelize.STRING,
secretValue: Sequelize.STRING, secretValue: Sequelize.STRING,
...@@ -1133,19 +1136,57 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1133,19 +1136,57 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
ParanoidUser.sync({ force: true }).success(function() { ParanoidUser.sync({ force: true }).success(function() {
ParanoidUser.bulkCreate(data).success(function() { ParanoidUser.bulkCreate(data).success(function() {
var date = parseInt(+new Date()/5000, 10) // since we save in UTC, let's format to UTC time
var date = moment().utc().format('YYYY-MM-DD h:mm')
ParanoidUser.destroy({secretValue: '42'}).success(function() { ParanoidUser.destroy({secretValue: '42'}).success(function() {
ParanoidUser.findAll({order: 'id'}).success(function(users) { ParanoidUser.findAll({order: 'id'}).success(function(users) {
expect(users.length).to.equal(3) expect(users.length).to.equal(1)
expect(users[0].username).to.equal("Bob")
expect(users[0].username).to.equal("Peter") self.sequelize.query('SELECT * FROM ' + ident('ParanoidUsers') + ' WHERE ' + ident('deletedAt') + ' IS NOT NULL ORDER BY ' + ident('id'), null, {raw: true}).success(function(users) {
expect(users[1].username).to.equal("Paul") expect(users[0].username).to.equal("Peter")
expect(users[2].username).to.equal("Bob") expect(users[1].username).to.equal("Paul")
expect(parseInt(+users[0].deletedAt/5000, 10)).to.equal(date) if (dialect === "sqlite") {
expect(parseInt(+users[1].deletedAt/5000, 10)).to.equal(date) expect(moment(users[0].deletedAt).format('YYYY-MM-DD h:mm')).to.equal(date)
expect(moment(users[1].deletedAt).format('YYYY-MM-DD h:mm')).to.equal(date)
} else {
expect(moment(users[0].deletedAt).utc().format('YYYY-MM-DD h:mm')).to.equal(date)
expect(moment(users[1].deletedAt).utc().format('YYYY-MM-DD h:mm')).to.equal(date)
}
done()
})
})
})
})
})
})
done() describe("can't find records marked as deleted with paranoid being true", function() {
it('with the DAOFactory', function(done) {
var User = this.sequelize.define('UserCol', {
username: Sequelize.STRING
}, { paranoid: true })
User.sync({ force: true }).success(function() {
User.bulkCreate([
{username: 'Toni'},
{username: 'Tobi'},
{username: 'Max'}
]).success(function() {
User.find(1).success(function(user) {
user.destroy().success(function() {
User.find(1).success(function(user) {
expect(user).to.be.null
User.count().success(function(cnt) {
expect(cnt).to.equal(2)
User.all().success(function(users) {
expect(users).to.have.length(2)
done()
})
})
})
})
}) })
}) })
}) })
...@@ -2693,6 +2734,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2693,6 +2734,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it("handles offset and limit", function(done) { it("handles offset and limit", function(done) {
var self = this var self = this
this.User.bulkCreate([{username: 'bobby'}, {username: 'tables'}]).success(function() { this.User.bulkCreate([{username: 'bobby'}, {username: 'tables'}]).success(function() {
self.User.findAll({ limit: 2, offset: 2 }).success(function(users) { self.User.findAll({ limit: 2, offset: 2 }).success(function(users) {
expect(users.length).to.equal(2) expect(users.length).to.equal(2)
...@@ -2780,6 +2822,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2780,6 +2822,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
done() done()
}) })
}) })
it("handles attributes", function(done) { it("handles attributes", function(done) {
this.User.findAndCountAll({where: "id != " + this.users[0].id, attributes: ['data']}).success(function(info) { this.User.findAndCountAll({where: "id != " + this.users[0].id, attributes: ['data']}).success(function(info) {
expect(info.count).to.equal(2) expect(info.count).to.equal(2)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!