Use variadic function for serialize_run

This commit is contained in:
hgw 2024-02-02 02:39:30 +00:00
parent e862324de8
commit 191c57757a
Signed by: hgw
SSH Key Fingerprint: SHA256:diG7RVYHjd3aDYkZWHYcBJbImu+6zfptuUP+3k/wol4

View File

@ -159,12 +159,12 @@ 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( await this.serialize_run(
"INSERT INTO options (name, value) VALUES ('schema_version', ?)", "INSERT INTO options (name, value) VALUES ('schema_version', ?)",
[currentSchemaVersion.toString()] currentSchemaVersion.toString()
); );
} }
@ -194,7 +194,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
async update_version_in_db() { async update_version_in_db() {
return this.serialize_run( return this.serialize_run(
"UPDATE options SET value = ? WHERE name = 'schema_version'", "UPDATE options SET value = ? WHERE name = 'schema_version'",
[currentSchemaVersion.toString()] currentSchemaVersion.toString()
); );
} }
@ -206,14 +206,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() {
@ -225,7 +225,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) {
@ -236,12 +236,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() {
@ -296,7 +296,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
async delete_migrations_older_than(version: number) { async delete_migrations_older_than(version: number) {
return this.serialize_run( return this.serialize_run(
"delete from migrations where migrations.version > ?", "delete from migrations where migrations.version > ?",
[version] version
); );
} }
@ -315,7 +315,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);
} }
} }
@ -330,18 +330,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;
} }
@ -369,8 +369,10 @@ 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++;
} }
} }
@ -401,13 +403,11 @@ 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),
]
); );
} }
@ -420,7 +420,8 @@ class SqliteMessageStorage implements SearchableMessageStorage {
await this.serialize_run( await this.serialize_run(
"DELETE FROM messages WHERE network = ? AND channel = ?", "DELETE FROM messages WHERE network = ? AND channel = ?",
[network.uuid, channel.name.toLowerCase()] network.uuid,
channel.name.toLowerCase()
); );
} }
@ -502,7 +503,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) => {