1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-11-16 21:27:30 +00:00
bibliogram/src/lib/utils/upgradedb.js

112 lines
4.1 KiB
JavaScript
Raw Normal View History

2020-02-01 04:44:40 +00:00
const constants = require("../constants")
const pj = require("path").join
const db = require("../db")
require("../testimports")(db)
const deltas = new Map([
// empty file to version 1
[1, function() {
db.prepare("DROP TABLE IF EXISTS Users")
.run()
db.prepare("DROP TABLE IF EXISTS DatabaseVersion")
.run()
db.prepare("DROP TABLE IF Exists Posts")
.run()
db.prepare("CREATE TABLE Users (username TEXT NOT NULL UNIQUE, user_id TEXT NOT NULL UNIQUE, PRIMARY KEY (username))")
.run()
db.prepare("CREATE TABLE DatabaseVersion (version INTEGER NOT NULL UNIQUE, PRIMARY KEY (version))")
.run()
db.prepare("CREATE TABLE Posts (shortcode TEXT NOT NULL UNIQUE, id TEXT NOT NULL UNIQUE, id_as_numeric NUMERIC NOT NULL, username TEXT NOT NULL, json TEXT NOT NULL, PRIMARY KEY (shortcode))")
// for future investigation: may not be able to sort by id as a string, may not be able to fit entire id in numeric type
.run()
}],
// version 1 to version 2
[2, function() {
db.transaction(() => {
db.prepare(
"CREATE TABLE Users_New (username TEXT NOT NULL UNIQUE, user_id TEXT NOT NULL UNIQUE, created INTEGER NOT NULL, updated INTEGER NOT NULL"
+", updated_version INTEGER NOT NULL, biography TEXT, post_count INTEGER NOT NULL, following_count INTEGER NOT NULL, followed_by_count INTEGER NOT NULL, external_url TEXT"
+", full_name TEXT, is_private INTEGER NOT NULL, is_verified INTEGER NOT NULL, profile_pic_url TEXT NOT NULL"
+", PRIMARY KEY (username))"
)
.run()
db.prepare("INSERT INTO Users_New SELECT username, user_id, 0, 0, 1, NULL, 0, 0, 0, NULL, NULL, 0, 0, '' from Users")
.run()
db.prepare("DROP TABLE Users")
.run()
db.prepare("ALTER TABLE Users_New RENAME TO Users")
.run()
})()
}],
// version 2 to version 3
[3, function() {
db.transaction(() => {
db.prepare("DROP TABLE IF EXISTS RequestHistory")
.run()
db.prepare("CREATE TABLE RequestHistory (type TEXT NOT NULL, success INTEGER NOT NULL, timestamp INTEGER NOT NULL)")
.run()
})()
2020-05-04 15:06:07 +00:00
}],
// version 3 to version 4
[4, function() {
db.transaction(() => {
db.prepare("DROP TABLE IF EXISTS UserSettings")
.run()
db.prepare(
"CREATE TABLE UserSettings (token TEXT NOT NULL, created INTEGER NOT NULL, language TEXT NOT NULL, show_comments INTEGER NOT NULL, link_hashtags INTEGER NOT NULL"
+", spa INTEGER NOT NULL, theme TEXT NOT NULL, caption_side TEXT NOT NULL, display_alt INTEGER NOT NULL"
+", PRIMARY KEY (token))"
)
.run()
})()
2020-05-05 15:35:18 +00:00
}],
// version 4 to version 5
[5, function() {
db.transaction(() => {
// the previous version wasn't around for long enough for me to care about the contents
db.prepare("DROP TABLE IF EXISTS UserSettings")
.run()
db.prepare(
"CREATE TABLE UserSettings (token TEXT NOT NULL, created INTEGER NOT NULL, language TEXT NOT NULL, show_comments INTEGER NOT NULL, link_hashtags INTEGER NOT NULL"
+", spa INTEGER NOT NULL, theme TEXT NOT NULL, caption_side TEXT NOT NULL, display_alt INTEGER NOT NULL, timeline_columns TEXT NOT NULL, display_top_nav INTEGER NOT NULL"
+", PRIMARY KEY (token))"
)
.run()
})()
}],
2020-02-01 04:44:40 +00:00
])
module.exports = async function() {
let currentVersion = 0
try {
currentVersion = db.prepare("SELECT version FROM DatabaseVersion").pluck().get() || 0
} catch (e) {}
const newVersion = constants.database_version
if (currentVersion !== newVersion) {
console.log(`Upgrading database from version ${currentVersion} to version ${newVersion}...`)
// go through the entire upgrade path
for (let entry = currentVersion+1; entry <= newVersion; entry++) {
// Back up current version
if (entry !== 1) {
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")
}
// Run delta
process.stdout.write(`Using script ${entry}... `)
deltas.get(entry)()
db.prepare("DELETE FROM DatabaseVersion").run()
db.prepare("INSERT INTO DatabaseVersion (version) VALUES (?)").run(entry)
process.stdout.write("done.\n")
}
console.log(
"Upgrade complete."
+"\n-----------------"
)
}
}