connection-manager.test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. const chai = require('chai');
  3. const jetpack = require('fs-jetpack').cwd(__dirname);
  4. const expect = chai.expect;
  5. const Support = require('../../support');
  6. const dialect = Support.getTestDialect();
  7. const { DataTypes } = require('@sequelize/core');
  8. const fileName = `${Math.random()}_test.sqlite`;
  9. const directoryName = `${Math.random()}_test_directory`;
  10. const nestedFileName = jetpack.path(directoryName, 'subdirectory', 'test.sqlite');
  11. if (dialect === 'sqlite3') {
  12. describe('[SQLITE Specific] Connection Manager', () => {
  13. after(() => {
  14. jetpack.remove(fileName);
  15. jetpack.remove(directoryName);
  16. });
  17. it('close connection and remove journal and wal files', async function () {
  18. const sequelize = Support.createSingleTestSequelizeInstance({
  19. storage: jetpack.path(fileName),
  20. });
  21. const User = sequelize.define('User', { username: DataTypes.STRING });
  22. await User.sync({ force: true });
  23. await sequelize.query('PRAGMA journal_mode = WAL');
  24. await User.create({ username: 'user1' });
  25. await sequelize.transaction(transaction => {
  26. return User.create({ username: 'user2' }, { transaction });
  27. });
  28. expect(jetpack.exists(fileName)).to.equal('file');
  29. expect(jetpack.exists(`${fileName}-shm`), 'shm file should exists').to.equal('file');
  30. expect(jetpack.exists(`${fileName}-wal`), 'wal file should exists').to.equal('file');
  31. // move wal file content to main database
  32. // so those files can be removed on connection close
  33. // https://www.sqlite.org/wal.html#ckpt
  34. await sequelize.query('PRAGMA wal_checkpoint');
  35. // wal, shm files exist after checkpoint
  36. expect(jetpack.exists(`${fileName}-shm`), 'shm file should exists').to.equal('file');
  37. expect(jetpack.exists(`${fileName}-wal`), 'wal file should exists').to.equal('file');
  38. await sequelize.close();
  39. expect(jetpack.exists(fileName)).to.equal('file');
  40. expect(jetpack.exists(`${fileName}-shm`), 'shm file exists').to.be.false;
  41. expect(jetpack.exists(`${fileName}-wal`), 'wal file exists').to.be.false;
  42. await this.sequelize.query('PRAGMA journal_mode = DELETE');
  43. });
  44. it('automatic path provision for `options.storage`', async () => {
  45. const sequelize = await Support.createSingleTestSequelizeInstance({
  46. storage: nestedFileName,
  47. });
  48. await sequelize.define('User', { username: DataTypes.STRING }).sync({ force: true });
  49. expect(jetpack.exists(nestedFileName)).to.equal('file');
  50. await sequelize.close();
  51. });
  52. });
  53. }