checkdb: Use single transaction
This commit is contained in:
+20
-18
@@ -104,30 +104,32 @@ async function migratedb(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default async function checkdb(db: DataSource): Promise<void> {
|
export default async function checkdb(db: DataSource): Promise<void> {
|
||||||
const stmt = sql.select("schemaver").from("meta");
|
let schemaver: number | undefined;
|
||||||
let maybe: number | undefined;
|
|
||||||
|
|
||||||
try {
|
await db.transaction(async txn => {
|
||||||
const row = await db.transaction(txn => txn.fetchRow(stmt));
|
const stmt = sql.select("schemaver").from("meta");
|
||||||
|
|
||||||
if (row !== undefined) {
|
try {
|
||||||
maybe = parseInt(row.schemaver!);
|
const row = await txn.fetchRow(stmt);
|
||||||
|
|
||||||
|
if (row !== undefined) {
|
||||||
|
schemaver = parseInt(row.schemaver!);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return await initdb(txn);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
return db.transaction(initdb);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (maybe === undefined) {
|
if (schemaver === undefined) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Database corrupted: `meta` table singleton row is missing"
|
"Database corrupted: `meta` table singleton row is missing"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const schemaver = maybe;
|
schemaver = await migratedb(txn, schemaver);
|
||||||
const newver = await db.transaction(txn => migratedb(txn, schemaver));
|
});
|
||||||
|
|
||||||
if (newver !== undefined) {
|
if (schemaver !== undefined) {
|
||||||
debug("Upgraded database to version %s", newver);
|
debug("Upgraded database to version %s", schemaver);
|
||||||
|
|
||||||
await db.vacuum();
|
await db.vacuum();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user