sqlite: use variadic function for serialize_run

This makes the usage of the function a bit nicer
This commit is contained in:
Reto Brunner 2023-09-23 13:27:54 +02:00
parent 20227b174c
commit 60ddf17124

View file

@ -149,12 +149,13 @@ class SqliteMessageStorage implements SearchableMessageStorage {
async setup_new_db() {
for (const stmt of schema) {
await this.serialize_run(stmt, []);
await this.serialize_run(stmt);
}
await this.serialize_run("INSERT INTO options (name, value) VALUES ('schema_version', ?)", [
currentSchemaVersion.toString(),
]);
await this.serialize_run(
"INSERT INTO options (name, value) VALUES ('schema_version', ?)",
currentSchemaVersion.toString()
);
}
async current_version(): Promise<number> {
@ -181,9 +182,10 @@ class SqliteMessageStorage implements SearchableMessageStorage {
}
async update_version_in_db() {
return this.serialize_run("UPDATE options SET value = ? WHERE name = 'schema_version'", [
currentSchemaVersion.toString(),
]);
return this.serialize_run(
"UPDATE options SET value = ? WHERE name = 'schema_version'",
currentSchemaVersion.toString()
);
}
async _run_migrations(dbVersion: number) {
@ -194,14 +196,14 @@ class SqliteMessageStorage implements SearchableMessageStorage {
const to_execute = necessaryMigrations(dbVersion);
for (const stmt of to_execute.map((m) => m.stmts).flat()) {
await this.serialize_run(stmt, []);
await this.serialize_run(stmt);
}
await this.update_version_in_db();
}
async run_pragmas() {
await this.serialize_run("PRAGMA foreign_keys = ON;", []);
await this.serialize_run("PRAGMA foreign_keys = ON;");
}
async run_migrations() {
@ -213,7 +215,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
return; // nothing to do
}
await this.serialize_run("BEGIN EXCLUSIVE TRANSACTION", []);
await this.serialize_run("BEGIN EXCLUSIVE TRANSACTION");
try {
if (version === 0) {
@ -224,12 +226,12 @@ class SqliteMessageStorage implements SearchableMessageStorage {
await this.insert_rollback_since(version);
} catch (err) {
await this.serialize_run("ROLLBACK", []);
await this.serialize_run("ROLLBACK");
throw err;
}
await this.serialize_run("COMMIT", []);
await this.serialize_run("VACUUM", []);
await this.serialize_run("COMMIT");
await this.serialize_run("VACUUM");
}
async close() {
@ -282,7 +284,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
}
async delete_migrations_older_than(version: number) {
return this.serialize_run("delete from migrations where migrations.version > ?", [version]);
return this.serialize_run("delete from migrations where migrations.version > ?", version);
}
async _downgrade_to(version: number) {
@ -300,7 +302,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
for (const rollback of _rollbacks) {
for (const stmt of rollback.stmts) {
await this.serialize_run(stmt, []);
await this.serialize_run(stmt);
}
}
@ -315,18 +317,18 @@ class SqliteMessageStorage implements SearchableMessageStorage {
throw Error(`${version} is not a valid version to downgrade to`);
}
await this.serialize_run("BEGIN EXCLUSIVE TRANSACTION", []);
await this.serialize_run("BEGIN EXCLUSIVE TRANSACTION");
let new_version: number;
try {
new_version = await this._downgrade_to(version);
} catch (err) {
await this.serialize_run("ROLLBACK", []);
await this.serialize_run("ROLLBACK");
throw err;
}
await this.serialize_run("COMMIT", []);
await this.serialize_run("COMMIT");
return new_version;
}
@ -354,7 +356,9 @@ class SqliteMessageStorage implements SearchableMessageStorage {
`insert into rollback_steps
(migration_id, step, statement)
values (?, ?, ?)`,
[migration.id, step, stmt]
migration.id,
step,
stmt
);
step++;
}
@ -381,13 +385,12 @@ class SqliteMessageStorage implements SearchableMessageStorage {
await this.serialize_run(
"INSERT INTO messages(network, channel, time, type, msg) VALUES(?, ?, ?, ?, ?)",
[
network.uuid,
channel.name.toLowerCase(),
msg.time.getTime(),
msg.type,
JSON.stringify(clonedMsg),
]
network.uuid,
channel.name.toLowerCase(),
msg.time.getTime(),
msg.type,
JSON.stringify(clonedMsg)
);
}
@ -398,10 +401,11 @@ class SqliteMessageStorage implements SearchableMessageStorage {
return;
}
await this.serialize_run("DELETE FROM messages WHERE network = ? AND channel = ?", [
await this.serialize_run(
"DELETE FROM messages WHERE network = ? AND channel = ?",
network.uuid,
channel.name.toLowerCase(),
]);
channel.name.toLowerCase()
);
}
async getMessages(
@ -481,7 +485,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
return this.isEnabled;
}
private serialize_run(stmt: string, params: any[]): Promise<void> {
private serialize_run(stmt: string, ...params: any[]): Promise<number> {
return new Promise((resolve, reject) => {
this.database.serialize(() => {
this.database.run(stmt, params, (err) => {