sqlite: add run helper function

Extract the serialization logic into a single place and
consistently log errors to the console rather than a fire
and forget approach.
This commit is contained in:
Reto Brunner 2022-08-27 13:41:52 +02:00 committed by Reto Brunner
parent e62b169a6a
commit 89ee537364

View file

@ -63,7 +63,7 @@ class SqliteMessageStorage implements ISqliteMessageStorage {
this.database = new sqlite3.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.run(line));
this.database.get( this.database.get(
"SELECT value FROM options WHERE name = 'schema_version'", "SELECT value FROM options WHERE name = 'schema_version'",
@ -74,13 +74,10 @@ class SqliteMessageStorage implements ISqliteMessageStorage {
// New table // New table
if (row === undefined) { if (row === undefined) {
this.database.serialize(() => this.run(
this.database.run( "INSERT INTO options (name, value) VALUES ('schema_version', ?)",
"INSERT INTO options (name, value) VALUES ('schema_version', ?)", currentSchemaVersion
currentSchemaVersion
)
); );
return; return;
} }
@ -100,11 +97,9 @@ class SqliteMessageStorage implements ISqliteMessageStorage {
`sqlite messages schema version is out of date (${storedSchemaVersion} < ${currentSchemaVersion}). Running migrations if any.` `sqlite messages schema version is out of date (${storedSchemaVersion} < ${currentSchemaVersion}). Running migrations if any.`
); );
this.database.serialize(() => this.run(
this.database.run( "UPDATE options SET value = ? WHERE name = 'schema_version'",
"UPDATE options SET value = ? WHERE name = 'schema_version'", currentSchemaVersion
currentSchemaVersion
)
); );
} }
); );
@ -145,15 +140,13 @@ class SqliteMessageStorage implements ISqliteMessageStorage {
return newMsg; return newMsg;
}, {}); }, {});
this.database.serialize(() => this.run(
this.database.run( "INSERT INTO messages(network, channel, time, type, msg) VALUES(?, ?, ?, ?, ?)",
"INSERT INTO messages(network, channel, time, type, msg) VALUES(?, ?, ?, ?, ?)", network.uuid,
network.uuid, channel.name.toLowerCase(),
channel.name.toLowerCase(), msg.time.getTime(),
msg.time.getTime(), msg.type,
msg.type, JSON.stringify(clonedMsg)
JSON.stringify(clonedMsg)
)
); );
} }
@ -162,12 +155,10 @@ class SqliteMessageStorage implements ISqliteMessageStorage {
return; return;
} }
this.database.serialize(() => this.run(
this.database.run( "DELETE FROM messages WHERE network = ? AND channel = ?",
"DELETE FROM messages WHERE network = ? AND channel = ?", network.uuid,
network.uuid, channel.name.toLowerCase()
channel.name.toLowerCase()
)
); );
} }
@ -266,6 +257,28 @@ class SqliteMessageStorage implements ISqliteMessageStorage {
canProvideMessages() { canProvideMessages() {
return this.isEnabled; return this.isEnabled;
} }
private run(stmt: string, ...params: any[]) {
this.serialize_run(stmt, params).catch((err) =>
log.error(`failed to run ${stmt}`, String(err))
);
}
private serialize_run(stmt: string, params: any[]): Promise<void> {
return new Promise((resolve, reject) => {
this.database.serialize(() => {
this.database.run(stmt, params, (err) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
});
}
} }
// TODO: type any // TODO: type any