Skip to content

Commit

Permalink
sqlite: accept db connection string
Browse files Browse the repository at this point in the history
This allows us to inject a memory db during testing
  • Loading branch information
brunnre8 committed Dec 23, 2023
1 parent 60ddf17 commit aec8d0b
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions server/plugins/messageStorage/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,32 +116,32 @@ class SqliteMessageStorage implements SearchableMessageStorage {
this.initDone = new Deferred();
}

async _enable() {
const logsPath = Config.getUserLogsPath();
const sqlitePath = path.join(logsPath, `${this.userName}.sqlite3`);
async _enable(connection_string: string) {
this.database = new sqlite3.Database(connection_string);

try {
await fs.mkdir(logsPath, {recursive: true});
await this.run_pragmas(); // must be done outside of a transaction
await this.run_migrations();
} catch (e) {
throw Helper.catch_to_error("Unable to create logs directory", e);
this.isEnabled = false;
throw Helper.catch_to_error("Migration failed", e);
}

this.isEnabled = true;
}

this.database = new sqlite3.Database(sqlitePath);
async enable() {
const logsPath = Config.getUserLogsPath();
const sqlitePath = path.join(logsPath, `${this.userName}.sqlite3`);

try {
await this.run_pragmas(); // must be done outside of a transaction
await this.run_migrations();
await fs.mkdir(logsPath, {recursive: true});
} catch (e) {
this.isEnabled = false;
throw Helper.catch_to_error("Migration failed", e);
throw Helper.catch_to_error("Unable to create logs directory", e);
}
}

async enable() {
try {
await this._enable();
await this._enable(sqlitePath);
} finally {
this.initDone.resolve(); // unblock the instance methods
}
Expand Down

0 comments on commit aec8d0b

Please sign in to comment.