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