table-exists.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { DataTypes } from '@sequelize/core';
  2. import { expect } from 'chai';
  3. import { sequelize } from '../support';
  4. const queryInterface = sequelize.queryInterface;
  5. describe('QueryInterface#tableExists', () => {
  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 return true if table exists', async () => {
  21. const exists = await queryInterface.tableExists('levels');
  22. expect(exists).to.be.true;
  23. });
  24. it('should return false if table does not exist', async () => {
  25. const exists = await queryInterface.tableExists('actors');
  26. expect(exists).to.be.false;
  27. });
  28. });
  29. if (sequelize.dialect.supports.schemas) {
  30. describe('With schema', () => {
  31. beforeEach(async () => {
  32. await queryInterface.createSchema('archive');
  33. await queryInterface.createTable(
  34. { tableName: 'levels', schema: 'archive' },
  35. {
  36. id: {
  37. type: DataTypes.INTEGER,
  38. primaryKey: true,
  39. autoIncrement: true,
  40. },
  41. name: {
  42. type: DataTypes.STRING,
  43. allowNull: false,
  44. },
  45. },
  46. );
  47. });
  48. it('should return true if table exists', async () => {
  49. const exists = await queryInterface.tableExists({ tableName: 'levels', schema: 'archive' });
  50. expect(exists).to.be.true;
  51. });
  52. it('should return false if table does not exist', async () => {
  53. const exists = await queryInterface.tableExists({ tableName: 'actors', schema: 'archive' });
  54. expect(exists).to.be.false;
  55. });
  56. });
  57. }
  58. });