|
1 | 1 | # database-specs |
2 | 2 |
|
3 | | -Contains the schema used for the Verifier Alliance database |
| 3 | +This repository contains the schema used for the Verifier Alliance database. The full schema is contained in `./database.sql`. This file is a generated dump from all applied schema migrations. The initial database schema can be found in `./migrations/20250717103432_database.sql`. It includes documentation about the schema and its tables. |
| 4 | +For a graphical representation of the schema, you can visit the [VerA docs](https://verifieralliance.org/docs/database-schema). |
| 5 | + |
| 6 | +## Migrations |
| 7 | + |
| 8 | +For keeping track of schema changes, we use a lightweight tool called [dbmate](https://github.com/amacneil/dbmate). Please follow the instructions on its GitHub repository for installation. |
| 9 | + |
| 10 | +### Prerequisites |
| 11 | + |
| 12 | +As a prerequisite for using dbmate, you should have a `.env` file configured with the database connection details. |
| 13 | +For this, copy the `.env.template` file to `.env` and replace the database connection string. |
| 14 | + |
| 15 | +### Running migrations |
| 16 | + |
| 17 | +For running all pending migrations against the database configured in `.env`, you can simply run: |
| 18 | + |
| 19 | +```bash |
| 20 | +dbmate migrate |
| 21 | +``` |
| 22 | + |
| 23 | +### Adding a new migration |
| 24 | + |
| 25 | +Any migration added should be capable of updating the live VerA production and test database. |
| 26 | +This means that a migration should also transform data if necessary. |
| 27 | + |
| 28 | +For setting up a new migration, please use a fresh database, because dbmate will dump the current state of the database into `database.sql` after running the migration. |
| 29 | +For using a fresh database, simply configure an unused database name in the `.env` file. |
| 30 | + |
| 31 | +The process for adding a new migration is as follows: |
| 32 | + |
| 33 | +1. Create a new migration file: `dbmate new <migration_name>` |
| 34 | +2. Add the SQL needed for the schema change in the generated migration file (e.g., `migrations/20250717103432_add_new_table.sql`) |
| 35 | + - The migration file should also include a `down` section to revert the changes if necessary. |
| 36 | + - It should transform any data if necessary, e.g., for filling a new column with default values or copying data from one table to another. |
| 37 | + - Ideally, the migration file should be idempotent, meaning it can be run multiple times without causing errors. |
| 38 | +3. Create a fresh database with the name configured in `.env`: `dbmate create` |
| 39 | +4. Run the migrations on the new database in order to generate the `database.sql` dump: `dbmate migrate` |
| 40 | +5. You can drop the database again: `dbmate drop` |
| 41 | +6. Both the migration file and the updated `database.sql` should be committed to the repository. |
| 42 | + |
| 43 | +Finally, any changes to the schema should be reflected in the `tests/`. |
| 44 | +Please update the tests if necessary, or add new tests to cover the changes made in the schema. |
0 commit comments