66 type GenericDataModel ,
77 type GenericMutationCtx ,
88 type GenericQueryCtx ,
9+ getFunctionAddress ,
910 getFunctionName ,
1011 internalMutationGeneric ,
1112 makeFunctionReference ,
@@ -27,6 +28,7 @@ export type { MigrationArgs, MigrationResult, MigrationStatus };
2728import { ConvexError , type GenericId } from "convex/values" ;
2829import type { ComponentApi } from "../component/_generated/component.js" ;
2930import { logStatusAndInstructions } from "./log.js" ;
31+ import type { MigrationFunctionHandle } from "../component/lib.js" ;
3032
3133// Note: this value is hard-coded in the docstring below. Please keep in sync.
3234export const DEFAULT_BATCH_SIZE = 100 ;
@@ -637,8 +639,12 @@ export type MigrationFunctionReference = FunctionReference<
637639export async function runToCompletion (
638640 ctx : ActionCtx ,
639641 component : ComponentApi ,
640- fnRef : MigrationFunctionReference ,
642+ fnRef : MigrationFunctionReference | MigrationFunctionHandle ,
641643 opts ?: {
644+ /**
645+ * The name of the migration function, generated with getFunctionName.
646+ */
647+ name ?: string ;
642648 /**
643649 * The cursor to start from.
644650 * null: start from the beginning.
@@ -656,15 +662,23 @@ export async function runToCompletion(
656662 */
657663 dryRun ?: boolean ;
658664 } ,
659- ) {
665+ ) : Promise < MigrationStatus > {
660666 let cursor = opts ?. cursor ;
667+ const {
668+ name = getFunctionName ( fnRef ) ,
669+ batchSize,
670+ dryRun = false ,
671+ } = opts ?? { } ;
672+ const address = getFunctionAddress ( fnRef ) ;
673+ const fnHandle =
674+ address . functionHandle ?? ( await createFunctionHandle ( fnRef ) ) ;
661675 while ( true ) {
662676 const status = await ctx . runMutation ( component . lib . migrate , {
663- name : getFunctionName ( fnRef ) ,
664- fnHandle : await createFunctionHandle ( fnRef ) ,
677+ name,
678+ fnHandle,
665679 cursor,
666- batchSize : opts ?. batchSize ,
667- dryRun : opts ?. dryRun ?? false ,
680+ batchSize,
681+ dryRun,
668682 oneBatchOnly : true ,
669683 } ) ;
670684 if ( status . isDone ) {
0 commit comments