Skip to content

Commit 2335799

Browse files
committed
move log util out
1 parent 4d3c02d commit 2335799

File tree

2 files changed

+77
-76
lines changed

2 files changed

+77
-76
lines changed

src/client/index.ts

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type { MigrationArgs, MigrationResult, MigrationStatus };
2626

2727
import { ConvexError, type GenericId } from "convex/values";
2828
import type { ComponentApi } from "../component/_generated/component.js";
29+
import { logStatusAndInstructions } from "./log.js";
2930

3031
// Note: this value is hard-coded in the docstring below. Please keep in sync.
3132
export const DEFAULT_BATCH_SIZE = 100;
@@ -608,82 +609,6 @@ export type MigrationFunctionReference = FunctionReference<
608609
MigrationArgs
609610
>;
610611

611-
612-
function logStatusAndInstructions(
613-
name: string,
614-
status: MigrationStatus,
615-
args: {
616-
fn?: string;
617-
cursor?: string | null;
618-
batchSize?: number;
619-
dryRun?: boolean;
620-
},
621-
) {
622-
const output: Record<string, unknown> = {};
623-
if (status.isDone) {
624-
if (status.latestEnd! < Date.now()) {
625-
output["Status"] = "Migration already done.";
626-
} else if (status.latestStart === status.latestEnd) {
627-
output["Status"] = "Migration was started and finished in one batch.";
628-
} else {
629-
output["Status"] = "Migration completed with this batch.";
630-
}
631-
} else {
632-
if (status.state === "failed") {
633-
output["Status"] = `Migration failed: ${status.error}`;
634-
} else if (status.state === "canceled") {
635-
output["Status"] = "Migration canceled.";
636-
} else if (status.latestStart >= Date.now()) {
637-
output["Status"] = "Migration started.";
638-
} else {
639-
output["Status"] = "Migration running.";
640-
}
641-
}
642-
if (args.dryRun) {
643-
output["DryRun"] = "No changes were committed.";
644-
output["Status"] = "DRY RUN: " + output["Status"];
645-
}
646-
output["Name"] = name;
647-
output["lastStarted"] = new Date(status.latestStart).toISOString();
648-
if (status.latestEnd) {
649-
output["lastFinished"] = new Date(status.latestEnd).toISOString();
650-
}
651-
output["processed"] = status.processed;
652-
if (status.next?.length) {
653-
if (status.isDone) {
654-
output["nowUp"] = status.next;
655-
} else {
656-
output["nextUp"] = status.next;
657-
}
658-
}
659-
const nextArgs = (status.next || []).map((n) => `"${n}"`).join(", ");
660-
const run = `npx convex run --component migrations`;
661-
if (!args.dryRun) {
662-
if (status.state === "inProgress") {
663-
output["toCancel"] = {
664-
cmd: `${run} lib:cancel`,
665-
args: `{"name": "${name}"}`,
666-
prod: `--prod`,
667-
};
668-
output["toMonitorStatus"] = {
669-
cmd: `${run} --watch lib:getStatus`,
670-
args: `{"names": ["${name}"${status.next?.length ? ", " + nextArgs : ""}]}`,
671-
prod: `--prod`,
672-
};
673-
} else {
674-
output["toStartOver"] = JSON.stringify({ ...args, cursor: null });
675-
if (status.next?.length) {
676-
output["toMonitorStatus"] = {
677-
cmd: `${run} --watch lib:getStatus`,
678-
args: `{"names": [${nextArgs}]}`,
679-
prod: `--prod`,
680-
};
681-
}
682-
}
683-
}
684-
return output;
685-
}
686-
687612
/* Type utils follow */
688613

689614
type QueryCtx = Pick<GenericQueryCtx<GenericDataModel>, "runQuery">;

src/client/log.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import type { MigrationStatus } from "../shared.js";
2+
3+
export function logStatusAndInstructions(
4+
name: string,
5+
status: MigrationStatus,
6+
args: {
7+
fn?: string;
8+
cursor?: string | null;
9+
batchSize?: number;
10+
dryRun?: boolean;
11+
},
12+
) {
13+
const output: Record<string, unknown> = {};
14+
if (status.isDone) {
15+
if (status.latestEnd! < Date.now()) {
16+
output["Status"] = "Migration already done.";
17+
} else if (status.latestStart === status.latestEnd) {
18+
output["Status"] = "Migration was started and finished in one batch.";
19+
} else {
20+
output["Status"] = "Migration completed with this batch.";
21+
}
22+
} else {
23+
if (status.state === "failed") {
24+
output["Status"] = `Migration failed: ${status.error}`;
25+
} else if (status.state === "canceled") {
26+
output["Status"] = "Migration canceled.";
27+
} else if (status.latestStart >= Date.now()) {
28+
output["Status"] = "Migration started.";
29+
} else {
30+
output["Status"] = "Migration running.";
31+
}
32+
}
33+
if (args.dryRun) {
34+
output["DryRun"] = "No changes were committed.";
35+
output["Status"] = "DRY RUN: " + output["Status"];
36+
}
37+
output["Name"] = name;
38+
output["lastStarted"] = new Date(status.latestStart).toISOString();
39+
if (status.latestEnd) {
40+
output["lastFinished"] = new Date(status.latestEnd).toISOString();
41+
}
42+
output["processed"] = status.processed;
43+
if (status.next?.length) {
44+
if (status.isDone) {
45+
output["nowUp"] = status.next;
46+
} else {
47+
output["nextUp"] = status.next;
48+
}
49+
}
50+
const nextArgs = (status.next || []).map((n) => `"${n}"`).join(", ");
51+
const run = `npx convex run --component migrations`;
52+
if (!args.dryRun) {
53+
if (status.state === "inProgress") {
54+
output["toCancel"] = {
55+
cmd: `${run} lib:cancel`,
56+
args: `{"name": "${name}"}`,
57+
prod: `--prod`,
58+
};
59+
output["toMonitorStatus"] = {
60+
cmd: `${run} --watch lib:getStatus`,
61+
args: `{"names": ["${name}"${status.next?.length ? ", " + nextArgs : ""}]}`,
62+
prod: `--prod`,
63+
};
64+
} else {
65+
output["toStartOver"] = JSON.stringify({ ...args, cursor: null });
66+
if (status.next?.length) {
67+
output["toMonitorStatus"] = {
68+
cmd: `${run} --watch lib:getStatus`,
69+
args: `{"names": [${nextArgs}]}`,
70+
prod: `--prod`,
71+
};
72+
}
73+
}
74+
}
75+
return output;
76+
}

0 commit comments

Comments
 (0)