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

Commit e6d6f393 by Jonathan Committed by Jan Aagaard Meier

Close #5356 : Add option to restart identity when truncate (#6415)

* 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 9f2d2226
# Future # Future
- [ADDED] `restartIdentity` option for truncate in postgres [#5356](https://github.com/sequelize/sequelize/issues/5356)
- [INTERNAL] Migrated to `node-mysql2` for prepared statements [#6354](https://github.com/sequelize/sequelize/issues/6354) - [INTERNAL] Migrated to `node-mysql2` for prepared statements [#6354](https://github.com/sequelize/sequelize/issues/6354)
- [ADDED] SQLCipher support via the SQLite connection manager - [ADDED] SQLCipher support via the SQLite connection manager
- [CHANGED] Range type bounds now default to [postgres default](https://www.postgresql.org/docs/9.5/static/rangetypes.html#RANGETYPES-CONSTRUCT) `[)` (inclusive, exclusive) [#5990](https://github.com/sequelize/sequelize/issues/5990) - [CHANGED] Range type bounds now default to [postgres default](https://www.postgresql.org/docs/9.5/static/rangetypes.html#RANGETYPES-CONSTRUCT) `[)` (inclusive, exclusive) [#5990](https://github.com/sequelize/sequelize/issues/5990)
......
...@@ -726,6 +726,7 @@ Delete multiple instances, or set their deletedAt timestamp to the current time ...@@ -726,6 +726,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.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.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.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.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.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. | | [options.benchmark=false] | Boolean | Print query execution time in milliseconds when logging SQL. |
......
...@@ -308,6 +308,10 @@ const QueryGenerator = { ...@@ -308,6 +308,10 @@ const QueryGenerator = {
query += ' CASCADE'; query += ' CASCADE';
} }
if (options.restartIdentity) {
query += ' RESTART IDENTITY';
}
return query; return query;
} }
......
...@@ -2181,6 +2181,7 @@ class Model { ...@@ -2181,6 +2181,7 @@ class Model {
* @param {Boolean} [options.force=false] Delete instead of setting deletedAt to current timestamp (only applicable if `paranoid` is enabled) * @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.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.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 {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 {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). * @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging).
...@@ -2202,7 +2203,8 @@ class Model { ...@@ -2202,7 +2203,8 @@ class Model {
hooks: true, hooks: true,
individualHooks: false, individualHooks: false,
force: false, force: false,
cascade: false cascade: false,
restartIdentity: false
}); });
options.type = QueryTypes.BULKDELETE; options.type = QueryTypes.BULKDELETE;
......
...@@ -43,6 +43,33 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -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 () { suite('delete without limit', function () {
var options = { var options = {
table: User.getTableName(), table: User.getTableName(),
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!