remove-index-query.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { buildInvalidOptionReceivedError } from '@sequelize/core/_non-semver-use-at-your-own-risk_/utils/check.js';
  2. import { createSequelizeInstance, expectsql, sequelize } from '../../support';
  3. const dialect = sequelize.dialect;
  4. const notImplementedError = new Error(
  5. `removeIndexQuery has not been implemented in ${dialect.name}.`,
  6. );
  7. describe('QueryGenerator#removeIndexQuery', () => {
  8. const queryGenerator = sequelize.queryGenerator;
  9. it('produces a DROP INDEX query from a table', () => {
  10. expectsql(() => queryGenerator.removeIndexQuery('myTable', 'user_foo_bar'), {
  11. default: `DROP INDEX [user_foo_bar] ON [myTable]`,
  12. sqlite3: 'DROP INDEX `user_foo_bar`',
  13. ibmi: `BEGIN DROP INDEX "user_foo_bar"; COMMIT; END`,
  14. db2: `DROP INDEX "user_foo_bar"`,
  15. postgres: `DROP INDEX "public"."user_foo_bar"`,
  16. snowflake: notImplementedError,
  17. });
  18. });
  19. it('produces a DROP INDEX query from a table with attributes', () => {
  20. expectsql(() => queryGenerator.removeIndexQuery('myTable', ['foo', 'bar']), {
  21. default: `DROP INDEX [my_table_foo_bar] ON [myTable]`,
  22. sqlite3: 'DROP INDEX `my_table_foo_bar`',
  23. ibmi: `BEGIN DROP INDEX "my_table_foo_bar"; COMMIT; END`,
  24. db2: `DROP INDEX "my_table_foo_bar"`,
  25. postgres: `DROP INDEX "public"."my_table_foo_bar"`,
  26. snowflake: notImplementedError,
  27. });
  28. });
  29. it('produces a DROP INDEX with CONCURRENTLY query from a table', () => {
  30. expectsql(
  31. () => queryGenerator.removeIndexQuery('myTable', 'user_foo_bar', { concurrently: true }),
  32. {
  33. default: buildInvalidOptionReceivedError('removeIndexQuery', dialect.name, [
  34. 'concurrently',
  35. ]),
  36. postgres: `DROP INDEX CONCURRENTLY "public"."user_foo_bar"`,
  37. snowflake: notImplementedError,
  38. },
  39. );
  40. });
  41. it('produces a DROP INDEX with IF EXISTS query from a table', () => {
  42. expectsql(
  43. () => queryGenerator.removeIndexQuery('myTable', 'user_foo_bar', { ifExists: true }),
  44. {
  45. default: `DROP INDEX IF EXISTS [user_foo_bar] ON [myTable]`,
  46. sqlite3: 'DROP INDEX IF EXISTS `user_foo_bar`',
  47. postgres: `DROP INDEX IF EXISTS "public"."user_foo_bar"`,
  48. ibmi: `BEGIN IF EXISTS (SELECT * FROM QSYS2.SYSINDEXES WHERE INDEX_NAME = "user_foo_bar") THEN DROP INDEX "user_foo_bar"; COMMIT; END IF; END`,
  49. snowflake: notImplementedError,
  50. 'db2 mysql': buildInvalidOptionReceivedError('removeIndexQuery', dialect.name, [
  51. 'ifExists',
  52. ]),
  53. },
  54. );
  55. });
  56. it('produces a DROP INDEX with CASCADE query from a table', () => {
  57. expectsql(() => queryGenerator.removeIndexQuery('myTable', 'user_foo_bar', { cascade: true }), {
  58. default: buildInvalidOptionReceivedError('removeIndexQuery', dialect.name, ['cascade']),
  59. postgres: `DROP INDEX "public"."user_foo_bar" CASCADE`,
  60. snowflake: notImplementedError,
  61. });
  62. });
  63. it('produces a DROP INDEX with CASCADE and IF EXISTS query from a table', () => {
  64. expectsql(
  65. () =>
  66. queryGenerator.removeIndexQuery('myTable', 'user_foo_bar', {
  67. cascade: true,
  68. ifExists: true,
  69. }),
  70. {
  71. default: `DROP INDEX IF EXISTS [user_foo_bar] ON [myTable] CASCADE`,
  72. postgres: `DROP INDEX IF EXISTS "public"."user_foo_bar" CASCADE`,
  73. snowflake: notImplementedError,
  74. 'db2 mysql': buildInvalidOptionReceivedError('removeIndexQuery', dialect.name, [
  75. 'cascade',
  76. 'ifExists',
  77. ]),
  78. 'ibmi mariadb mssql sqlite3': buildInvalidOptionReceivedError(
  79. 'removeIndexQuery',
  80. dialect.name,
  81. ['cascade'],
  82. ),
  83. },
  84. );
  85. });
  86. it('produces a DROP INDEX with CONCURRENTLY and IF EXISTS query from a table', () => {
  87. expectsql(
  88. () =>
  89. queryGenerator.removeIndexQuery('myTable', 'user_foo_bar', {
  90. concurrently: true,
  91. ifExists: true,
  92. }),
  93. {
  94. default: `DROP INDEX CONCURRENTLY IF EXISTS [user_foo_bar] ON [myTable]`,
  95. postgres: `DROP INDEX CONCURRENTLY IF EXISTS "public"."user_foo_bar"`,
  96. snowflake: notImplementedError,
  97. 'db2 mysql': buildInvalidOptionReceivedError('removeIndexQuery', dialect.name, [
  98. 'concurrently',
  99. 'ifExists',
  100. ]),
  101. 'ibmi mariadb mssql sqlite3': buildInvalidOptionReceivedError(
  102. 'removeIndexQuery',
  103. dialect.name,
  104. ['concurrently'],
  105. ),
  106. },
  107. );
  108. });
  109. it('throws an error for DROP INDEX with CASCADE and CONCURRENTLY query from a table', () => {
  110. expectsql(
  111. () =>
  112. queryGenerator.removeIndexQuery('myTable', 'user_foo_bar', {
  113. cascade: true,
  114. concurrently: true,
  115. }),
  116. {
  117. default: buildInvalidOptionReceivedError('removeIndexQuery', dialect.name, [
  118. 'cascade',
  119. 'concurrently',
  120. ]),
  121. postgres: new Error(
  122. `Cannot specify both concurrently and cascade options in removeIndexQuery for ${dialect.name} dialect`,
  123. ),
  124. snowflake: notImplementedError,
  125. },
  126. );
  127. });
  128. it('produces a DROP INDEX query from a model', () => {
  129. const MyModel = sequelize.define('MyModel', {});
  130. expectsql(() => queryGenerator.removeIndexQuery(MyModel, 'user_foo_bar'), {
  131. default: `DROP INDEX [user_foo_bar] ON [MyModels]`,
  132. sqlite3: 'DROP INDEX `user_foo_bar`',
  133. ibmi: `BEGIN DROP INDEX "user_foo_bar"; COMMIT; END`,
  134. db2: `DROP INDEX "user_foo_bar"`,
  135. postgres: `DROP INDEX "public"."user_foo_bar"`,
  136. snowflake: notImplementedError,
  137. });
  138. });
  139. it('produces a DROP INDEX query from a model definition', () => {
  140. const MyModel = sequelize.define('MyModel', {});
  141. const myDefinition = MyModel.modelDefinition;
  142. expectsql(() => queryGenerator.removeIndexQuery(myDefinition, 'user_foo_bar'), {
  143. default: `DROP INDEX [user_foo_bar] ON [MyModels]`,
  144. sqlite3: 'DROP INDEX `user_foo_bar`',
  145. ibmi: `BEGIN DROP INDEX "user_foo_bar"; COMMIT; END`,
  146. db2: `DROP INDEX "user_foo_bar"`,
  147. postgres: `DROP INDEX "public"."user_foo_bar"`,
  148. snowflake: notImplementedError,
  149. });
  150. });
  151. it('produces a DROP INDEX query from a table and schema', () => {
  152. expectsql(
  153. () =>
  154. queryGenerator.removeIndexQuery(
  155. { tableName: 'myTable', schema: 'mySchema' },
  156. 'user_foo_bar',
  157. ),
  158. {
  159. default: `DROP INDEX [user_foo_bar] ON [mySchema].[myTable]`,
  160. sqlite3: 'DROP INDEX `user_foo_bar`',
  161. postgres: `DROP INDEX "mySchema"."user_foo_bar"`,
  162. ibmi: `BEGIN DROP INDEX "user_foo_bar"; COMMIT; END`,
  163. db2: `DROP INDEX "user_foo_bar"`,
  164. snowflake: notImplementedError,
  165. },
  166. );
  167. });
  168. it('produces a DROP INDEX query from a table and default schema', () => {
  169. expectsql(
  170. () =>
  171. queryGenerator.removeIndexQuery(
  172. { tableName: 'myTable', schema: dialect.getDefaultSchema() },
  173. 'user_foo_bar',
  174. ),
  175. {
  176. default: `DROP INDEX [user_foo_bar] ON [myTable]`,
  177. sqlite3: 'DROP INDEX `user_foo_bar`',
  178. ibmi: `BEGIN DROP INDEX "user_foo_bar"; COMMIT; END`,
  179. db2: `DROP INDEX "user_foo_bar"`,
  180. postgres: `DROP INDEX "public"."user_foo_bar"`,
  181. snowflake: notImplementedError,
  182. },
  183. );
  184. });
  185. it('produces a DROP INDEX query from a table and globally set schema', () => {
  186. const sequelizeSchema = createSequelizeInstance({ schema: 'mySchema' });
  187. const queryGeneratorSchema = sequelizeSchema.queryGenerator;
  188. expectsql(() => queryGeneratorSchema.removeIndexQuery('myTable', 'user_foo_bar'), {
  189. default: `DROP INDEX [user_foo_bar] ON [mySchema].[myTable]`,
  190. sqlite3: 'DROP INDEX `user_foo_bar`',
  191. postgres: `DROP INDEX "mySchema"."user_foo_bar"`,
  192. ibmi: `BEGIN DROP INDEX "user_foo_bar"; COMMIT; END`,
  193. db2: 'DROP INDEX "user_foo_bar"',
  194. snowflake: notImplementedError,
  195. });
  196. });
  197. });