remove-constraint-query.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 notSupportedError = new Error(
  5. `Remove constraint queries are not supported by ${dialect.name} dialect`,
  6. );
  7. describe('QueryGenerator#removeConstraintQuery', () => {
  8. const queryGenerator = sequelize.queryGenerator;
  9. it('generates a query that drops a constraint', () => {
  10. expectsql(() => queryGenerator.removeConstraintQuery('myTable', 'myConstraint'), {
  11. default: 'ALTER TABLE [myTable] DROP CONSTRAINT [myConstraint]',
  12. sqlite3: notSupportedError,
  13. });
  14. });
  15. it('generates a query that drops a constraint with IF EXISTS', () => {
  16. expectsql(
  17. () => queryGenerator.removeConstraintQuery('myTable', 'myConstraint', { ifExists: true }),
  18. {
  19. default: 'ALTER TABLE [myTable] DROP CONSTRAINT IF EXISTS [myConstraint]',
  20. sqlite3: notSupportedError,
  21. 'db2 ibmi mysql snowflake': buildInvalidOptionReceivedError(
  22. 'removeConstraintQuery',
  23. dialect.name,
  24. ['ifExists'],
  25. ),
  26. },
  27. );
  28. });
  29. it('generates a query that drops a constraint with CASCADE', () => {
  30. expectsql(
  31. () => queryGenerator.removeConstraintQuery('myTable', 'myConstraint', { cascade: true }),
  32. {
  33. default: buildInvalidOptionReceivedError('removeConstraintQuery', dialect.name, [
  34. 'cascade',
  35. ]),
  36. sqlite3: notSupportedError,
  37. 'postgres snowflake': 'ALTER TABLE [myTable] DROP CONSTRAINT [myConstraint] CASCADE',
  38. },
  39. );
  40. });
  41. it('generates a query that drops a constraint with IF EXISTS and CASCADE', () => {
  42. expectsql(
  43. () =>
  44. queryGenerator.removeConstraintQuery('myTable', 'myConstraint', {
  45. cascade: true,
  46. ifExists: true,
  47. }),
  48. {
  49. default: buildInvalidOptionReceivedError('removeConstraintQuery', dialect.name, [
  50. 'cascade',
  51. ]),
  52. postgres: 'ALTER TABLE "myTable" DROP CONSTRAINT IF EXISTS "myConstraint" CASCADE',
  53. snowflake: buildInvalidOptionReceivedError('removeConstraintQuery', dialect.name, [
  54. 'ifExists',
  55. ]),
  56. sqlite3: notSupportedError,
  57. },
  58. );
  59. });
  60. it('generates a query that drops a constraint from a model', () => {
  61. const MyModel = sequelize.define('MyModel', {});
  62. expectsql(() => queryGenerator.removeConstraintQuery(MyModel, 'myConstraint'), {
  63. default: 'ALTER TABLE [MyModels] DROP CONSTRAINT [myConstraint]',
  64. sqlite3: notSupportedError,
  65. });
  66. });
  67. it('generates a query that drops a constraint from a model definition', () => {
  68. const MyModel = sequelize.define('MyModel', {});
  69. const myDefinition = MyModel.modelDefinition;
  70. expectsql(() => queryGenerator.removeConstraintQuery(myDefinition, 'myConstraint'), {
  71. default: 'ALTER TABLE [MyModels] DROP CONSTRAINT [myConstraint]',
  72. sqlite3: notSupportedError,
  73. });
  74. });
  75. it('generates a query that drops a constraint with schema', () => {
  76. expectsql(
  77. () =>
  78. queryGenerator.removeConstraintQuery(
  79. { tableName: 'myTable', schema: 'mySchema' },
  80. 'myConstraint',
  81. ),
  82. {
  83. default: 'ALTER TABLE [mySchema].[myTable] DROP CONSTRAINT [myConstraint]',
  84. sqlite3: notSupportedError,
  85. },
  86. );
  87. });
  88. it('generates a query that drops a constraint with default schema', () => {
  89. expectsql(
  90. () =>
  91. queryGenerator.removeConstraintQuery(
  92. { tableName: 'myTable', schema: dialect.getDefaultSchema() },
  93. 'myConstraint',
  94. ),
  95. {
  96. default: 'ALTER TABLE [myTable] DROP CONSTRAINT [myConstraint]',
  97. sqlite3: notSupportedError,
  98. },
  99. );
  100. });
  101. it('generates a query that drops a constraint from a table and globally set schema', () => {
  102. const sequelizeSchema = createSequelizeInstance({ schema: 'mySchema' });
  103. const queryGeneratorSchema = sequelizeSchema.queryGenerator;
  104. expectsql(() => queryGeneratorSchema.removeConstraintQuery('myTable', 'myConstraint'), {
  105. default: 'ALTER TABLE [mySchema].[myTable] DROP CONSTRAINT [myConstraint]',
  106. sqlite3: notSupportedError,
  107. });
  108. });
  109. it('generates a query that drops a column 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. expectsql(
  115. () =>
  116. queryGenerator.removeConstraintQuery(
  117. { tableName: 'myTable', schema: 'mySchema', delimiter: 'custom' },
  118. 'myConstraint',
  119. ),
  120. {
  121. sqlite3: notSupportedError,
  122. },
  123. );
  124. });
  125. });