How do you execute D1 migration with Worker Build?

Is it possible to perform migration to d1 by CI/CD with workers build? Currently, I have set up a deploy command that executes bunx wrangler d1 execute migration apply and bunx wrangler deploy in sequence. How do you guys do it?
1 Reply
shun
shunOP5w ago
@PatrickJ Yes. I am also able to perform migration by running the following shell.
#!/bin/bash

WORKER_ENV=$1
DATABASE_NAME=$2

chmod +x ./misc/scripts/slack-notification.sh

bunx wrangler d1 migrations apply $DATABASE_NAME --remote
MIGRATION_RESULT=$?
if [ $MIGRATION_RESULT -ne 0 ]; then
./misc/scripts/slack-notification.sh "$WORKER_ENV" "failed"
exit 1
fi

bunx wrangler deploy
DEPLOY_RESULT=$?

if [ $DEPLOY_RESULT -eq 0 ]; then
./misc/scripts/slack-notification.sh "$WORKER_ENV" "success"
else
./misc/scripts/slack-notification.sh "$WORKER_ENV" "failed"
fi
#!/bin/bash

WORKER_ENV=$1
DATABASE_NAME=$2

chmod +x ./misc/scripts/slack-notification.sh

bunx wrangler d1 migrations apply $DATABASE_NAME --remote
MIGRATION_RESULT=$?
if [ $MIGRATION_RESULT -ne 0 ]; then
./misc/scripts/slack-notification.sh "$WORKER_ENV" "failed"
exit 1
fi

bunx wrangler deploy
DEPLOY_RESULT=$?

if [ $DEPLOY_RESULT -eq 0 ]; then
./misc/scripts/slack-notification.sh "$WORKER_ENV" "success"
else
./misc/scripts/slack-notification.sh "$WORKER_ENV" "failed"
fi
My deploy command in Cloudflara Workers Build menu is below. chmod +x ./misc/scripts/deploy.sh && ./misc/scripts/deploy.sh "staging" "my-d1-database-name" I'm separating workers by env (staging, production, etc..). so arguments in ./misc/scripts/deploy.sh is ok for fixed value.
Want results from more Discord servers?
Add your server