Add CRDT backfill script
This commit is contained in:
74
server/scripts/20221008000000-backfill-crdt.ts
Normal file
74
server/scripts/20221008000000-backfill-crdt.ts
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import "./bootstrap";
|
||||||
|
import { updateYFragment } from "@getoutline/y-prosemirror";
|
||||||
|
import * as Y from "yjs";
|
||||||
|
import { parser } from "@server/editor";
|
||||||
|
import { Document } from "@server/models";
|
||||||
|
|
||||||
|
const limit = 100;
|
||||||
|
const page = 0;
|
||||||
|
const teamId = process.argv[2];
|
||||||
|
|
||||||
|
export default async function main(exit = false) {
|
||||||
|
const work = async (page: number): Promise<void> => {
|
||||||
|
console.log(`Backfill crdt… page ${page}`);
|
||||||
|
|
||||||
|
if (!teamId && process.env.DEPLOYMENT === "hosted") {
|
||||||
|
throw new Error("Team ID is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve all documents within set limit.
|
||||||
|
const documents = await Document.unscoped().findAll({
|
||||||
|
attributes: ["id", "urlId", "text", "state"],
|
||||||
|
limit,
|
||||||
|
offset: page * limit,
|
||||||
|
where: {
|
||||||
|
...(teamId ? { teamId } : {}),
|
||||||
|
state: null,
|
||||||
|
},
|
||||||
|
order: [["createdAt", "ASC"]],
|
||||||
|
paranoid: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(documents.length);
|
||||||
|
|
||||||
|
for (const document of documents) {
|
||||||
|
if (document.state || !document.text) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Writing CRDT for ${document.id}`);
|
||||||
|
|
||||||
|
const ydoc = new Y.Doc();
|
||||||
|
const type = ydoc.get("default", Y.XmlFragment) as Y.XmlFragment;
|
||||||
|
const doc = parser.parse(document.text);
|
||||||
|
|
||||||
|
if (!type.doc) {
|
||||||
|
throw new Error("type.doc not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
// apply new document to existing ydoc
|
||||||
|
updateYFragment(type.doc, type, doc, new Map());
|
||||||
|
|
||||||
|
const state = Y.encodeStateAsUpdate(ydoc);
|
||||||
|
document.state = Buffer.from(state);
|
||||||
|
|
||||||
|
await document.save({
|
||||||
|
hooks: false,
|
||||||
|
silent: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return documents.length === limit ? work(page + 1) : undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
await work(page);
|
||||||
|
|
||||||
|
if (exit) {
|
||||||
|
console.log("Backfill complete");
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== "test") {
|
||||||
|
main(true);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user