App postgresql-server
provides the rest of the apps with the capability of storing and reading data from PostgreSQL. App comes pre-loaded with full schema (located in schema/mediawords.sql
) at build time.
Additionally, on every start the wrapper script will test the instance for the schema version (stored in database_variables
table) that's currently being used in the attached data volume, and if the schema version is older than the current schema version in schema/mediawords.sql
, it will execute appropriate migration scripts (located in schema/migrations/
) to get the data volume's schema to the newest version.
The schema migrations get applied privately, i.e. before the database instance actually goes "live" and becomes accessible to other apps, so every app is guaranteed to be connecting to the database instance with the most up-to-date schema version.
To make changes to the schema:
- Edit the main schema file (located in
schema/mediawords.sql
file under thepostgresql-server
app) to make required changes; - In the main schema, update the
MEDIACLOUD_DATABASE_SCHEMA_VERSION
variable at the top of the file by increasing the schema version; - In the schema migrations directory (located in
schema/migrations/
directory under thepostgresql-server
app), add a new migration file with the namemediawords-<old-schema-version>-<new-schema-version>.sql
; in the migration file, add SQL statements that both- makes the required changes in the schema itself (creates / drops tables, columns, etc.), and
- sets the
MEDIACLOUD_DATABASE_SCHEMA_VERSION
variable to the newest schema version;
- Rebuild
postgresql-server
app image withbuild.py
developer script, or justgit push
the changes to for the CI server to rebuild everything; - Pull the updated
postgresql-server
image in production, remove old container running an outdated image, and create a new container using the updated image and a data volume from the old container. - Start the container. The wrapper script in the container will temporarily start a private instance of PostgreSQL and apply the schema migrations before starting a public instance of the service for other apps to use.