CabalDAO
CabalDAO6mo ago

How to use an mutation in a preview deployment setup

Thanks - ok - so i would write something like api.mutations.myFunctionName? And how would that fit into a deploy integration where I am importing data to preview first? I.e. where would it go in the deploy command: npx convex deploy --cmd 'npm run build' && if [ "$VERCEL_ENV" == "preview" ]; then npx convex import --preview-name "$VERCEL_GIT_COMMIT_REF" ./data/mock-data/rtp_preview_snapshot.zip; fi
12 Replies
ballingt
ballingt6mo ago
Close, but
mutations:myFunctionName
mutations:myFunctionName
That looks good, is that not working?
CabalDAO
CabalDAOOP6mo ago
I will test it out - but where would i include it in the deploy command so it runs after the import?
ballingt
ballingt6mo ago
Your script:
npx convex deploy --cmd 'npm run build' && if [ "$VERCEL_ENV" == "preview" ]; then npx convex import --preview-name "$VERCEL_GIT_COMMIT_REF" ./data/mock-data/rtp_preview_snapshot.zip; fi
npx convex deploy --cmd 'npm run build' && if [ "$VERCEL_ENV" == "preview" ]; then npx convex import --preview-name "$VERCEL_GIT_COMMIT_REF" ./data/mock-data/rtp_preview_snapshot.zip; fi
You want to run this command after the import?
CabalDAO
CabalDAOOP6mo ago
yes
ballingt
ballingt6mo ago
npx convex deploy --cmd 'npm run build' && if [ "$VERCEL_ENV" == "preview" ]; then npx convex import --preview-name "$VERCEL_GIT_COMMIT_REF" ./data/mock-data/rtp_preview_snapshot.zip; run-your-command-here; fi
npx convex deploy --cmd 'npm run build' && if [ "$VERCEL_ENV" == "preview" ]; then npx convex import --preview-name "$VERCEL_GIT_COMMIT_REF" ./data/mock-data/rtp_preview_snapshot.zip; run-your-command-here; fi
CabalDAO
CabalDAOOP6mo ago
just write it out or do i have to prefix it with --preview-run
ballingt
ballingt6mo ago
Are you familiar with shell? this is an if statment. Commands are separated by semicolons if you want the next one to run regardless. Hm what is your goal?
CabalDAO
CabalDAOOP6mo ago
yes just wondering if I need to use the --preview-run prefix and if it would work even though in the docs --preview-run is listed as part of the convex deploy command yea when i added --preview-run and the function name it says --preview-run command not found
ballingt
ballingt6mo ago
Ah check out the help for convex run npx convex run --help there's a --preview-name flag
CabalDAO
CabalDAOOP6mo ago
i tried with and without the --preview-run but it says command not found Can you help me with exactly what the full command would be? npx convex deploy --cmd 'npm run build' && if [ "$VERCEL_ENV" == "preview" ]; then npx convex import --preview-name "$VERCEL_GIT_COMMIT_REF" ./data/mock-data/rtp_preview_snapshot.zip; migrations:mutateOrgIdForPreview; fi This does not work
ballingt
ballingt6mo ago
you want to run the command npx convex run --preview-name "$VERCEL_GIT_COMMIT_REF" migration:mutateOrgIdForPreview '{"foo": 123}' that's what should run where you have written migrations:mutateOrgIdForPreview
i tried with and without the --preview-run but it says command not found
Check out the help for run by running npx convex run --help in a terminal, it tells you which flags are allowed
CabalDAO
CabalDAOOP6mo ago
Thank you, that seems to work but for some reason the function I wrote is failing with an error I dont understand. Here's my function it's calling:
import { mutation } from "./_generated/server";

export const mutateOrgIdForPreview = mutation({
args: {},
handler: async (ctx, args) => {
const previewOrgId = "org_2lCrbFBnr0ANpF9RrkekHyHO2Cu";
const devOrgId = "org_2l2BRUaCWlNnBnZFADyxl58FPLp";

const referralOverviews = await ctx.db
.query("referralOverview")
.withIndex("clerkOrgId", q => q.eq("clerkOrgId", devOrgId))
.collect();

console.log(referralOverviews)

await Promise.all(
referralOverviews.map(async (referralOverview) => {
return await ctx.db.patch(referralOverview._id, {
clerkOrgId: previewOrgId,
})
})
);

return true;
},
});
import { mutation } from "./_generated/server";

export const mutateOrgIdForPreview = mutation({
args: {},
handler: async (ctx, args) => {
const previewOrgId = "org_2lCrbFBnr0ANpF9RrkekHyHO2Cu";
const devOrgId = "org_2l2BRUaCWlNnBnZFADyxl58FPLp";

const referralOverviews = await ctx.db
.query("referralOverview")
.withIndex("clerkOrgId", q => q.eq("clerkOrgId", devOrgId))
.collect();

console.log(referralOverviews)

await Promise.all(
referralOverviews.map(async (referralOverview) => {
return await ctx.db.patch(referralOverview._id, {
clerkOrgId: previewOrgId,
})
})
);

return true;
},
});
Oh, I take it back, it seems to work now Thank you for your help

Did you find this page helpful?