Preview Environment release scripts and DB Migrations

I’m moving a simple web + db setup from PR reviews to Preview environments so there is a separate DB for each PR.

On the very first deployment of the environment, I want to copy a DB using a script. Then when a build happens, I run DB migrations. Subsequent pushes would just run the build command again which would run migrations.

What I’m struggling with is how to fit this into Render’s lifecycle. The only hook I have is afterFirstDeployCommand but that runs after the build and the start command so the migration errors. There’s also no env variable indicating that this is the first deploy so I can’t write a script.

What I’m after is a hook like Heroku’s postdeploy which I’ve used for this sort of thing in the past. I need to run a script after service creation, but before the build

If I understand correctly, your migrations do not succeed on an empty database?

If scripting is an option, how about modifying the copy process to copy only if the destination is empty? Then you can run the copy & migrations together always.

Thanks for the reply but I’m giving up on the preview environment path for now.

I ended up adding the DB copy to the migration but ran into a roadblock where the Postgres tools pg_dump and pg_restore are not available in the Render environment.

What would be useful is a way of cloning a DB in the YAML definition.

I’ll revisit this when I have seed scripts that can be run instead of a straight DB copy.