truncate-table-query.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { buildInvalidOptionReceivedError } from '@sequelize/core/_non-semver-use-at-your-own-risk_/utils/check.js';
  2. import { createSequelizeInstance, expectPerDialect, sequelize } from '../../support';
  3. const dialect = sequelize.dialect;
  4. describe('QueryGenerator#truncateTableQuery', () => {
  5. const queryGenerator = sequelize.queryGenerator;
  6. it('produces a TRUNCATE TABLE query for a table', () => {
  7. expectPerDialect(() => queryGenerator.truncateTableQuery('myTable'), {
  8. mssql: 'TRUNCATE TABLE [myTable]',
  9. sqlite3: ['DELETE FROM `myTable`'],
  10. 'db2 ibmi': 'TRUNCATE TABLE "myTable" IMMEDIATE',
  11. 'mariadb mysql': 'TRUNCATE `myTable`',
  12. 'postgres snowflake': 'TRUNCATE "myTable"',
  13. });
  14. });
  15. it('produces a TRUNCATE TABLE query with CASCADE for a table', () => {
  16. expectPerDialect(() => queryGenerator.truncateTableQuery('myTable', { cascade: true }), {
  17. default: buildInvalidOptionReceivedError('truncateTableQuery', dialect.name, ['cascade']),
  18. postgres: `TRUNCATE "myTable" CASCADE`,
  19. });
  20. });
  21. it('produces a TRUNCATE TABLE query with RESTART IDENTITY for a table', () => {
  22. expectPerDialect(
  23. () => queryGenerator.truncateTableQuery('myTable', { restartIdentity: true }),
  24. {
  25. default: buildInvalidOptionReceivedError('truncateTableQuery', dialect.name, [
  26. 'restartIdentity',
  27. ]),
  28. sqlite3: [
  29. 'DELETE FROM `myTable`',
  30. "DELETE FROM `sqlite_sequence` WHERE `name` = 'myTable'",
  31. ],
  32. postgres: `TRUNCATE "myTable" RESTART IDENTITY`,
  33. },
  34. );
  35. });
  36. it('produces a TRUNCATE TABLE query with CASCADE and RESTART IDENTITY query for a table', () => {
  37. expectPerDialect(
  38. () => queryGenerator.truncateTableQuery('myTable', { cascade: true, restartIdentity: true }),
  39. {
  40. default: buildInvalidOptionReceivedError('truncateTableQuery', dialect.name, [
  41. 'cascade',
  42. 'restartIdentity',
  43. ]),
  44. sqlite3: buildInvalidOptionReceivedError('truncateTableQuery', dialect.name, ['cascade']),
  45. postgres: `TRUNCATE "myTable" RESTART IDENTITY CASCADE`,
  46. },
  47. );
  48. });
  49. it('produces a TRUNCATE TABLE query for a model', () => {
  50. const MyModel = sequelize.define('MyModel', {});
  51. expectPerDialect(() => queryGenerator.truncateTableQuery(MyModel), {
  52. mssql: 'TRUNCATE TABLE [MyModels]',
  53. sqlite3: ['DELETE FROM `MyModels`'],
  54. 'db2 ibmi': 'TRUNCATE TABLE "MyModels" IMMEDIATE',
  55. 'mariadb mysql': 'TRUNCATE `MyModels`',
  56. 'postgres snowflake': 'TRUNCATE "MyModels"',
  57. });
  58. });
  59. it('produces a TRUNCATE TABLE query for a model definition', () => {
  60. const MyModel = sequelize.define('MyModel', {});
  61. const myDefinition = MyModel.modelDefinition;
  62. expectPerDialect(() => queryGenerator.truncateTableQuery(myDefinition), {
  63. mssql: 'TRUNCATE TABLE [MyModels]',
  64. sqlite3: ['DELETE FROM `MyModels`'],
  65. 'db2 ibmi': 'TRUNCATE TABLE "MyModels" IMMEDIATE',
  66. 'mariadb mysql': 'TRUNCATE `MyModels`',
  67. 'postgres snowflake': 'TRUNCATE "MyModels"',
  68. });
  69. });
  70. it('produces a TRUNCATE TABLE query from a table and schema', () => {
  71. expectPerDialect(
  72. () => queryGenerator.truncateTableQuery({ tableName: 'myTable', schema: 'mySchema' }),
  73. {
  74. mssql: 'TRUNCATE TABLE [mySchema].[myTable]',
  75. sqlite3: ['DELETE FROM `mySchema.myTable`'],
  76. 'db2 ibmi': 'TRUNCATE TABLE "mySchema"."myTable" IMMEDIATE',
  77. 'mariadb mysql': 'TRUNCATE `mySchema`.`myTable`',
  78. 'postgres snowflake': 'TRUNCATE "mySchema"."myTable"',
  79. },
  80. );
  81. });
  82. it('produces a TRUNCATE TABLE query from a table and default schema', () => {
  83. expectPerDialect(
  84. () =>
  85. queryGenerator.truncateTableQuery({
  86. tableName: 'myTable',
  87. schema: dialect.getDefaultSchema(),
  88. }),
  89. {
  90. mssql: 'TRUNCATE TABLE [myTable]',
  91. sqlite3: ['DELETE FROM `myTable`'],
  92. 'db2 ibmi': 'TRUNCATE TABLE "myTable" IMMEDIATE',
  93. 'mariadb mysql': 'TRUNCATE `myTable`',
  94. 'postgres snowflake': 'TRUNCATE "myTable"',
  95. },
  96. );
  97. });
  98. it('produces a TRUNCATE TABLE query from a table and globally set schema', () => {
  99. const sequelizeSchema = createSequelizeInstance({ schema: 'mySchema' });
  100. const queryGeneratorSchema = sequelizeSchema.queryGenerator;
  101. expectPerDialect(() => queryGeneratorSchema.truncateTableQuery('myTable'), {
  102. mssql: 'TRUNCATE TABLE [mySchema].[myTable]',
  103. sqlite3: ['DELETE FROM `mySchema.myTable`'],
  104. 'db2 ibmi': 'TRUNCATE TABLE "mySchema"."myTable" IMMEDIATE',
  105. 'mariadb mysql': 'TRUNCATE `mySchema`.`myTable`',
  106. 'postgres snowflake': 'TRUNCATE "mySchema"."myTable"',
  107. });
  108. });
  109. it('produces a TRUNCATE TABLE query for a table with schema and custom delimiter argument', () => {
  110. // This test is only relevant for dialects that do not support schemas
  111. if (dialect.supports.schemas) {
  112. return;
  113. }
  114. expectPerDialect(
  115. () =>
  116. queryGenerator.truncateTableQuery({
  117. tableName: 'myTable',
  118. schema: 'mySchema',
  119. delimiter: 'custom',
  120. }),
  121. {
  122. sqlite3: ['DELETE FROM `mySchemacustommyTable`'],
  123. },
  124. );
  125. });
  126. it('produces a TRUNCATE TABLE query with RESTART IDENTITY for a table with schema and custom delimiter argument', () => {
  127. // This test is only relevant for dialects that do not support schemas
  128. if (dialect.supports.schemas) {
  129. return;
  130. }
  131. expectPerDialect(
  132. () =>
  133. queryGenerator.truncateTableQuery(
  134. { tableName: 'myTable', schema: 'mySchema', delimiter: 'custom' },
  135. { restartIdentity: true },
  136. ),
  137. {
  138. sqlite3: [
  139. 'DELETE FROM `mySchemacustommyTable`',
  140. "DELETE FROM `sqlite_sequence` WHERE `name` = 'mySchemacustommyTable'",
  141. ],
  142. },
  143. );
  144. });
  145. });