A migration or backup service that runs once and exits is easy to start with up -d, but a script also needs to know when it is done. docker compose wait does exactly that.
docker compose up -d migrate
docker compose wait migrate
The command blocks until the containers of the named services stop, then prints their exit codes, so your script can read the result and stop when the migration failed. Leave out the service name and it waits for all of them.
There is also --down-project, which takes down the whole project as soon as the first container stops. I would use that for a test stack: start everything, wait for the test runner, clean up in one go.
It waits for a stop, so on a long running service like a web server it simply hangs. And a service with restart: always or unless-stopped never really stays stopped, so give one-shot jobs restart: "no" (the default). Inside Compose itself, depends_on with condition: service_completed_successfully is the cleaner way to order things; wait is for the script around it.
Further reading: docker compose wait