Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve logging and default handling #8

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,37 +300,30 @@ export class Migrations<DataModel extends GenericDataModel> {
return (await this._runInteractive(ctx, args)) as any;
} else if (args.next?.length) {
throw new Error("You can only pass next if you also provide fn");
}
const numItems = args.batchSize || defaultBatchSize;
if (!args.batchSize) {
if (args.batchSize === 0) {
console.warn(`Batch size is zero. Using the default: ${numItems}`);
}
console.warn(
"Running this from the dashboard? Here's some args to use:"
);
console.warn({
"Dry run": '{ "dryRun": true, "cursor": null }',
"For real": '{ "fn": "path/to/migrations:yourFnName" }',
});
}
if (
(args.cursor === undefined || args.cursor === "") &&
args.dryRun === undefined
} else if (
args.cursor === undefined ||
args.cursor === "" ||
args.dryRun === undefined ||
args.batchSize === 0
) {
console.warn(
"No cursor or dryRun specified - doing a dry run on the next batch" +
"Running this from the CLI or dashboard? Here's some args to use:"
"Running this from the CLI or dashboard? Here's some args to use:"
);
console.warn({
"Dry run": '{ "dryRun": true, "cursor": null }',
"For real": '{ "fn": "path/to/migrations:yourFnName" }',
});
args.cursor = null;
args.dryRun = true;
}
if (args.cursor === "" || args.cursor === undefined) {
if (args.dryRun) {

const numItems = args.batchSize || defaultBatchSize;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think batchSize=0 still becomes defaultBatchSize. is that intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary, but is also ok here. I believe this is only possible when calling from the dashboard / CLI and that value is likely only ever 0 if it's set as a "helpful" default.

if (args.cursor === undefined || args.cursor === "") {
if (args.dryRun === undefined) {
console.warn(
"No cursor or dryRun specified - doing a dry run on the next batch."
);
args.cursor = null;
args.dryRun = true;
} else if (args.dryRun) {
console.warn("Setting cursor to null for dry run");
args.cursor = null;
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/component/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export const migrate = mutation({
handler: async (ctx, args) => {
// Step 1: Get or create the state.
const { fnHandle, batchSize, next: next_, dryRun, ...initialState } = args;
if (batchSize !== undefined && batchSize <= 0) {
throw new Error("Batch size must be greater than 0");
}
if (!fnHandle.startsWith("function://")) {
throw new Error(
"Invalid fnHandle.\n" +
Expand Down