drop-table.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { DataTypes } from '@sequelize/core';
  2. import { expect } from 'chai';
  3. import { sequelize } from '../support';
  4. const queryInterface = sequelize.queryInterface;
  5. describe('QueryInterface#dropTable', () => {
  6. describe('Without schema', () => {
  7. beforeEach(async () => {
  8. await queryInterface.createTable('levels', {
  9. id: {
  10. type: DataTypes.INTEGER,
  11. autoIncrement: true,
  12. primaryKey: true,
  13. },
  14. name: {
  15. type: DataTypes.STRING,
  16. allowNull: false,
  17. },
  18. });
  19. });
  20. it('should drop a table', async () => {
  21. await queryInterface.dropTable('levels');
  22. const exists = await queryInterface.tableExists('levels');
  23. expect(exists).to.be.false;
  24. });
  25. if (sequelize.dialect.supports.dropTable.cascade) {
  26. it('should drop a table with dependencies', async () => {
  27. await queryInterface.createTable('actors', {
  28. id: {
  29. type: DataTypes.INTEGER,
  30. autoIncrement: true,
  31. primaryKey: true,
  32. },
  33. levelId: {
  34. type: DataTypes.INTEGER,
  35. references: {
  36. table: 'levels',
  37. key: 'id',
  38. },
  39. },
  40. });
  41. await queryInterface.dropTable('levels', { cascade: true });
  42. const allTables = await queryInterface.listTables();
  43. const tableNames = allTables.map(table => table.tableName);
  44. // Cascade only removes the foreign key constraint, not the related table
  45. expect(tableNames).to.deep.equal(['actors']);
  46. });
  47. }
  48. });
  49. if (sequelize.dialect.supports.schemas) {
  50. describe('With schema', () => {
  51. beforeEach(async () => {
  52. await queryInterface.createSchema('archive');
  53. await queryInterface.createTable(
  54. { tableName: 'levels', schema: 'archive' },
  55. {
  56. id: {
  57. type: DataTypes.INTEGER,
  58. primaryKey: true,
  59. autoIncrement: true,
  60. },
  61. name: {
  62. type: DataTypes.STRING,
  63. allowNull: false,
  64. },
  65. },
  66. );
  67. });
  68. it('should drop a table', async () => {
  69. await queryInterface.dropTable({ tableName: 'levels', schema: 'archive' });
  70. const exists = await queryInterface.tableExists({ tableName: 'levels', schema: 'archive' });
  71. expect(exists).to.be.false;
  72. });
  73. });
  74. }
  75. });