From a15e922e27d74b3dd3fd35edd0efb48392610fdf Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sat, 10 Mar 2018 18:49:16 +0200 Subject: [PATCH] Close sqlite database when user quits Fixes #2178 --- src/client.js | 4 ++++ src/plugins/sqlite.js | 18 +++++++++++++++++- test/plugins/sqlite.js | 15 +++++++++------ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/client.js b/src/client.js index 90a2e021..62dcdc05 100644 --- a/src/client.js +++ b/src/client.js @@ -537,6 +537,10 @@ Client.prototype.quit = function(signOut) { network.destroy(); }); + + if (this.messageStorage) { + this.messageStorage.close(); + } }; Client.prototype.clientAttach = function(socketId, token) { diff --git a/src/plugins/sqlite.js b/src/plugins/sqlite.js index 7480ed1d..afe930aa 100644 --- a/src/plugins/sqlite.js +++ b/src/plugins/sqlite.js @@ -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; diff --git a/test/plugins/sqlite.js b/test/plugins/sqlite.js index 968cd46f..9a79dc6c 100644 --- a/test/plugins/sqlite.js +++ b/test/plugins/sqlite.js @@ -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(); + }); + }); });