-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmigrateRelations.ts
More file actions
73 lines (70 loc) · 2.61 KB
/
migrateRelations.ts
File metadata and controls
73 lines (70 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import getRelationData from "./getRelationData";
import getBlockProps from "./getBlockProps";
import type { json } from "./getBlockProps";
import setBlockProps from "./setBlockProps";
import { getSetting, setSetting } from "./extensionSettings";
import { USE_REIFIED_RELATIONS } from "~/data/userSettings";
import {
createReifiedRelation,
DISCOURSE_GRAPH_PROP_NAME,
} from "./createReifiedBlock";
const MIGRATION_PROP_NAME = "relation-migration";
const migrateRelations = async (dryRun = false): Promise<number> => {
const authorized = getSetting<boolean>(USE_REIFIED_RELATIONS, false);
if (!authorized) return 0;
let numProcessed = 0;
await setSetting(USE_REIFIED_RELATIONS, false); // so queries use patterns
// wait for the settings to propagate
await new Promise((resolve) => setTimeout(resolve, 150));
try {
const processed = new Set<string>();
const relationData = await getRelationData(true);
for (const rel of relationData) {
const key = `${rel.source}:${rel.relUid}:${rel.target}`;
if (processed.has(key)) continue;
processed.add(key);
if (!dryRun) {
const uid = (await createReifiedRelation({
sourceUid: rel.source,
destinationUid: rel.target,
relationBlockUid: rel.relUid,
}))!;
const sourceProps = getBlockProps(rel.source);
const dgDataOrig = sourceProps[DISCOURSE_GRAPH_PROP_NAME];
const dgData: Record<string, json> =
dgDataOrig !== null &&
typeof dgDataOrig === "object" &&
!Array.isArray(dgDataOrig)
? dgDataOrig
: {};
const migrationDataOrig = dgData[MIGRATION_PROP_NAME];
let migrationData: Record<string, json> =
migrationDataOrig !== null &&
typeof migrationDataOrig === "object" &&
!Array.isArray(migrationDataOrig)
? migrationDataOrig
: {};
if (migrationData[uid] !== undefined) {
console.debug(`reprocessed ${key}`);
}
// clean up old migration entries
migrationData = Object.fromEntries(
Object.entries(migrationData).filter(
([uid]) =>
window.roamAlphaAPI.q(
`[:find ?p :where [?p :block/uid "${uid}"]]`,
).length > 0,
),
);
migrationData[uid] = new Date().valueOf();
dgData[MIGRATION_PROP_NAME] = migrationData;
setBlockProps(rel.source, { [DISCOURSE_GRAPH_PROP_NAME]: dgData });
}
numProcessed++;
}
} finally {
await setSetting(USE_REIFIED_RELATIONS, true);
}
return numProcessed;
};
export default migrateRelations;