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() { async setup_new_db() {
for (const stmt of schema) { 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', ?)", [ await this.serialize_run(
currentSchemaVersion.toString(), "INSERT INTO options (name, value) VALUES ('schema_version', ?)",
]); currentSchemaVersion.toString()
);
} }
async current_version(): Promise<number> { async current_version(): Promise<number> {
@ -181,9 +182,10 @@ class SqliteMessageStorage implements SearchableMessageStorage {
} }
async update_version_in_db() { async update_version_in_db() {
return this.serialize_run("UPDATE options SET value = ? WHERE name = 'schema_version'", [ return this.serialize_run(
currentSchemaVersion.toString(), "UPDATE options SET value = ? WHERE name = 'schema_version'",
]); currentSchemaVersion.toString()
);
} }
async _run_migrations(dbVersion: number) { async _run_migrations(dbVersion: number) {
@ -194,14 +196,14 @@ class SqliteMessageStorage implements SearchableMessageStorage {
const to_execute = necessaryMigrations(dbVersion); const to_execute = necessaryMigrations(dbVersion);
for (const stmt of to_execute.map((m) => m.stmts).flat()) { 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(); await this.update_version_in_db();
} }
async run_pragmas() { async run_pragmas() {
await this.serialize_run("PRAGMA foreign_keys = ON;", []); await this.serialize_run("PRAGMA foreign_keys = ON;");
} }
async run_migrations() { async run_migrations() {
@ -213,7 +215,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
return; // nothing to do return; // nothing to do
} }
await this.serialize_run("BEGIN EXCLUSIVE TRANSACTION", []); await this.serialize_run("BEGIN EXCLUSIVE TRANSACTION");
try { try {
if (version === 0) { if (version === 0) {
@ -224,12 +226,12 @@ class SqliteMessageStorage implements SearchableMessageStorage {
await this.insert_rollback_since(version); await this.insert_rollback_since(version);
} catch (err) { } catch (err) {
await this.serialize_run("ROLLBACK", []); await this.serialize_run("ROLLBACK");
throw err; throw err;
} }
await this.serialize_run("COMMIT", []); await this.serialize_run("COMMIT");
await this.serialize_run("VACUUM", []); await this.serialize_run("VACUUM");
} }
async close() { async close() {
@ -282,7 +284,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
} }
async delete_migrations_older_than(version: number) { 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) { async _downgrade_to(version: number) {
@ -300,7 +302,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
for (const rollback of _rollbacks) { for (const rollback of _rollbacks) {
for (const stmt of rollback.stmts) { 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`); 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; let new_version: number;
try { try {
new_version = await this._downgrade_to(version); new_version = await this._downgrade_to(version);
} catch (err) { } catch (err) {
await this.serialize_run("ROLLBACK", []); await this.serialize_run("ROLLBACK");
throw err; throw err;
} }
await this.serialize_run("COMMIT", []); await this.serialize_run("COMMIT");
return new_version; return new_version;
} }
@ -354,7 +356,9 @@ class SqliteMessageStorage implements SearchableMessageStorage {
`insert into rollback_steps `insert into rollback_steps
(migration_id, step, statement) (migration_id, step, statement)
values (?, ?, ?)`, values (?, ?, ?)`,
[migration.id, step, stmt] migration.id,
step,
stmt
); );
step++; step++;
} }
@ -381,13 +385,12 @@ class SqliteMessageStorage implements SearchableMessageStorage {
await this.serialize_run( await this.serialize_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)
]
); );
} }
@ -398,10 +401,11 @@ class SqliteMessageStorage implements SearchableMessageStorage {
return; 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, network.uuid,
channel.name.toLowerCase(), channel.name.toLowerCase()
]); );
} }
async getMessages( async getMessages(
@ -481,7 +485,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
return this.isEnabled; 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) => { return new Promise((resolve, reject) => {
this.database.serialize(() => { this.database.serialize(() => {
this.database.run(stmt, params, (err) => { this.database.run(stmt, params, (err) => {