table-exists-query.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { createSequelizeInstance, expectsql, sequelize } from '../../support';
  2. const dialect = sequelize.dialect;
  3. describe('QueryGenerator#tableExistsQuery', () => {
  4. const queryGenerator = sequelize.queryGenerator;
  5. const defaultSchema = dialect.getDefaultSchema();
  6. it('produces a table exists query for a table', () => {
  7. expectsql(() => queryGenerator.tableExistsQuery('myTable'), {
  8. default: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'myTable' AND TABLE_SCHEMA = '${defaultSchema}'`,
  9. db2: `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME = 'myTable' AND TABSCHEMA = '${defaultSchema}'`,
  10. ibmi: `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_NAME = 'myTable' AND TABLE_SCHEMA = CURRENT SCHEMA`,
  11. mssql: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = N'myTable' AND TABLE_SCHEMA = N'${defaultSchema}'`,
  12. sqlite3: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'myTable'`,
  13. });
  14. });
  15. it('produces a table exists query for a model', () => {
  16. const MyModel = sequelize.define('MyModel', {});
  17. expectsql(() => queryGenerator.tableExistsQuery(MyModel), {
  18. default: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'MyModels' AND TABLE_SCHEMA = '${defaultSchema}'`,
  19. db2: `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME = 'MyModels' AND TABSCHEMA = '${defaultSchema}'`,
  20. ibmi: `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_NAME = 'MyModels' AND TABLE_SCHEMA = CURRENT SCHEMA`,
  21. mssql: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = N'MyModels' AND TABLE_SCHEMA = N'${defaultSchema}'`,
  22. sqlite3: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'MyModels'`,
  23. });
  24. });
  25. it('produces a table exists query for a model definition', () => {
  26. const MyModel = sequelize.define('MyModel', {});
  27. const myDefinition = MyModel.modelDefinition;
  28. expectsql(() => queryGenerator.tableExistsQuery(myDefinition), {
  29. default: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'MyModels' AND TABLE_SCHEMA = '${defaultSchema}'`,
  30. db2: `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME = 'MyModels' AND TABSCHEMA = '${defaultSchema}'`,
  31. ibmi: `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_NAME = 'MyModels' AND TABLE_SCHEMA = CURRENT SCHEMA`,
  32. mssql: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = N'MyModels' AND TABLE_SCHEMA = N'${defaultSchema}'`,
  33. sqlite3: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'MyModels'`,
  34. });
  35. });
  36. it('produces a table exists query for a table and schema', () => {
  37. expectsql(() => queryGenerator.tableExistsQuery({ tableName: 'myTable', schema: 'mySchema' }), {
  38. default: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'myTable' AND TABLE_SCHEMA = 'mySchema'`,
  39. db2: `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME = 'myTable' AND TABSCHEMA = 'mySchema'`,
  40. ibmi: `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_NAME = 'myTable' AND TABLE_SCHEMA = 'mySchema'`,
  41. mssql: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = N'myTable' AND TABLE_SCHEMA = N'mySchema'`,
  42. sqlite3: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'mySchema.myTable'`,
  43. });
  44. });
  45. it('produces a table exists query for a table and default schema', () => {
  46. expectsql(
  47. () =>
  48. queryGenerator.tableExistsQuery({
  49. tableName: 'myTable',
  50. schema: dialect.getDefaultSchema(),
  51. }),
  52. {
  53. default: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'myTable' AND TABLE_SCHEMA = '${defaultSchema}'`,
  54. db2: `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME = 'myTable' AND TABSCHEMA = '${defaultSchema}'`,
  55. ibmi: `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_NAME = 'myTable' AND TABLE_SCHEMA = CURRENT SCHEMA`,
  56. mssql: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = N'myTable' AND TABLE_SCHEMA = N'${defaultSchema}'`,
  57. sqlite3: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'myTable'`,
  58. },
  59. );
  60. });
  61. it('produces a table exists query for a table and globally set schema', () => {
  62. const sequelizeSchema = createSequelizeInstance({ schema: 'mySchema' });
  63. const queryGeneratorSchema = sequelizeSchema.queryGenerator;
  64. expectsql(() => queryGeneratorSchema.tableExistsQuery('myTable'), {
  65. default: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'myTable' AND TABLE_SCHEMA = 'mySchema'`,
  66. db2: `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME = 'myTable' AND TABSCHEMA = 'mySchema'`,
  67. ibmi: `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_NAME = 'myTable' AND TABLE_SCHEMA = 'mySchema'`,
  68. mssql: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = N'myTable' AND TABLE_SCHEMA = N'mySchema'`,
  69. sqlite3: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'mySchema.myTable'`,
  70. });
  71. });
  72. it('produces a table exists query for a table with schema and custom delimiter argument', () => {
  73. // This test is only relevant for dialects that do not support schemas
  74. if (dialect.supports.schemas) {
  75. return;
  76. }
  77. expectsql(
  78. () =>
  79. queryGenerator.tableExistsQuery({
  80. tableName: 'myTable',
  81. schema: 'mySchema',
  82. delimiter: 'custom',
  83. }),
  84. {
  85. sqlite3: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'mySchemacustommyTable'`,
  86. },
  87. );
  88. });
  89. });