Skip to content

Commit e9f241e

Browse files
authored
ENG-1156 use the pattern relation queries in migration (#611)
* eng-1156 use the pattern relation queries in migration * add timeout * remove setEnabled * spurious eslint ignore
1 parent efeab5a commit e9f241e

File tree

8 files changed

+86
-76
lines changed

8 files changed

+86
-76
lines changed

apps/roam/src/components/Export.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ const ExportDialog: ExportDialogComponent = ({
199199
if (response.status === 401) {
200200
setGitHubAccessToken(null);
201201
setError("Authentication failed. Please log in again.");
202-
setSetting("oauth-github", "");
202+
await setSetting("oauth-github", "");
203203
return { status: 401 };
204204
}
205205
return { status: response.status };
@@ -222,7 +222,7 @@ const ExportDialog: ExportDialogComponent = ({
222222
const addToSelectedCanvas = async (pageUid: string) => {
223223
if (typeof results !== "object") return;
224224

225-
let props: Record<string, unknown> = getBlockProps(pageUid);
225+
const props: Record<string, unknown> = getBlockProps(pageUid);
226226

227227
const PADDING_BETWEEN_SHAPES = 20;
228228
const COMMON_BOUNDS_XOFFSET = 250;
@@ -309,10 +309,10 @@ const ExportDialog: ExportDialogComponent = ({
309309
let minY = Number.MAX_SAFE_INTEGER;
310310

311311
shapes.forEach((shape) => {
312-
let rightX = shape.x + shape.w;
313-
let leftX = shape.x;
314-
let topY = shape.y;
315-
let bottomY = shape.y - shape.h;
312+
const rightX = shape.x + shape.w;
313+
const leftX = shape.x;
314+
const topY = shape.y;
315+
const bottomY = shape.y - shape.h;
316316

317317
if (rightX > maxX) maxX = rightX;
318318
if (leftX < minX) minX = leftX;

apps/roam/src/components/ExportGithub.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import MenuItemSelect from "roamjs-components/components/MenuItemSelect";
1111
import apiGet from "roamjs-components/util/apiGet";
1212
import apiPost from "roamjs-components/util/apiPost";
1313
import { getNodeEnv } from "roamjs-components/util/env";
14-
import getExtensionApi from "roamjs-components/util/extensionApiContext";
1514
import { setSetting } from "~/utils/extensionSettings";
1615

1716
type UserReposResponse = {
@@ -60,11 +59,11 @@ export const ExportGithub = ({
6059
const isDev = useMemo(() => getNodeEnv() === "development", []);
6160
const setRepo = (repo: string) => {
6261
setSelectedRepo(repo);
63-
setSetting("selected-repo", repo);
62+
void setSetting("selected-repo", repo).catch(() => undefined);
6463
};
6564

66-
const handleReceivedAccessToken = (token: string) => {
67-
setSetting("oauth-github", token);
65+
const handleReceivedAccessToken = (token: string): void => {
66+
void setSetting("oauth-github", token).catch(() => undefined);
6867
setGitHubAccessToken(token);
6968
setClickedInstall(false);
7069
authWindow.current?.close();
@@ -93,7 +92,7 @@ export const ExportGithub = ({
9392

9493
if (e.message === "Bad credentials") {
9594
setGitHubAccessToken(null);
96-
setSetting("oauth-github", "");
95+
void setSetting("oauth-github", "").catch(() => undefined);
9796
}
9897
return false;
9998
}

apps/roam/src/components/settings/AdminPanel.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,10 @@ const NodeListTab = (): React.ReactElement => {
257257

258258
const MigrationTab = (): React.ReactElement => {
259259
let initial = true;
260-
const enabled = getSetting("use-reified-relations");
261260
const [useMigrationResults, setMigrationResults] = useState<string>("");
262261
const [useOngoing, setOngoing] = useState<boolean>(false);
263262
const [useDryRun, setDryRun] = useState<boolean>(false);
263+
const enabled = getSetting("use-reified-relations", false);
264264
const doMigrateRelations = async () => {
265265
setOngoing(true);
266266
try {
@@ -428,7 +428,9 @@ const FeatureFlagsTab = (): React.ReactElement => {
428428
onChange={(e) => {
429429
const target = e.target as HTMLInputElement;
430430
setUseReifiedRelations(target.checked);
431-
setSetting("use-reified-relations", target.checked);
431+
void setSetting("use-reified-relations", target.checked).catch(
432+
() => undefined,
433+
);
432434
}}
433435
labelElement={
434436
<>

apps/roam/src/components/settings/DiscourseRelationConfigPanel.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,10 +910,10 @@ export const RelationEditPanel = ({
910910
disabled={loading}
911911
onClick={() => {
912912
saveCyToElementRef(tab);
913-
setSetting(
913+
void setSetting(
914914
"discourse-relation-copy",
915915
JSON.stringify(elementsRef.current[tab]),
916-
);
916+
).catch(() => undefined);
917917
renderToast({
918918
id: "relation-copy",
919919
content: "Copied Relation",

apps/roam/src/components/settings/HomePersonalSettings.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ const HomePersonalSettings = ({ onloadArgs }: { onloadArgs: OnloadArgs }) => {
215215
)}
216216
onChange={(e) => {
217217
const target = e.target as HTMLInputElement;
218-
setSetting(DISCOURSE_CONTEXT_OVERLAY_IN_CANVAS_KEY, target.checked);
218+
void setSetting(
219+
DISCOURSE_CONTEXT_OVERLAY_IN_CANVAS_KEY,
220+
target.checked,
221+
).catch(() => undefined);
219222
}}
220223
labelElement={
221224
<>
@@ -232,7 +235,9 @@ const HomePersonalSettings = ({ onloadArgs }: { onloadArgs: OnloadArgs }) => {
232235
defaultChecked={getSetting(STREAMLINE_STYLING_KEY, false)}
233236
onChange={(e) => {
234237
const target = e.target as HTMLInputElement;
235-
setSetting(STREAMLINE_STYLING_KEY, target.checked);
238+
void setSetting(STREAMLINE_STYLING_KEY, target.checked).catch(
239+
() => undefined,
240+
);
236241

237242
// Load or unload the streamline styling
238243
const existingStyleElement =

apps/roam/src/utils/createReifiedBlock.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import createBlock from "roamjs-components/writes/createBlock";
22
import createPage from "roamjs-components/writes/createPage";
33
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
4-
import { getSetting } from "~/utils/extensionSettings";
54

65
export const DISCOURSE_GRAPH_PROP_NAME = "discourse-graph";
76

@@ -117,15 +116,12 @@ export const createReifiedRelation = async ({
117116
relationBlockUid: string;
118117
destinationUid: string;
119118
}): Promise<string | undefined> => {
120-
const authorized = getSetting("use-reified-relations");
121-
if (authorized) {
122-
return await createReifiedBlock({
123-
destinationBlockUid: await getOrCreateRelationPageUid(),
124-
schemaUid: relationBlockUid,
125-
parameterUids: {
126-
sourceUid,
127-
destinationUid,
128-
},
129-
});
130-
}
119+
return await createReifiedBlock({
120+
destinationBlockUid: await getOrCreateRelationPageUid(),
121+
schemaUid: relationBlockUid,
122+
parameterUids: {
123+
sourceUid,
124+
destinationUid,
125+
},
126+
});
131127
};
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import getExtensionAPI from "roamjs-components/util/extensionApiContext";
22

3-
export function getSetting<T>(key: string, defaultValue?: T): T {
3+
export const getSetting = <T>(key: string, defaultValue?: T): T => {
44
const extensionAPI = getExtensionAPI();
55
const value = extensionAPI.settings.get(key);
66

77
if (value !== undefined && value !== null) {
88
return value as T;
99
}
1010
return defaultValue as T;
11-
}
11+
};
1212

13-
export function setSetting<T>(key: string, value: T): void {
13+
export const setSetting = async <T>(key: string, value: T): Promise<void> => {
1414
const extensionAPI = getExtensionAPI();
15-
extensionAPI.settings.set(key, value);
16-
}
15+
await extensionAPI.settings.set(key, value);
16+
};

apps/roam/src/utils/migrateRelations.ts

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import getRelationData from "./getRelationData";
22
import getBlockProps from "./getBlockProps";
33
import type { json } from "./getBlockProps";
44
import setBlockProps from "./setBlockProps";
5-
import { getSetting } from "./extensionSettings";
5+
import { getSetting, setSetting } from "./extensionSettings";
66
import {
77
createReifiedRelation,
88
DISCOURSE_GRAPH_PROP_NAME,
@@ -13,50 +13,58 @@ const MIGRATION_PROP_NAME = "relation-migration";
1313
const migrateRelations = async (dryRun = false): Promise<number> => {
1414
const authorized = getSetting("use-reified-relations");
1515
if (!authorized) return 0;
16-
const processed = new Set<string>();
17-
const relationData = await getRelationData();
1816
let numProcessed = 0;
19-
for (const rel of relationData) {
20-
const key = `${rel.source}:${rel.relUid}:${rel.target}`;
21-
if (processed.has(key)) continue;
22-
processed.add(key);
23-
if (!dryRun) {
24-
const uid = (await createReifiedRelation({
25-
sourceUid: rel.source,
26-
destinationUid: rel.target,
27-
relationBlockUid: rel.relUid,
28-
}))!;
29-
const sourceProps = getBlockProps(rel.source);
30-
const dgDataOrig = sourceProps[DISCOURSE_GRAPH_PROP_NAME];
31-
const dgData: Record<string, json> =
32-
dgDataOrig !== null &&
33-
typeof dgDataOrig === "object" &&
34-
!Array.isArray(dgDataOrig)
35-
? dgDataOrig
36-
: {};
37-
const migrationDataOrig = dgData[MIGRATION_PROP_NAME];
38-
let migrationData: Record<string, json> =
39-
migrationDataOrig !== null &&
40-
typeof migrationDataOrig === "object" &&
41-
!Array.isArray(migrationDataOrig)
42-
? migrationDataOrig
43-
: {};
44-
if (migrationData[uid] !== undefined) {
45-
console.debug(`reprocessed ${key}`);
17+
await setSetting("use-reified-relations", false); // so queries use patterns
18+
// wait for the settings to propagate
19+
await new Promise((resolve) => setTimeout(resolve, 150));
20+
try {
21+
const processed = new Set<string>();
22+
const relationData = await getRelationData();
23+
for (const rel of relationData) {
24+
const key = `${rel.source}:${rel.relUid}:${rel.target}`;
25+
if (processed.has(key)) continue;
26+
processed.add(key);
27+
if (!dryRun) {
28+
const uid = (await createReifiedRelation({
29+
sourceUid: rel.source,
30+
destinationUid: rel.target,
31+
relationBlockUid: rel.relUid,
32+
}))!;
33+
const sourceProps = getBlockProps(rel.source);
34+
const dgDataOrig = sourceProps[DISCOURSE_GRAPH_PROP_NAME];
35+
const dgData: Record<string, json> =
36+
dgDataOrig !== null &&
37+
typeof dgDataOrig === "object" &&
38+
!Array.isArray(dgDataOrig)
39+
? dgDataOrig
40+
: {};
41+
const migrationDataOrig = dgData[MIGRATION_PROP_NAME];
42+
let migrationData: Record<string, json> =
43+
migrationDataOrig !== null &&
44+
typeof migrationDataOrig === "object" &&
45+
!Array.isArray(migrationDataOrig)
46+
? migrationDataOrig
47+
: {};
48+
if (migrationData[uid] !== undefined) {
49+
console.debug(`reprocessed ${key}`);
50+
}
51+
// clean up old migration entries
52+
migrationData = Object.fromEntries(
53+
Object.entries(migrationData).filter(
54+
([uid]) =>
55+
window.roamAlphaAPI.q(
56+
`[:find ?p :where [?p :block/uid "${uid}"]]`,
57+
).length > 0,
58+
),
59+
);
60+
migrationData[uid] = new Date().valueOf();
61+
dgData[MIGRATION_PROP_NAME] = migrationData;
62+
setBlockProps(rel.source, { [DISCOURSE_GRAPH_PROP_NAME]: dgData });
4663
}
47-
// clean up old migration entries
48-
migrationData = Object.fromEntries(
49-
Object.entries(migrationData).filter(
50-
([uid]) =>
51-
window.roamAlphaAPI.q(`[:find ?p :where [?p :block/uid "${uid}"]]`)
52-
.length > 0,
53-
),
54-
);
55-
migrationData[uid] = new Date().valueOf();
56-
dgData[MIGRATION_PROP_NAME] = migrationData;
57-
setBlockProps(rel.source, { [DISCOURSE_GRAPH_PROP_NAME]: dgData });
64+
numProcessed++;
5865
}
59-
numProcessed++;
66+
} finally {
67+
await setSetting("use-reified-relations", true);
6068
}
6169
return numProcessed;
6270
};

0 commit comments

Comments
 (0)