Fork me on GitHub
#sql
<
2022-08-18
>
athomasoriginal13:08:49

What are people's experience with auto-running DB migrations on app start? I normally just setup apps to manually run a command like run-migrations , but I have seen that some will trigger the command whenever thee app starts.

agile_geek14:08:59

I’ve used a number of approaches but usually for a web service I have an endpoint that will trigger applying migrations and a step in the CD pipeline that pings that endpoint.

agile_geek14:08:57

You can also secure that endpoint to ensure it’s not DDOS’ed.

athomasoriginal14:08:07

Interesting. What do you find is the advantage of an endpoint?

agile_geek14:08:14

If your CD pipeline is run on the same infrastructure as the service i.e. codepipeline in AWS and AWS ECS/EKS, you can expose that endpoint only to the VPN.

👍 1
agile_geek14:08:12

I can ensure the migrations are run once on every deployment but if the migration fails I can prevent the service spinning up. Also I can manually rerun if required.

agile_geek14:08:07

I’m sure there are better ways but this is simple and ensures that migrations are not run for every instance of the service but only once per deployment.

👍 1
agile_geek14:08:22

Running on start would run the migration for every instance. So scaling to ten instances would attempt 10 migrations and it’s possible if they’re not written carefully to be idempotent they could interfere with each other.

agile_geek14:08:04

Simplifies to reduce race conditions.

agile_geek14:08:25

Also it’s easy to test locally.

agile_geek14:08:36

Another potentially ‘better’ approach is to have an argument to the main fn that only runs the migration and run that once in CD.

1