Why is running a migration as a `pre-install` hook often a mistake?
AdvancedConceptHooks, Tests & Validation
Answer
pre-install runs before any release resource exists. If the database is a subchart in the same release, its StatefulSet, Service and Secret do not exist yet, so the migration Job cannot connect and fails, failing the install. Common fixes:
- Use
post-install,pre-upgrade: on first install the DB comes up first; on upgrades the DB already exists and migrations run before the new app version rolls out. - Or run migrations from an init container or at app startup with a migration lock.
- Make the Job wait for the DB with an init container running
pg_isready.
Also put any ConfigMap or Secret the hook needs into the hook itself (with a lower weight), because on pre-upgrade the new non-hook ConfigMap has not been applied yet.