-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmigrateLegacyToBlockProps.ts
More file actions
314 lines (287 loc) · 8.51 KB
/
migrateLegacyToBlockProps.ts
File metadata and controls
314 lines (287 loc) · 8.51 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import getBlockProps from "~/utils/getBlockProps";
import type { json } from "~/utils/getBlockProps";
import setBlockProps from "~/utils/setBlockProps";
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
import { createBlock } from "roamjs-components/writes";
import { getSetting, setSetting } from "~/utils/extensionSettings";
import internalError from "~/utils/internalError";
import {
readAllLegacyFeatureFlags,
readAllLegacyGlobalSettings,
readAllLegacyPersonalSettings,
readAllLegacyDiscourseNodeSettings,
} from "./accessors";
import {
FeatureFlagsSchema,
GlobalSettingsSchema,
PersonalSettingsSchema,
DiscourseNodeSchema,
DG_BLOCK_PROP_SETTINGS_PAGE_TITLE,
DISCOURSE_NODE_PAGE_PREFIX,
TOP_LEVEL_BLOCK_PROP_KEYS,
getPersonalSettingsKey,
} from "./zodSchema";
import type { z } from "zod";
const LOG_PREFIX = "[DG BlockProps Migration]";
const GRAPH_MIGRATION_MARKER = "Block props migrated";
const PERSONAL_MIGRATION_MARKER = "dg-personal-settings-migrated";
const MAX_ERROR_CONTEXT_LENGTH = 5000;
const hasGraphMigrationMarker = (blockMap: Record<string, string>): boolean =>
!!blockMap[GRAPH_MIGRATION_MARKER];
const isPropsValid = (
schema: z.ZodTypeAny,
props: Record<string, json> | null,
): boolean =>
!!props && Object.keys(props).length > 0 && schema.safeParse(props).success;
const serializeErrorContext = (value: unknown): string => {
try {
return JSON.stringify(value).slice(0, MAX_ERROR_CONTEXT_LENGTH);
} catch {
return String(value);
}
};
const shouldWrite = (
schema: z.ZodTypeAny,
currentProps: Record<string, json> | null,
parsedLegacy: Record<string, json>,
): boolean => {
if (!isPropsValid(schema, currentProps)) {
return true;
}
return JSON.stringify(parsedLegacy) !== JSON.stringify(currentProps);
};
const migrateSection = ({
label,
blockUid,
schema,
legacyData,
}: {
label: string;
blockUid: string;
schema: z.ZodTypeAny;
legacyData: Record<string, unknown>;
}): boolean => {
const currentProps = getBlockProps(blockUid);
const parseResult = schema.safeParse(legacyData);
if (!parseResult.success) {
if (isPropsValid(schema, currentProps)) {
console.log(
`${LOG_PREFIX} ${label}: legacy malformed but props already valid, skipping`,
);
return true;
}
console.warn(`${LOG_PREFIX} ${label}: Zod validation failed, skipping`, {
error: parseResult.error.message,
});
internalError({
error: parseResult.error,
type: "DG Block Props Migration",
context: {
label,
blockUid,
legacyData: serializeErrorContext(legacyData),
currentProps: serializeErrorContext(currentProps),
},
sendEmail: false,
});
return false;
}
const parsedLegacy = parseResult.data as Record<string, json>;
if (!shouldWrite(schema, currentProps, parsedLegacy)) {
console.log(`${LOG_PREFIX} ${label}: props already non-default, skipping`);
return true;
}
setBlockProps(blockUid, parsedLegacy, false);
console.log(`${LOG_PREFIX} ${label}: migrated`);
return true;
};
const migrateDiscourseNodes = async (): Promise<boolean> => {
const nodePages = (await window.roamAlphaAPI.data.async.fast.q(`
[:find ?uid ?title
:where
[?page :node/title ?title]
[?page :block/uid ?uid]
[(clojure.string/starts-with? ?title "${DISCOURSE_NODE_PAGE_PREFIX}")]]
`)) as [string, string][];
let allOk = true;
for (const [nodePageUid, title] of nodePages) {
if (typeof nodePageUid !== "string" || typeof title !== "string") continue;
const nodeText = title.replace(DISCOURSE_NODE_PAGE_PREFIX, "");
const legacyData = readAllLegacyDiscourseNodeSettings(
nodePageUid,
nodeText,
);
if (!legacyData) {
if (isPropsValid(DiscourseNodeSchema, getBlockProps(nodePageUid))) {
console.log(
`${LOG_PREFIX} Discourse Node (${nodeText}): legacy unreadable but props already valid, skipping`,
);
continue;
}
console.warn(
`${LOG_PREFIX} Discourse Node (${nodeText}): legacy data unreadable`,
);
internalError({
error: `Legacy discourse node data unreadable: ${nodeText}`,
type: "DG Block Props Migration",
context: {
label: `Discourse Node (${nodeText})`,
blockUid: nodePageUid,
currentProps: serializeErrorContext(getBlockProps(nodePageUid)),
},
sendEmail: false,
});
allOk = false;
continue;
}
if (
!migrateSection({
label: `Discourse Node (${nodeText})`,
blockUid: nodePageUid,
schema: DiscourseNodeSchema,
legacyData,
})
) {
allOk = false;
}
}
return allOk;
};
export const migrateGraphLevel = async (
blockUids: Record<string, string>,
): Promise<void> => {
const pageUid = getPageUidByPageTitle(DG_BLOCK_PROP_SETTINGS_PAGE_TITLE);
if (!pageUid) {
internalError({
error: `Settings page not found: ${DG_BLOCK_PROP_SETTINGS_PAGE_TITLE}`,
type: "DG Block Props Migration",
context: { scope: "graph" },
sendEmail: false,
});
return;
}
if (hasGraphMigrationMarker(blockUids)) {
console.log(`${LOG_PREFIX} graph-level: skipped (already migrated)`);
return;
}
let failures = 0;
const featureFlagUid = blockUids[TOP_LEVEL_BLOCK_PROP_KEYS.featureFlags];
if (!featureFlagUid) {
internalError({
error: `Missing block uid for ${TOP_LEVEL_BLOCK_PROP_KEYS.featureFlags}`,
type: "DG Block Props Migration",
context: {
scope: "graph",
blockUids: serializeErrorContext(blockUids),
},
sendEmail: false,
});
failures++;
} else {
const legacyFlags = readAllLegacyFeatureFlags();
if (
!migrateSection({
label: "Feature Flags",
blockUid: featureFlagUid,
schema: FeatureFlagsSchema,
legacyData: legacyFlags as Record<string, unknown>,
})
) {
failures++;
}
}
const globalUid = blockUids[TOP_LEVEL_BLOCK_PROP_KEYS.global];
if (!globalUid) {
internalError({
error: `Missing block uid for ${TOP_LEVEL_BLOCK_PROP_KEYS.global}`,
type: "DG Block Props Migration",
context: {
scope: "graph",
blockUids: serializeErrorContext(blockUids),
},
sendEmail: false,
});
failures++;
} else {
const legacyGlobal = readAllLegacyGlobalSettings();
if (
!migrateSection({
label: "Global",
blockUid: globalUid,
schema: GlobalSettingsSchema,
legacyData: legacyGlobal,
})
) {
failures++;
}
}
if (!(await migrateDiscourseNodes())) {
failures++;
}
if (failures === 0) {
try {
await createBlock({
parentUid: pageUid,
node: { text: GRAPH_MIGRATION_MARKER },
});
console.log(`${LOG_PREFIX} graph-level: completed`);
} catch (e) {
console.warn(
`${LOG_PREFIX} graph-level: data migrated but marker write failed (will retry next load)`,
e,
);
}
} else {
console.warn(
`${LOG_PREFIX} graph-level: ${failures} section(s) failed, marker not created (will retry next load)`,
);
}
};
export const migratePersonalSettings = async (
blockUids: Record<string, string>,
): Promise<void> => {
if (getSetting<boolean>(PERSONAL_MIGRATION_MARKER, false)) {
console.log(`${LOG_PREFIX} personal: skipped (already migrated)`);
return;
}
const personalKey = getPersonalSettingsKey();
const personalUid = blockUids[personalKey];
if (!personalUid) {
console.warn(
`${LOG_PREFIX} personal: block not found for key "${personalKey}", skipping`,
);
internalError({
error: `Missing personal settings block for key "${personalKey}"`,
type: "DG Block Props Migration",
context: {
scope: "personal",
personalKey,
blockUids: serializeErrorContext(blockUids),
},
sendEmail: false,
});
return;
}
const legacyPersonal = readAllLegacyPersonalSettings();
const ok = migrateSection({
label: "Personal",
blockUid: personalUid,
schema: PersonalSettingsSchema,
legacyData: legacyPersonal,
});
if (ok) {
try {
await setSetting(PERSONAL_MIGRATION_MARKER, true);
console.log(`${LOG_PREFIX} personal: completed`);
} catch (e) {
console.warn(
`${LOG_PREFIX} personal: data migrated but marker write failed (will retry next load)`,
e,
);
}
} else {
console.warn(
`${LOG_PREFIX} personal: failed, marker not created (will retry next load)`,
);
}
};