Is possible to run migrations in Pages build command?

I'm not sure what is the right flow to run D1 migrations... If I understand it correctly so far, you have to:

Push migrations with a command: npx wrangler d1 migrations apply db-name as a wrangler command from the local env to make changes in the prod. DB
and then push the changed app code (wich rely on new DB schema) to git.

Shouldn't be a migration run right after the deployment in the "build command" like:
npm run build && npx wrangler d1 migrations apply db-name

Is this possible?

I tried the comamnd and got the error:
In a non-interactive environment, it's necessary to set a CLOUDFLARE_API_TOKEN environment variable for wrangler to work. Please go to https://developers.cloudflare.com/fundamentals/api/get-started/create-token/ for instructions on how to create an api token, and assign its value to CLOUDFLARE_API_TOKEN.


After creating CLOUDFLARE_API_TOKEN env var I get this error:
Failed: Error while executing user command. Exited with error code: 1
Failed: build command exited with code: 1
Failed: error occurred while running build command
Was this page helpful?