createTable.test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../support');
  5. const { DataTypes } = require('@sequelize/core');
  6. const dialect = Support.getTestDialect();
  7. describe(Support.getTestDialectTeaser('QueryInterface'), () => {
  8. beforeEach(function () {
  9. this.queryInterface = this.sequelize.queryInterface;
  10. });
  11. describe('createTable', () => {
  12. it('should create a auto increment primary key', async function () {
  13. await this.queryInterface.createTable('TableWithPK', {
  14. table_id: {
  15. type: DataTypes.INTEGER,
  16. primaryKey: true,
  17. autoIncrement: true,
  18. },
  19. });
  20. const result = await this.queryInterface.describeTable('TableWithPK');
  21. if (['mssql', 'mysql', 'mariadb'].includes(dialect)) {
  22. expect(result.table_id.autoIncrement).to.be.true;
  23. } else if (dialect === 'postgres') {
  24. expect(result.table_id.defaultValue).to.equal(
  25. 'nextval("TableWithPK_table_id_seq"::regclass)',
  26. );
  27. }
  28. });
  29. // SQLITE does not respect the index name when the index is created through CREATE TABLE
  30. // As such, Sequelize's createTable does not add the constraint in the Sequelize Dialect.
  31. // Instead, `sequelize.sync` calls CREATE INDEX after the table has been created,
  32. // as that query *does* respect the index name.
  33. if (dialect !== 'sqlite3') {
  34. it('should create unique constraint with uniqueKeys', async function () {
  35. await this.queryInterface.createTable(
  36. 'MyTable',
  37. {
  38. id: {
  39. type: DataTypes.INTEGER,
  40. primaryKey: true,
  41. autoIncrement: true,
  42. },
  43. name: {
  44. type: DataTypes.STRING,
  45. },
  46. email: {
  47. type: DataTypes.STRING,
  48. },
  49. },
  50. {
  51. uniqueKeys: {
  52. myCustomIndex: {
  53. fields: ['name', 'email'],
  54. },
  55. myOtherIndex: {
  56. fields: ['name'],
  57. },
  58. },
  59. },
  60. );
  61. const indexes = (await this.queryInterface.showIndex('MyTable'))
  62. .filter(index => !index.primary)
  63. .sort((a, b) => a.name.localeCompare(b.name));
  64. for (const index of indexes) {
  65. index.fields.sort((a, b) => a.attribute.localeCompare(b.attribute));
  66. }
  67. // name + email
  68. expect(indexes[0].unique).to.be.true;
  69. expect(indexes[0].fields[0].attribute).to.equal('email');
  70. expect(indexes[0].fields[1].attribute).to.equal('name');
  71. // name
  72. expect(indexes[1].unique).to.be.true;
  73. expect(indexes[1].fields[0].attribute).to.equal('name');
  74. });
  75. }
  76. if (Support.sequelize.dialect.supports.schemas) {
  77. it('should work with schemas', async function () {
  78. await this.sequelize.createSchema('hero');
  79. await this.queryInterface.createTable(
  80. 'User',
  81. {
  82. name: {
  83. type: DataTypes.STRING,
  84. },
  85. },
  86. {
  87. schema: 'hero',
  88. },
  89. );
  90. });
  91. }
  92. describe('enums', () => {
  93. it('should work with enums (1)', async function () {
  94. await this.queryInterface.createTable('SomeTable', {
  95. someEnum: DataTypes.ENUM('value1', 'value2', 'value3'),
  96. });
  97. const table = await this.queryInterface.describeTable('SomeTable');
  98. if (dialect.includes('postgres')) {
  99. expect(table.someEnum.special).to.deep.equal(['value1', 'value2', 'value3']);
  100. }
  101. });
  102. it('should work with enums (2)', async function () {
  103. await this.queryInterface.createTable('SomeTable', {
  104. someEnum: {
  105. type: DataTypes.ENUM(['value1', 'value2', 'value3']),
  106. },
  107. });
  108. const table = await this.queryInterface.describeTable('SomeTable');
  109. if (dialect.includes('postgres')) {
  110. expect(table.someEnum.special).to.deep.equal(['value1', 'value2', 'value3']);
  111. }
  112. });
  113. it('should work with enums (3)', async function () {
  114. await this.queryInterface.createTable('SomeTable', {
  115. someEnum: {
  116. type: DataTypes.ENUM(['value1', 'value2', 'value3']),
  117. field: 'otherName',
  118. },
  119. });
  120. const table = await this.queryInterface.describeTable('SomeTable');
  121. if (dialect.includes('postgres')) {
  122. expect(table.otherName.special).to.deep.equal(['value1', 'value2', 'value3']);
  123. }
  124. });
  125. if (Support.sequelize.dialect.supports.schemas) {
  126. it('should work with enums (4, schemas)', async function () {
  127. await this.queryInterface.createSchema('archive');
  128. await this.queryInterface.createTable(
  129. 'SomeTable',
  130. {
  131. someEnum: {
  132. type: DataTypes.ENUM(['value1', 'value2', 'value3']),
  133. field: 'otherName',
  134. },
  135. },
  136. { schema: 'archive' },
  137. );
  138. const table = await this.queryInterface.describeTable({
  139. tableName: 'SomeTable',
  140. schema: 'archive',
  141. });
  142. if (dialect.includes('postgres')) {
  143. expect(table.otherName.special).to.deep.equal(['value1', 'value2', 'value3']);
  144. }
  145. });
  146. }
  147. it('should work with enums (5)', async function () {
  148. await this.queryInterface.createTable('SomeTable', {
  149. someEnum: {
  150. type: DataTypes.ENUM(['COMMENT']),
  151. comment: 'special enum col',
  152. },
  153. });
  154. const table = await this.queryInterface.describeTable('SomeTable');
  155. if (dialect.includes('postgres')) {
  156. expect(table.someEnum.special).to.deep.equal(['COMMENT']);
  157. expect(table.someEnum.comment).to.equal('special enum col');
  158. }
  159. });
  160. it('should work with multiple enums', async function () {
  161. await this.queryInterface.createTable('SomeTable', {
  162. someEnum: DataTypes.ENUM('value1', 'value2', 'value3'),
  163. });
  164. // Drop the table, this will leave the enum type behind
  165. await this.queryInterface.dropTable('SomeTable');
  166. // Create the table again with a second enum this time
  167. await this.queryInterface.createTable('SomeTable', {
  168. someEnum: DataTypes.ENUM('value1', 'value2', 'value3'),
  169. someOtherEnum: DataTypes.ENUM('otherValue1', 'otherValue2', 'otherValue3'),
  170. });
  171. const table = await this.queryInterface.describeTable('SomeTable');
  172. if (dialect.includes('postgres')) {
  173. expect(table.someEnum.special).to.deep.equal(['value1', 'value2', 'value3']);
  174. expect(table.someOtherEnum.special).to.deep.equal([
  175. 'otherValue1',
  176. 'otherValue2',
  177. 'otherValue3',
  178. ]);
  179. }
  180. });
  181. });
  182. });
  183. });