Implement database migrations for Postgres backend #1878
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Using the current implementation of "migrations" causes a heavy load on the database as it locks the tables during "create if not exists" while starting a worker process. This would be fine if only a single worker is used and there is no concurrent access to the database but in most scenarios you would use a WSGI server like gunicorn which spawns multiple workers which all try to apply migrations at the same time and lock the tables. Gunicorn may also restart a worker if it has not accepted a request in a specified timeout which will resurrect and try to apply migrations, thus locking the tables and all current requests going to other workers. The requested implementation will not lock tables every time and it will only do so if there are unapplied migrations. This also helps with updating the database schema obviously and there would not be need to check if a field exists like:
Changes
Include a brief summary of changes...
Checklist