Close sqlite database when user quits

Fixes #2178
This commit is contained in:
Pavel Djundik 2018-03-10 18:49:16 +02:00
parent 6857c5dd08
commit a15e922e27
3 changed files with 30 additions and 7 deletions

View File

@ -537,6 +537,10 @@ Client.prototype.quit = function(signOut) {
network.destroy();
});
if (this.messageStorage) {
this.messageStorage.close();
}
};
Client.prototype.clientAttach = function(socketId, token) {

View File

@ -35,7 +35,7 @@ class MessageStorage {
this.isEnabled = true;
this.database = new sqlite3.cached.Database(sqlitePath);
this.database = new sqlite3.Database(sqlitePath);
this.database.serialize(() => {
schema.forEach((line) => this.database.run(line));
@ -68,6 +68,22 @@ class MessageStorage {
});
}
close(callback) {
if (!this.isEnabled) {
return;
}
this.database.close((err) => {
if (err) {
log.error(`Failed to close sqlite database: ${err}`);
}
if (callback) {
callback(err);
}
});
}
index(network, channel, msg) {
if (!this.isEnabled) {
return;

View File

@ -29,18 +29,13 @@ describe("SQLite Message Storage", function() {
});
});
it("should create database file", function(done) {
it("should create database file", function() {
expect(store.isEnabled).to.be.false;
expect(fs.existsSync(expectedPath)).to.be.false;
store.enable("testUser");
expect(store.isEnabled).to.be.true;
store.database.serialize(() => {
expect(fs.existsSync(expectedPath)).to.be.true;
done();
});
});
it("should create tables", function(done) {
@ -103,4 +98,12 @@ describe("SQLite Message Storage", function() {
done();
});
});
it("should close database", function(done) {
store.close((err) => {
expect(err).to.be.null;
expect(fs.existsSync(expectedPath)).to.be.true;
done();
});
});
});