@@ -442,4 +442,57 @@ await migrations.getStatus(ctx, { migrations: ["myNewMutation"] });
442442await migrations .cancel (ctx , " myNewMutation" );
443443```
444444
445+ ## Running migrations synchronously
446+
447+ If you want to run a migration synchronously from a test or action, you can use
448+ ` runSynchronously ` . Note that if the action crashes or is canceled, it will not
449+ continue migrating in the background.
450+
451+ From an action:
452+
453+ ``` ts
454+ import { components , internal } from " ./_generated/api" ;
455+ import { internalAction } from " ./_generated/server" ;
456+ import { runSynchronously } from " @convex-dev/migrations" ;
457+
458+ export const myAction = internalAction ({
459+ args: {},
460+ handler : async (ctx ) => {
461+ // ...
462+ const toRun = internal .example .setDefaultValue ;
463+ await runSynchronously (ctx , components .migrations , toRun );
464+ },
465+ });
466+ ```
467+
468+ In a test:
469+
470+ ``` ts
471+ import { test } from " vitest" ;
472+ import { convexTest } from " convex-test" ;
473+ import component from " @convex-dev/migrations/test" ;
474+ import { runSynchronously } from " @convex-dev/migrations" ;
475+ import { components , internal } from " ./_generated/api" ;
476+ import schema from " ./schema" ;
477+
478+ test (" test setDefaultValue migration" , async () => {
479+ const t = convexTest (schema );
480+ // Register the component in the test instance
481+ component .register (t );
482+
483+ await t .run (async (ctx ) => {
484+ // Add sample data to migrate
485+ await ctx .db .insert (" myTable" , { optionalField: undefined });
486+
487+ // Run the migration to completion
488+ const migrationToTest = internal .example .setDefaultValue ;
489+ await runSynchronously (ctx , components .migrations , migrationToTest );
490+
491+ // Assert that the migration was successful by checking the data
492+ const docs = await ctx .db .query (" myTable" ).collect ();
493+ expect (docs .every ((doc ) => doc .optionalField !== undefined )).toBe (true );
494+ });
495+ });
496+ ```
497+
445498<!-- END: Include on https://convex.dev/components -->
0 commit comments