<commit_before>'use strict';

const chai = require('chai');
const expect = chai.expect;
const Support = require(__dirname + '/../../support');
const dialect = Support.getTestDialect();
const sinon = require('sinon');
const DataTypes = require(__dirname + '/../../../../lib/data-types');

if (dialect === 'mysql') {
  describe('[MYSQL Specific] Connection Manager', () => {
    it('works correctly after being idle', function() {
      const User = this.sequelize.define('User', { username: DataTypes.STRING });
      const spy = sinon.spy();

      return User
        .sync({force: true})
        .then(() => User.create({username: 'user1'}))
        .then(() => User.count())
        .then(count => {
          expect(count).to.equal(1);
          spy();
          return this.sequelize.Promise.delay(1000);
        })
        .then(() => User.count())
        .then(count => {
          expect(count).to.equal(1);
          spy();
          if (!spy.calledTwice) {
            throw new Error('Spy was not called twice');
          }
        });
    });

    it('accepts new queries after shutting down a connection', () => {
      // Create a sequelize instance with fast disconnecting connection
      const sequelize = Support.createSequelizeInstance({ pool: { idle: 50, max: 1 }});
      const User = sequelize.define('User', { username: DataTypes.STRING });

      return User
      .sync({force: true})
      .then(() => User.create({username: 'user1'}))
      .then(() => sequelize.Promise.delay(100))
      .then(() => {
        // This query will be queued just after the `client.end` is executed and before its callback is called
        return sequelize.query('SELECT COUNT(*) AS count FROM Users', { type: sequelize.QueryTypes.SELECT });
      })
      .then(count => {
        expect(count[0].count).to.equal(1);
      });
    });

    it('should maintain connection', () => {
      const sequelize = Support.createSequelizeInstance({ pool: {min: 1, max: 1, handleDisconnects: true, idle: 5000 }});
      const cm = sequelize.connectionManager;
      let conn;

      return sequelize.sync()
        .then(() => cm.getConnection())
        .then(connection => {
          // Save current connection
          conn = connection;

          return cm.releaseConnection(conn);
        })
        .then(() => {
          // Get next available connection
          return cm.getConnection();
        })
        .then(connection => {
          // Old threadId should be different from current new one
          expect(conn.threadId).to.be.equal(connection.threadId);
          expect(cm.validate(conn)).to.be.ok;

          return cm.releaseConnection(connection);
        });
    });

    it('should work with handleDisconnects', () => {
      const sequelize = Support.createSequelizeInstance({pool: {min: 1, max: 1, handleDisconnects: true, idle: 5000}});
      const cm = sequelize.connectionManager;
      let conn;

      return sequelize
        .sync()
        .then(() => cm.getConnection())
        .then(connection => {
          // Save current connection
          conn = connection;
          // simulate a unexpected end
          connection.close();
        })
        .then(() => cm.releaseConnection(conn))
        .then(() => {
          // Get next available connection
          return cm.getConnection();
        })
        .then(connection => {
          // Old threadId should be different from current new one
          expect(conn.threadId).to.not.be.equal(connection.threadId);
          expect(cm.validate(conn)).to.not.be.ok;
          return cm.releaseConnection(connection);
        });
    });
  });
}
<commit_msg>#7184 Regression in affected rows reporting for updates (#7423)<commit_after>'use strict';

const chai = require('chai');
const expect = chai.expect;
const Support = require(__dirname + '/../../support');
const dialect = Support.getTestDialect();
const sinon = require('sinon');
const DataTypes = require(__dirname + '/../../../../lib/data-types');

if (dialect === 'mysql') {
  describe('[MYSQL Specific] Connection Manager', () => {
    it('works correctly after being idle', function() {
      const User = this.sequelize.define('User', { username: DataTypes.STRING });
      const spy = sinon.spy();

      return User
        .sync({force: true})
        .then(() => User.create({username: 'user1'}))
        .then(() => User.count())
        .then(count => {
          expect(count).to.equal(1);
          spy();
          return this.sequelize.Promise.delay(1000);
        })
        .then(() => User.count())
        .then(count => {
          expect(count).to.equal(1);
          spy();
          if (!spy.calledTwice) {
            throw new Error('Spy was not called twice');
          }
        });
    });

    it('accepts new queries after shutting down a connection', () => {
      // Create a sequelize instance with fast disconnecting connection
      const sequelize = Support.createSequelizeInstance({ pool: { idle: 50, max: 1 }});
      const User = sequelize.define('User', { username: DataTypes.STRING });

      return User
      .sync({force: true})
      .then(() => User.create({username: 'user1'}))
      .then(() => sequelize.Promise.delay(100))
      .then(() => {
        // This query will be queued just after the `client.end` is executed and before its callback is called
        return sequelize.query('SELECT COUNT(*) AS count FROM Users', { type: sequelize.QueryTypes.SELECT });
      })
      .then(count => {
        expect(count[0].count).to.equal(1);
      });
    });

    it('should maintain connection', () => {
      const sequelize = Support.createSequelizeInstance({ pool: {min: 1, max: 1, handleDisconnects: true, idle: 5000 }});
      const cm = sequelize.connectionManager;
      let conn;

      return sequelize.sync()
        .then(() => cm.getConnection())
        .then(connection => {
          // Save current connection
          conn = connection;

          return cm.releaseConnection(conn);
        })
        .then(() => {
          // Get next available connection
          return cm.getConnection();
        })
        .then(connection => {
          // Old threadId should be different from current new one
          expect(conn.threadId).to.be.equal(connection.threadId);
          expect(cm.validate(conn)).to.be.ok;

          return cm.releaseConnection(connection);
        });
    });

    it('should work with handleDisconnects', () => {
      const sequelize = Support.createSequelizeInstance({pool: {min: 1, max: 1, handleDisconnects: true, idle: 5000}});
      const cm = sequelize.connectionManager;
      let conn;

      return sequelize
        .sync()
        .then(() => cm.getConnection())
        .then(connection => {
          // Save current connection
          conn = connection;
          // simulate a unexpected end
          connection.close();
        })
        .then(() => cm.releaseConnection(conn))
        .then(() => {
          // Get next available connection
          return cm.getConnection();
        })
        .then(connection => {
          // Old threadId should be different from current new one
          expect(conn.threadId).to.not.be.equal(connection.threadId);
          expect(cm.validate(conn)).to.not.be.ok;
          return cm.releaseConnection(connection);
        });
    });

    it('-FOUND_ROWS can be suppressed to get back legacy behavior', () => {
      const sequelize = Support.createSequelizeInstance({ dialectOptions: { flags: '' }});
      const User = sequelize.define('User', { username: DataTypes.STRING });

      return User.sync({force: true})
        .then(() => User.create({ id: 1, username: 'jozef' }))
        .then(() => User.update({ username: 'jozef'}, {
          where: {
            id: 1
          }
        }))
        // https://github.com/sequelize/sequelize/issues/7184
        .spread(affectedCount => affectedCount.should.equal(1));
    });
  });
}

{'fraction_non_alphanumeric': 0.1531214671523678, 'fraction_numerical': 0.008918477578193694, 'mean_word_length': 2.2287104622871046, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4765248', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2153, 'n_words': 546}