WFDASH-WEBAPP/scripts/migrations/2026-10-28-add-collection-auto-push.ts

47 lines
2 KiB
TypeScript
Raw Permalink Normal View History

/**
* One-shot migration: Collection Confirm auto-push.
*
* ai_settings.collection_auto_push_enabled bool (off by default)
* ai_settings.collection_auto_push_log json (what was pushed, last 2 days)
*
* Run with: npm run pb:migrate:collection-auto-push
*
* Idempotent. Manual step on the live PB, before deploying. Until it has
* run the app never auto-pushes (it needs the log to record what it did).
*/
import "./_env-loader";
import PocketBase from "pocketbase";
const PB_URL = process.env.NEXT_PUBLIC_POCKETBASE_URL || process.env.PB_URL || "http://127.0.0.1:8090";
const ADMIN_EMAIL = process.env.PB_ADMIN_EMAIL;
const ADMIN_PASSWORD = process.env.PB_ADMIN_PASSWORD;
const TAG = "[collection-auto-push]";
const FIELDS: Array<Record<string, unknown> & { name: string }> = [
{ name: "collection_auto_push_enabled", type: "bool", required: false, presentable: false },
{ name: "collection_auto_push_log", type: "json", required: false, presentable: false, maxSize: 200000 },
];
async function main() {
if (!ADMIN_EMAIL || !ADMIN_PASSWORD) {
throw new Error("Set PB_ADMIN_EMAIL and PB_ADMIN_PASSWORD (e.g. from .env.local).");
}
const pb = new PocketBase(PB_URL);
await pb.collection("_superusers").authWithPassword(ADMIN_EMAIL, ADMIN_PASSWORD);
console.log(`${TAG} Authenticated against ${PB_URL}`);
const collection = await pb.collections.getOne("ai_settings");
const existing = new Set(collection.fields.map((f: { name: string }) => f.name));
const missing = FIELDS.filter((f) => !existing.has(f.name));
for (const f of FIELDS) if (existing.has(f.name)) console.log(`${TAG} ai_settings.${f.name} already exists — skipping.`);
if (missing.length > 0) {
await pb.collections.update(collection.id, { fields: [...collection.fields, ...missing] });
for (const f of missing) console.log(`${TAG} ai_settings.${f.name} added.`);
}
console.log(`${TAG} Done.`);
}
main().catch((err) => {
console.error(`${TAG} Migration failed:`, err);
process.exit(1);
});