sqlite: convert migrations to async
This removes quite a bunch of indention and callbacks
This commit is contained in:
parent
bbe81bb2fa
commit
f068fd4290
@ -63,52 +63,47 @@ class SqliteMessageStorage implements ISqliteMessageStorage {
|
||||
|
||||
this.database = new sqlite3.Database(sqlitePath);
|
||||
|
||||
this.run_migrations()
|
||||
this.run_migrations().catch((err) => {
|
||||
log.error("Migration failed", String(err));
|
||||
this.isEnabled = false;
|
||||
});
|
||||
}
|
||||
|
||||
private run_migrations() {
|
||||
this.database.serialize(() => {
|
||||
schema.forEach((line) => this.run(line));
|
||||
|
||||
this.database.get(
|
||||
"SELECT value FROM options WHERE name = 'schema_version'",
|
||||
(err, row) => {
|
||||
if (err) {
|
||||
return log.error(`Failed to retrieve schema version: ${err.toString()}`);
|
||||
async run_migrations() {
|
||||
for (const stmt of schema) {
|
||||
await this.serialize_run(stmt, []);
|
||||
}
|
||||
|
||||
// New table
|
||||
if (row === undefined) {
|
||||
this.run(
|
||||
const version = await this.serialize_get(
|
||||
"SELECT value FROM options WHERE name = 'schema_version'"
|
||||
);
|
||||
|
||||
if (version === undefined) {
|
||||
// new table
|
||||
await this.serialize_run(
|
||||
"INSERT INTO options (name, value) VALUES ('schema_version', ?)",
|
||||
currentSchemaVersion
|
||||
[currentSchemaVersion]
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const storedSchemaVersion = parseInt(row.value, 10);
|
||||
const storedSchemaVersion = parseInt(version.value, 10);
|
||||
|
||||
if (storedSchemaVersion === currentSchemaVersion) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (storedSchemaVersion > currentSchemaVersion) {
|
||||
return log.error(
|
||||
`sqlite messages schema version is higher than expected (${storedSchemaVersion} > ${currentSchemaVersion}). Is The Lounge out of date?`
|
||||
);
|
||||
throw `sqlite messages schema version is higher than expected (${storedSchemaVersion} > ${currentSchemaVersion}). Is The Lounge out of date?`;
|
||||
}
|
||||
|
||||
log.info(
|
||||
`sqlite messages schema version is out of date (${storedSchemaVersion} < ${currentSchemaVersion}). Running migrations if any.`
|
||||
);
|
||||
|
||||
this.run(
|
||||
"UPDATE options SET value = ? WHERE name = 'schema_version'",
|
||||
currentSchemaVersion
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
await this.serialize_run("UPDATE options SET value = ? WHERE name = 'schema_version'", [
|
||||
currentSchemaVersion,
|
||||
]);
|
||||
}
|
||||
|
||||
close(callback?: (error?: Error | null) => void) {
|
||||
|
Loading…
Reference in New Issue
Block a user