1
0
Fork 0
out-of-your-element-fork-th.../db/migrations/0002-optimise-profile-content.up.js

14 lines
651 B
JavaScript
Raw Permalink Normal View History

2023-09-29 04:41:11 +00:00
module.exports = async function(db) {
const hasher = await require("xxhash-wasm")()
2023-09-30 12:24:05 +00:00
const contents = db.prepare("SELECT distinct hashed_profile_content FROM sim_member WHERE hashed_profile_content IS NOT NULL").pluck().all()
2023-09-29 04:41:11 +00:00
const stmt = db.prepare("UPDATE sim_member SET hashed_profile_content = ? WHERE hashed_profile_content = ?")
db.transaction(() => {
2023-09-30 12:24:05 +00:00
for (let s of contents) {
let b = Buffer.isBuffer(s) ? Uint8Array.from(s) : Uint8Array.from(Buffer.from(s))
const unsignedHash = hasher.h64Raw(b)
2023-09-29 04:41:11 +00:00
const signedHash = unsignedHash - 0x8000000000000000n // shifting down to signed 64-bit range
2023-09-30 12:24:05 +00:00
stmt.run(signedHash, s)
2023-09-29 04:41:11 +00:00
}
})()
}