This workspace is used for database schema definitions and migrations.
Project currently uses PostgreSQL 11. Make sure you use the correct version.
## Migrations usage
:exclamation: It is important to understand that migrations that had already been performed are _locked_ and are not a subject to change. Therefore for every alteration of schema you need to generate a new migration and perform alterations there.
Never commit changed files for migrations that already had been performed elsewhere, - it will break the migrations.
To migrate to latest version: `yarn migrate`
To migrate up/down to specific version: `yarn migrate <version>`
To migrate down to empty state: `yarn migrate 0000`
To generate new migration: `yarn generate`
_* At the moment, version numbers expect to be padded to 4 digits_
## Policies and roles
We use Row Level Security when accessing the database from the application. In order to create a correct schema for new tables please study migration files for the schemas of previous tables (including possible later changes).
In order to use Row Level Security every transaction with the database must set the correct role via `omnivore.set_claims` function.
### Current roles
`omnivore_user` - a user role that is intended for a regular user to access the data. Currently, this is the primary user of the database.
## Database users on GCP
`postgres` - administrator of the database, used for migrations.
`app_user` - a user that the app uses to login to database.
:exclamation: Do not issue any extra grants to `app_user` other than that are needed to assume a certain internal role, i.e. `GRANT omnivore_user TO app_user`
## Installing and Using locally
- Install and run the _postgresql_ service on your machine.