Skip to content

Developer Commands: Database

Mino Togna edited this page Oct 7, 2024 · 3 revisions

Developer Commands: Database

We develop against production copy of the database.
These commands expect you to have docker setup as explained in README.md#create-your-own-local-datbase

Copying a database dump to container

Database container expected to be located at ~/downloads/backup
When downloading the backup, the default name is {uuid}.
You can rename to backup with mv {uuid} backup

docker cp ~/downloads/backup fra-db:/

Restoring (production) copy

To restore a copy from backup, the backup is expected to be located in root of the container (/)

dropdb -f frap-dev --user frap
createdb --owner=frap --username=frap frap-dev 
pg_restore --no-owner --clean --if-exists --verbose -U frap -d frap-dev /backup

Redis cache

After restoring a local database, it is required to update the redis cache. run the following command:

ts-node ts-node src/tools/generateCache/index.ts

Creating and using snapshot to quickly reset DB

Many times when running migrations, you might want to roll back to initial state (eg. prod)
To avoid using the slow pg_restore you can use snapshots.

To create a snapshot:

docker exec -it fra-db bash
createdb -U frap -T frap-dev snapshot

This creates a snapshot called snapshot from the current state of DB frap-dev

To restore the snapshot, simply drop the db and switch the params:

docker exec -it fra-db bash
dropdb frap-dev -U frap
time createdb -U frap -T snapshot frap-dev

Script Helpers

There's an intellij/webstorm run configuration available: db:backup-import. it is a shell script running the following command:

(cd src/tools/db/importBackup && ./exec.sh) && ts-node src/tools/generateCache/index.ts