fp/services/pocketbase/utils/data_migrations/2025-11-05-populate-vtuber-field.js
CJ_Clippy 6caf2dbcc3
Some checks failed
ci / test (push) Failing after 9m42s
fp/our CI/CD / build (push) Successful in 1m19s
add /vods and /vt/:slug/vods
2025-11-08 12:36:26 -08:00

65 lines
2.0 KiB
JavaScript

/**
* Migration Script: Assign missing VOD vtubers to "projektmelody"
* ---------------------------------------------------------------
* This script connects to a PocketBase instance and updates all VOD records
* that do not have a `vtuber` set, assigning them to the vtuber with slug "projektmelody".
*
* Environment variables:
* PB_URL Base URL of your PocketBase instance (e.g. "http://127.0.0.1:8090")
* PB_ADMIN_EMAIL PocketBase admin email
* PB_ADMIN_PASS PocketBase admin password
*
* Usage:
* $ npx @dotenvx/dotenvx run -f .env.local -- node ./2025-11-05-fix-vod-vtuber.js
*/
import PocketBase from 'pocketbase';
const pb = new PocketBase(process.env.PB_URL || 'http://127.0.0.1:8090');
async function main() {
console.log('Authenticating with PocketBase...');
await pb
.collection("_superusers")
.authWithPassword(process.env.PB_USERNAME, process.env.PB_PASSWORD);
console.log('Fetching vtuber "projektmelody"...');
const projekt = await pb.collection('vtubers').getFirstListItem('slug="projektmelody"');
if (!projekt) {
throw new Error('Could not find vtuber with slug "projektelody"');
}
console.log('Fetching VODs...');
let page = 1;
const perPage = 50;
let updatedCount = 0;
while (true) {
const vods = await pb.collection('vods').getList(page, perPage);
if (vods.items.length === 0) break;
for (const vod of vods.items) {
// Only update if vtuber is missing or empty
if (!vod.vtubers || vod.vtubers.length === 0) {
await pb.collection('vods').update(vod.id, {
vtubers: [projekt.id],
});
console.log(`✅ Updated VOD ${vod.id} → projektmelody`);
updatedCount++;
}
}
if (vods.items.length < perPage) break;
page++;
}
console.log(`Done! Updated ${updatedCount} VODs that had no vtuber set.`);
pb.authStore.clear();
}
main().catch((err) => {
console.error('Migration failed:', err);
process.exit(1);
});