Skip to content

Commit

Permalink
Disable databases if initizialisation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Nachtalb committed May 22, 2021
1 parent 91ddee6 commit 43ffe52
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
15 changes: 11 additions & 4 deletions src/plugins/messageStorage/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,21 @@ class MessageStorage {
}

enable() {
if (!sqlite3) {
return false;
}

const logsPath = Helper.getUserLogsPath();
const sqlitePath = path.join(logsPath, `${this.client.name}.sqlite3`);

try {
fs.mkdirSync(logsPath, {recursive: true});
} catch (e) {
log.error("Unable to create logs directory", e);

return;
return false;
}

this.isEnabled = true;

const sqlitePath = path.join(logsPath, `${this.client.name}.sqlite3`);
this.database = new sqlite3.Database(sqlitePath);
this.database.serialize(() => {
schema.forEach((line) => this.database.run(line));
Expand All @@ -56,6 +58,7 @@ class MessageStorage {
"SELECT value FROM options WHERE name = 'schema_version'",
(err, row) => {
if (err) {
this.isEnabled = false;
return log.error(`Failed to retrieve schema version: ${err}`);
}

Expand All @@ -78,6 +81,7 @@ class MessageStorage {
}

if (storedSchemaVersion > currentSchemaVersion) {
this.isEnabled = false;
return log.error(
`sqlite messages schema version is higher than expected (${storedSchemaVersion} > ${currentSchemaVersion}). Is The Lounge out of date?`
);
Expand All @@ -96,6 +100,9 @@ class MessageStorage {
}
);
});

this.isEnabled = true;
return true;
}

close(callback) {
Expand Down
22 changes: 15 additions & 7 deletions src/plugins/messageStorage/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,22 @@ class MessageStorage {
constructor(client) {
this.client = client;
this.isEnabled = false;
this._clientFolder = null;
}

enable() {
this.isEnabled = true;
const clientFolder = path.join(Helper.getUserLogsPath(), this.client.name);

try {
fs.mkdirSync(clientFolder, {recursive: true});
this._clientFolder = clientFolder;
this.isEnabled = true;
return true;
} catch (e) {
log.error(`Unable to create client log directory at: "${clientFolder}"`, e);
return false;
}
}

close(callback) {
Expand Down Expand Up @@ -94,7 +106,7 @@ class MessageStorage {

fs.appendFile(logPath, line, (e) => {
if (e) {
log.error("Failed to write user log", e);
log.error(`Failed to write user log at: "${logPath}"`, e);
}
});
}
Expand All @@ -111,17 +123,13 @@ class MessageStorage {
}

_getLogPath(network, channel) {
const logFolder = path.join(
Helper.getUserLogsPath(),
this.client.name,
this._getNetworkFolderName(network)
);
const logFolder = path.join(this._clientFolder, this._getNetworkFolderName(network));

try {
fs.mkdirSync(logFolder, {recursive: true});
return path.join(logFolder, this._getChannelFileName(channel));
} catch (e) {
log.error("Unable to create logs directory", e);
log.error(`Unable to create network log directory at: "${logFolder}"`, e);
}
}

Expand Down

0 comments on commit 43ffe52

Please sign in to comment.