regressions.test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. 'use strict';
  2. const chai = require('chai');
  3. const times = require('lodash/times');
  4. const expect = chai.expect;
  5. const sinon = require('sinon');
  6. const Support = require('../../support');
  7. const { DataTypes, Op, sql } = require('@sequelize/core');
  8. const dialect = Support.getTestDialect();
  9. if (dialect.startsWith('mssql')) {
  10. describe(Support.getTestDialectTeaser('Regressions'), () => {
  11. it('does not duplicate columns in ORDER BY statement, #9008', async function () {
  12. const LoginLog = this.sequelize.define('LoginLog', {
  13. ID: {
  14. field: 'id',
  15. type: DataTypes.INTEGER,
  16. primaryKey: true,
  17. autoIncrement: true,
  18. },
  19. UserID: {
  20. field: 'userid',
  21. type: DataTypes.UUID,
  22. allowNull: false,
  23. },
  24. });
  25. const User = this.sequelize.define('User', {
  26. UserID: {
  27. field: 'userid',
  28. type: DataTypes.UUID,
  29. defaultValue: sql.uuidV4,
  30. primaryKey: true,
  31. },
  32. UserName: {
  33. field: 'username',
  34. type: DataTypes.STRING(50),
  35. allowNull: false,
  36. },
  37. });
  38. LoginLog.belongsTo(User, {
  39. foreignKey: 'UserID',
  40. });
  41. User.hasMany(LoginLog, {
  42. foreignKey: 'UserID',
  43. });
  44. await this.sequelize.sync({ force: true });
  45. const vyom = await User.create({ UserName: 'Vayom' });
  46. const shakti = await User.create({ UserName: 'Shaktimaan' });
  47. const nikita = await User.create({ UserName: 'Nikita' });
  48. const arya = await User.create({ UserName: 'Aryamaan' });
  49. await Promise.all([
  50. vyom.createLoginLog(),
  51. shakti.createLoginLog(),
  52. nikita.createLoginLog(),
  53. arya.createLoginLog(),
  54. ]);
  55. const logs = await LoginLog.findAll({
  56. include: [
  57. {
  58. model: User,
  59. where: {
  60. UserName: {
  61. [Op.like]: '%maan%',
  62. },
  63. },
  64. },
  65. ],
  66. order: [[User, 'UserName', 'DESC']],
  67. offset: 0,
  68. limit: 10,
  69. });
  70. expect(logs).to.have.length(2);
  71. expect(logs[0].user.get('UserName')).to.equal('Shaktimaan');
  72. expect(logs[1].user.get('UserName')).to.equal('Aryamaan');
  73. // #11258 and similar
  74. const otherLogs = await LoginLog.findAll({
  75. include: [
  76. {
  77. model: User,
  78. where: {
  79. UserName: {
  80. [Op.like]: '%maan%',
  81. },
  82. },
  83. },
  84. ],
  85. order: [['id', 'DESC']],
  86. offset: 0,
  87. limit: 10,
  88. });
  89. expect(otherLogs).to.have.length(2);
  90. expect(otherLogs[0].user.get('UserName')).to.equal('Aryamaan');
  91. expect(otherLogs[1].user.get('UserName')).to.equal('Shaktimaan');
  92. // Separate queries can apply order freely
  93. const separateUsers = await User.findAll({
  94. include: [
  95. {
  96. model: LoginLog,
  97. separate: true,
  98. order: ['id'],
  99. },
  100. ],
  101. where: {
  102. UserName: {
  103. [Op.like]: '%maan%',
  104. },
  105. },
  106. order: ['UserName', ['UserID', 'DESC']],
  107. offset: 0,
  108. limit: 10,
  109. });
  110. expect(separateUsers).to.have.length(2);
  111. expect(separateUsers[0].get('UserName')).to.equal('Aryamaan');
  112. expect(separateUsers[0].get('loginLogs')).to.have.length(1);
  113. expect(separateUsers[1].get('UserName')).to.equal('Shaktimaan');
  114. expect(separateUsers[1].get('loginLogs')).to.have.length(1);
  115. });
  116. it('allow referencing FK to different tables in a schema with onDelete, #10125', async function () {
  117. const Child = this.sequelize.define(
  118. 'Child',
  119. {},
  120. {
  121. timestamps: false,
  122. freezeTableName: true,
  123. schema: 'a',
  124. },
  125. );
  126. const Toys = this.sequelize.define(
  127. 'Toys',
  128. {},
  129. {
  130. timestamps: false,
  131. freezeTableName: true,
  132. schema: 'a',
  133. },
  134. );
  135. const Parent = this.sequelize.define(
  136. 'Parent',
  137. {},
  138. {
  139. timestamps: false,
  140. freezeTableName: true,
  141. schema: 'a',
  142. },
  143. );
  144. Child.hasOne(Toys, {
  145. foreignKey: { onDelete: 'CASCADE' },
  146. });
  147. Parent.hasOne(Toys, {
  148. foreignKey: { onDelete: 'CASCADE' },
  149. });
  150. const spy = sinon.spy();
  151. await this.sequelize.queryInterface.createSchema('a');
  152. await this.sequelize.sync({
  153. force: true,
  154. logging: spy,
  155. });
  156. expect(spy).to.have.been.called;
  157. const log = spy.args.find(arg =>
  158. arg[0].includes(`IF OBJECT_ID(N'[a].[Toys]', 'U') IS NULL CREATE TABLE`),
  159. )[0];
  160. expect(log.match(/ON DELETE CASCADE/g).length).to.equal(2);
  161. });
  162. it('sets the varchar(max) length correctly on describeTable', async function () {
  163. const Users = this.sequelize.define(
  164. '_Users',
  165. {
  166. username: DataTypes.STRING('MAX'),
  167. },
  168. { freezeTableName: true },
  169. );
  170. await Users.sync({ force: true });
  171. const metadata = await this.sequelize.queryInterface.describeTable('_Users');
  172. const username = metadata.username;
  173. expect(username.type).to.include('(MAX)');
  174. });
  175. it('sets the char(10) length correctly on describeTable', async function () {
  176. const Users = this.sequelize.define(
  177. '_Users',
  178. {
  179. username: DataTypes.CHAR(10),
  180. },
  181. { freezeTableName: true },
  182. );
  183. await Users.sync({ force: true });
  184. const metadata = await this.sequelize.queryInterface.describeTable('_Users');
  185. const username = metadata.username;
  186. expect(username.type).to.include('(10)');
  187. });
  188. it('saves value bigger than 2147483647, #11245', async function () {
  189. const BigIntTable = this.sequelize.define(
  190. 'BigIntTable',
  191. {
  192. business_id: {
  193. type: DataTypes.BIGINT,
  194. allowNull: false,
  195. },
  196. },
  197. {
  198. freezeTableName: true,
  199. },
  200. );
  201. const bigIntValue = 2_147_483_648;
  202. await BigIntTable.sync({ force: true });
  203. await BigIntTable.create({
  204. business_id: bigIntValue,
  205. });
  206. const record = await BigIntTable.findOne();
  207. expect(Number(record.business_id)).to.equals(bigIntValue);
  208. });
  209. it('saves boolean is true, #12090', async function () {
  210. const BooleanTable = this.sequelize.define(
  211. 'BooleanTable',
  212. {
  213. status: {
  214. type: DataTypes.BOOLEAN,
  215. allowNull: false,
  216. },
  217. },
  218. {
  219. freezeTableName: true,
  220. },
  221. );
  222. const value = true;
  223. await BooleanTable.sync({ force: true });
  224. await BooleanTable.create({
  225. status: value,
  226. });
  227. const record = await BooleanTable.findOne();
  228. expect(record.status).to.equals(value);
  229. });
  230. // Fixes https://github.com/sequelize/sequelize/issues/15426
  231. it('rolls back changes after inserting more than 1000 rows outside of a transaction', async function () {
  232. const User = this.sequelize.define('User', {
  233. username: {
  234. type: DataTypes.STRING,
  235. allowNull: false,
  236. },
  237. });
  238. await User.sync({ force: true });
  239. try {
  240. await User.bulkCreate([...times(1000, () => ({ username: 'John' })), { username: null }]);
  241. } catch {
  242. // ignore
  243. }
  244. const count = await User.count();
  245. expect(count).to.equal(0);
  246. });
  247. });
  248. }