1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-11-22 08:07:30 +00:00

Improve first run upgrade experience

This commit is contained in:
Cadence Ember 2020-05-21 23:12:46 +12:00
parent a56bf3b6c3
commit 49575dc8a1
No known key found for this signature in database
GPG Key ID: 128B99B1B74A6412

View File

@ -97,35 +97,89 @@ const deltas = new Map([
}] }]
]) ])
function writeProgress(i) {
const size = deltas.size
const progress = "=".repeat(i) + " ".repeat(deltas.size-i)
const numberLength = String(deltas.size).length
process.stdout.cursorTo(0)
process.stdout.write(
`Creating database... (${String(i).padStart(numberLength, " ")}`
+`/${String(size).padStart(numberLength, " ")}) [${progress}]`
)
}
async function createBackup(entry) {
const filename = `backups/bibliogram.db.bak-v${entry-1}`
process.stdout.write(`Backing up current to ${filename}... `)
await db.backup(pj(__dirname, "../../../db", filename))
process.stdout.write("done.\n")
}
/**
* @param {number} entry
* @param {boolean} log
*/
function runDelta(entry, log) {
if (log) process.stdout.write(`Using script ${entry}... `)
deltas.get(entry)()
db.prepare("DELETE FROM DatabaseVersion").run()
db.prepare("INSERT INTO DatabaseVersion (version) VALUES (?)").run(entry)
if (log) process.stdout.write("done.\n")
}
module.exports = async function() { module.exports = async function() {
let currentVersion = 0 let currentVersion = 0
let upgradeType = "stages" // "stages", "progress", whether to execute each stage at a time or show a progress bar and run all
try { try {
currentVersion = db.prepare("SELECT version FROM DatabaseVersion").pluck().get() || 0 currentVersion = db.prepare("SELECT version FROM DatabaseVersion").pluck().get() || 0
} catch (e) {} } catch (e) {}
if (currentVersion === 0) {
upgradeType = "progress"
console.log(
"Welcome to Bibliogram! Thank you for installing."
+"\n"
+"\n -> Make sure you have set `config/website_origin`"
+"\n as instructed in the installation guide."
+"\n -> Consider adding yourself to the instance list:"
+"\n https://github.com/cloudrac3r/bibliogram/wiki/Instances"
+"\n -> Join the Matrix chatroom for help: #bibliogram:matrix.org"
+"\n"
)
writeProgress(0)
await new Promise(resolve => setTimeout(resolve, 300))
}
const newVersion = constants.database_version const newVersion = constants.database_version
if (currentVersion !== newVersion) { if (currentVersion !== newVersion) {
console.log(`Upgrading database from version ${currentVersion} to version ${newVersion}...`) if (upgradeType === "stages") {
// go through the entire upgrade path console.log(`Upgrading database from version ${currentVersion} to version ${newVersion}...`)
}
// go through the entire upgrade sequence
for (let entry = currentVersion+1; entry <= newVersion; entry++) { for (let entry = currentVersion+1; entry <= newVersion; entry++) {
// Back up current version // Back up current version
if (entry !== 1) { if (upgradeType === "stages" && entry !== 1) {
const filename = `backups/bibliogram.db.bak-v${entry-1}` await createBackup(entry)
process.stdout.write(`Backing up current to ${filename}... `)
await db.backup(pj(__dirname, "../../../db", filename))
process.stdout.write("done.\n")
} }
// Run delta // Run delta
process.stdout.write(`Using script ${entry}... `) runDelta(entry, upgradeType === "stages")
deltas.get(entry)()
db.prepare("DELETE FROM DatabaseVersion").run() if (upgradeType === "progress") {
db.prepare("INSERT INTO DatabaseVersion (version) VALUES (?)").run(entry) writeProgress(entry)
process.stdout.write("done.\n") }
}
if (upgradeType === "stages") {
console.log(
"Upgrade complete."
+"\n-----------------"
)
} else {
process.stdout.write(" done.\n\n")
await new Promise(resolve => setTimeout(resolve, 300))
} }
console.log(
"Upgrade complete."
+"\n-----------------"
)
} }
} }