Merge pull request #2190 from thelounge/xpaw/fix-2178

Close sqlite database when user quits
This commit is contained in:
Jérémie Astori 2018-03-10 17:34:26 -05:00 committed by GitHub
commit bd619220d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 7 deletions

View File

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

View File

@ -35,7 +35,7 @@ class MessageStorage {
this.isEnabled = true; this.isEnabled = true;
this.database = new sqlite3.cached.Database(sqlitePath); this.database = new sqlite3.Database(sqlitePath);
this.database.serialize(() => { this.database.serialize(() => {
schema.forEach((line) => this.database.run(line)); 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) { index(network, channel, msg) {
if (!this.isEnabled) { if (!this.isEnabled) {
return; 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(store.isEnabled).to.be.false;
expect(fs.existsSync(expectedPath)).to.be.false; expect(fs.existsSync(expectedPath)).to.be.false;
store.enable("testUser"); store.enable("testUser");
expect(store.isEnabled).to.be.true; expect(store.isEnabled).to.be.true;
store.database.serialize(() => {
expect(fs.existsSync(expectedPath)).to.be.true;
done();
});
}); });
it("should create tables", function(done) { it("should create tables", function(done) {
@ -103,4 +98,12 @@ describe("SQLite Message Storage", function() {
done(); done();
}); });
}); });
it("should close database", function(done) {
store.close((err) => {
expect(err).to.be.null;
expect(fs.existsSync(expectedPath)).to.be.true;
done();
});
});
}); });