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

Commit f7fe31fa by Felix Becker Committed by Sushant

Backport: Add option to restart identity when truncate (#6417)

Issue #5356

  - update query interface with option restartIdentity
  - add test for option restartIdentity
  - update documentation for option restartIdentity with truncate
  - add restartIdentity entry in changelog
1 parent ffc99050
# Future
- [ADDED] `restartIdentity` option for truncate in postgres [#5356](https://github.com/sequelize/sequelize/issues/5356)
# 3.23.5
- [FIXED] Fixed an issue where custom-named model fields break when offsetting, ordering, and including hasMany simultaneously. [#5985](https://github.com/sequelize/sequelize/issues/5985)
- [FIXED] Don't remove includes from count queries and unify findAndCount and count queries. [#6123](https://github.com/sequelize/sequelize/issues/6123)
......
......@@ -725,6 +725,7 @@ Delete multiple instances, or set their deletedAt timestamp to the current time
| [options.force=false] | Boolean | Delete instead of setting deletedAt to current timestamp (only applicable if `paranoid` is enabled) |
| [options.truncate=false] | Boolean | If set to true, dialects that support it will use TRUNCATE instead of DELETE FROM. If a table is truncated the where and limit options are ignored |
| [options.cascade=false] | Boolean | Only used in conjunction 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. |
| [options.restartIdentity=false] | Boolean | Only used in conjunction with TRUNCATE. Automatically restart sequences owned by columns of the truncated table. Postgres only. |
| [options.transaction] | Transaction | Transaction to run query under |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.benchmark=false] | Boolean | Print query execution time in milliseconds when logging SQL. |
......
......@@ -350,6 +350,10 @@ var QueryGenerator = {
query += ' CASCADE';
}
if (options.restartIdentity) {
query += ' RESTART IDENTITY';
}
return query;
}
......
......@@ -2240,6 +2240,7 @@ Model.prototype.truncate = function(options) {
* @param {Boolean} [options.force=false] Delete instead of setting deletedAt to current timestamp (only applicable if `paranoid` is enabled)
* @param {Boolean} [options.truncate=false] If set to true, dialects that support it will use TRUNCATE instead of DELETE FROM. If a table is truncated the where and limit options are ignored
* @param {Boolean} [options.cascade=false] Only used in conjunction 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} [options.restartIdentity=false] Only used in conjunction with TRUNCATE. Automatically restart sequences owned by columns of the truncated table.
* @param {Transaction} [options.transaction] Transaction to run query under
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging).
......@@ -2262,7 +2263,8 @@ Model.prototype.destroy = function(options) {
hooks: true,
individualHooks: false,
force: false,
cascade: false
cascade: false,
restartIdentity: false
});
options.type = QueryTypes.BULKDELETE;
......
......@@ -43,6 +43,33 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
});
});
suite('truncate with cascade and restartIdentity', function () {
var options = {
table: User.getTableName(),
where: {},
truncate: true,
cascade: true,
restartIdentity: true,
limit: 10
};
test(util.inspect(options, {depth: 2}), function () {
return expectsql(
sql.deleteQuery(
options.table,
options.where,
options,
User
), {
postgres: 'TRUNCATE "public"."test_users" CASCADE RESTART IDENTITY',
mssql: 'TRUNCATE TABLE [public].[test_users]',
mysql: 'TRUNCATE `public.test_users`',
sqlite: 'DELETE FROM `public.test_users`'
}
);
});
});
suite('delete without limit', function () {
var options = {
table: User.getTableName(),
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!