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

Commit a3fa52cd by Jan Aagaard Meier

Merge pull request #3816 from BridgeAR/truncate

Add sequelize.truncate function
2 parents 1f17bece ae156e99
# Next # Next
- [FEATURE] Add support for keyword `after` in options of a field (useful for migrations), only for MySQL. [#3166](https://github.com/sequelize/sequelize/pull/3166) - [FEATURE] Add support for keyword `after` in options of a field (useful for migrations), only for MySQL. [#3166](https://github.com/sequelize/sequelize/pull/3166)
- [FEATURE] There's a new sequelize.truncate function to truncate all tables defined through the sequelize models [#2671](https://github.com/sequelize/sequelize/pull/2671)
- [FIXED] Fix a case where `type` in `sequelize.query` was not being set to raw. [#3800](https://github.com/sequelize/sequelize/pull/3800) - [FIXED] Fix a case where `type` in `sequelize.query` was not being set to raw. [#3800](https://github.com/sequelize/sequelize/pull/3800)
- [FIXED] Fix an issue where include all was not being properly expanded for self-references [#3804](https://github.com/sequelize/sequelize/issues/3804) - [FIXED] Fix an issue where include all was not being properly expanded for self-references [#3804](https://github.com/sequelize/sequelize/issues/3804)
- [FIXED] Fix instance.changed regression to not return false negatives for not changed null values [#3812](https://github.com/sequelize/sequelize/issues/3812) - [FIXED] Fix instance.changed regression to not return false negatives for not changed null values [#3812](https://github.com/sequelize/sequelize/issues/3812)
......
...@@ -1441,6 +1441,23 @@ module.exports = (function() { ...@@ -1441,6 +1441,23 @@ module.exports = (function() {
}; };
/** /**
* Truncate all instances of the model. This is a convenient method for Model.destroy({ truncate: true }).
*
* @param {object} [options] The options passed to Model.destroy in addition to truncate
* @param {Boolean|function} [options.transaction]
* @param {Boolean|function} [options.cascade = false] Only used in conjuction with TRUNCATE. Truncates all tables that have foreign-key references to the named table, or to any tables added to the group due to CASCADE.
* @param {Boolean|function} [options.logging] A function that logs sql queries, or false for no logging
* @return {Promise}
*
* @see {Model#destroy} for more information
*/
Model.prototype.truncate = function(options) {
options = options || {};
options.truncate = true;
return this.destroy(options);
};
/**
* Delete multiple instances, or set their deletedAt timestamp to the current time if `paranoid` is enabled. * Delete multiple instances, or set their deletedAt timestamp to the current time if `paranoid` is enabled.
* *
* @param {Object} options * @param {Object} options
...@@ -1460,7 +1477,7 @@ module.exports = (function() { ...@@ -1460,7 +1477,7 @@ module.exports = (function() {
, instances; , instances;
if (!options || !(options.where || options.truncate)) { if (!options || !(options.where || options.truncate)) {
throw new Error('Missing where or truncate attribute in the options parameter passed to destroy.'); throw new Error('Missing where or truncate attribute in the options parameter of model.destroy.');
} }
options = Utils._.extend({ options = Utils._.extend({
......
...@@ -902,6 +902,31 @@ module.exports = (function() { ...@@ -902,6 +902,31 @@ module.exports = (function() {
}; };
/** /**
* Truncate all tables defined through the sequelize models. This is done
* by calling Model.truncate() on each model.
*
* @param {object} [options] The options passed to Model.destroy in addition to truncate
* @param {Boolean|function} [options.transaction]
* @param {Boolean|function} [options.logging] A function that logs sql queries, or false for no logging
* @return {Promise}
*
* @see {Model#truncate} for more information
*/
Sequelize.prototype.truncate = function(options) {
var models = [];
this.modelManager.forEachModel(function(model) {
if (model) {
models.push(model);
}
}, { reverse: false });
return Promise.map(models, function(model) {
return model.truncate(options);
});
};
/**
* Test the connection by trying to authenticate * Test the connection by trying to authenticate
* *
* @fires success If authentication was successfull * @fires success If authentication was successfull
......
...@@ -896,7 +896,7 @@ describe(Support.getTestDialectTeaser('HasMany'), function() { ...@@ -896,7 +896,7 @@ describe(Support.getTestDialectTeaser('HasMany'), function() {
describe('allows the user to provide an attribute definition object as foreignKey', function() { describe('allows the user to provide an attribute definition object as foreignKey', function() {
it('works with a column that hasnt been defined before', function() { it('works with a column that hasnt been defined before', function() {
var Task = this.sequelize.define('task', {}) var Task = this.sequelize.define('task', {})
, User = this.sequelize.define('user', {}); , User = this.sequelize.define('user', {});
User.hasMany(Task, { User.hasMany(Task, {
foreignKey: { foreignKey: {
......
...@@ -1130,6 +1130,22 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1130,6 +1130,22 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
describe('destroy', function() { describe('destroy', function() {
it('convenient method `truncate` should clear the table', function() {
var User = this.sequelize.define('User', { username: DataTypes.STRING }),
data = [
{ username: 'user1' },
{ username: 'user2' }
];
return this.sequelize.sync({ force: true }).then(function() {
return User.bulkCreate(data);
}).then(function() {
return User.truncate();
}).then(function() {
return expect(User.findAll()).to.eventually.have.length(0);
});
});
it('truncate should clear the table', function() { it('truncate should clear the table', function() {
var User = this.sequelize.define('User', { username: DataTypes.STRING }), var User = this.sequelize.define('User', { username: DataTypes.STRING }),
data = [ data = [
...@@ -1155,7 +1171,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1155,7 +1171,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
throw new Error('Destroy should throw an error if no where clause is given.'); throw new Error('Destroy should throw an error if no where clause is given.');
}, function(err) { }, function(err) {
expect(err).to.be.an.instanceof(Error); expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal('Missing where or truncate attribute in the options parameter passed to destroy.'); expect(err.message).to.equal('Missing where or truncate attribute in the options parameter of model.destroy.');
}); });
}); });
......
...@@ -647,8 +647,35 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() { ...@@ -647,8 +647,35 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
}); });
}); });
describe('truncate', function() {
it("truncates all models", function() {
var Project = this.sequelize.define('project' + config.rand(), {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
title: DataTypes.STRING
});
return this.sequelize.sync({ force: true }).then(function() {
return Project.create({ title: 'bla' });
}).bind(this).then(function(project) {
expect(project).to.exist;
expect(project.title).to.equal('bla');
expect(project.id).to.equal(1);
return this.sequelize.truncate().then(function() {
return Project.findAll({});
});
}).then(function(projects) {
expect(projects).to.exist;
expect(projects).to.have.length(0);
});
});
});
describe('sync', function() { describe('sync', function() {
it('synchronizes all daos', function() { it('synchronizes all models', function() {
var Project = this.sequelize.define('project' + config.rand(), { title: DataTypes.STRING }); var Project = this.sequelize.define('project' + config.rand(), { title: DataTypes.STRING });
var Task = this.sequelize.define('task' + config.rand(), { title: DataTypes.STRING }); var Task = this.sequelize.define('task' + config.rand(), { title: DataTypes.STRING });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!