Run database migrations
The API's tables change from release to release, and each change is a migration of the tool Alembic, stored as a revision in backend-api/backend/alembic/versions. The migrations run before the API starts, so that a new image meets the schema that it expects. This guide explains when they run, how several containers avoid running them at once, and how to run them yourself, check the state of a database and prepare a first deployment.
When the migrations run
The API's image starts with the script api_entrypoint.py. Its first step runs python -m alembic upgrade head and its second step starts the API. When the migrations fail, the script prints "Database migrations failed" and stops with the exit status 1, so a container that cannot migrate does not start serving requests, and a platform that restarts it shows the reason in the log. The upgrade applies every revision that the database lacks, in order, and does nothing when the database is current.
A deployment that runs several replicas can also run the migrations in a step of its own before it rolls out the replicas, for example as a job that starts the API's image with the command above. That step is safe to run again whenever a release starts.
Several containers at once
Starting many replicas at the same moment would run the same migration several times, so for MySQL and PostgreSQL the migrations take a lock in Valkey, under the key alembic_migrations. The first container that finds the lock free keeps it for at most 2 minutes and migrates. The others print "Another migration in progress by pod" and the id of the holder, and they wait, asking again every 5 seconds. When the first container has finished, it leaves a marker that makes the others print "Migrations already applied, skipping..." for the next 30 seconds. The lock needs the deployment's Valkey, which Connect FINOS Workspace to its database and cache describes, so the migrations need that Valkey to be reachable. SQLite takes no lock, because it serves a single machine.
Run the migrations yourself
Run Alembic inside the API's container, in the directory backend-api/backend, with the same environment as the API:
python -m alembic upgrade head
To see which revision a database is at, and which revisions the image knows, use:
python -m alembic current
python -m alembic history
Alembic prints the identifier of each revision. A database that is behind has a current revision that is older than the newest one of the history, and upgrade head brings it to the newest.
Upgrade a deployment
- Back up the database, because a migration changes the data in place, and some revisions remove columns that nothing reads any more.
- Roll out the new image. The migrations run when its first container starts.
- Check that the API's log says "Database migrations completed successfully", and that
/healthanswers.
Roll out the replicas of the API and the worker together, because the code of an old replica does not know a column that a new revision added or removed. Do not run an older image against a database that a newer image has migrated.
Prepare a first deployment
On an empty database, the same step creates every table. The deployment also needs its first organization and administrator, which the script scripts.init_users creates from the file scripts/cfgs/config.toml: the file names the organization and the administrators, and the script creates them when they do not exist, creates the roles and sets the DEFAULT_USER_MAPPING that new people receive in the file user_create.json. The script is safe to run on every start, and a run that finds everything in place changes nothing. Provision an organization from a file is the way to create further organizations, and the administrator gets a password with the command python -m scripts.init_users --new-password <email>, as Run FINOS Workspace on your machine shows.