- There are no values for iodine in the database.
- Several values may be incomplete. For instance, biotin is only listed for a handful of foods.
- Create a database, a corresponding user, and connect the two:
The last command is important for the population of the actual CNF database. When
> psql -U postgres psql> create database <foodie>; psql> create user <foodie> with encrypted password <password>; psql> grant all privileges on database <foodie> to <foodie>; psql> grant pg_read_server_files to "<foodie>";
psql
is running in Docker it may also be necessary to addAdditionally, at least one migration may require super-user access for the user, unless the extensionpsql>\c <foodie> foodie>grant all privileges on all tables in schema public to <foodie>;
uuid-ossp
is already defined. - Populate the CNF database by once running the script under
scripts/populate_cnf_db.sql
. To achieve that run the following steps:- Open a console in the project folder
> psql -U postgres
- It may be necessary to switch to the project folder in
psql
as well. This can be achieved withpsql> \cd <absolute path>;
. psql> \ir scripts/populate_cnf_db.sql
- In case of Docker:
- The script files are copied to
/tmp/scripts
viadocker-compose.yml
. - Connect to container
docker exec -it postgres /bin/bash
. - Change into
tmp
viacd /tmp
. - Run
psql -U <foodie-user> -d <foodie-database> -h db
, enter the password. - Run the script exactly as above.
- The script files are copied to
- Why not a migration?
There are several reasons for that:
- To
copy from
via SQL the user needs to be a superuser, which may be problematic. copy from
does not handle relative paths, i.e. one needs an absolute path. Absolute paths present an unnecessary constraint and impede development, and deployment.- In theory one may use a different population set or no population at all. Both work fine with an external population, but not with a migration.
- To
- The system scans for migrations in the folder
conf/db/migrations/default
and applies new ones. After a migration one should re-generate database related code:sbt slickGenerate
generates the base queries, and types.
Depending on the setup the commands below may need to be prefixed with sudo
.
- Start containers detached
docker compose up -d
- Connect to the container
docker compose run db bash
- Dump the database as insert statements (for better debugging):
pg_dump -h <container-name> -d <database-name> -U <user-name> --inserts -W > /tmp/<backup-file-name>.sql
. You will be prompted for the password for said user. Moving the file to/tmp
handles possible access issues. Even better - dump only the relevant tables by listing them with-t <table_name>
for each table (prefixed withpublic.
).pg_dump -h foodie-postgres -d foodie -t public.user -t public.session -t public.meal_entry -t public.meal -t public.complex_food -t public.recipe -t public.complex_ingredient -t public.recipe_ingredient -t public.reference_entry -t public.reference_map -U foodie --insert -W > /tmp/<date>.sql
- Find the id the desired container:
docker ps
- In a third CLI copy the backup file to your local file system:
docker cp <container-id>:/tmp/<backup-file-name>.sql <path-on-local-file-system>
For the moment the deployment is handled manually. There needs to be a running Docker service running on the target machine.
- Connect to the target machine.
- If this is the first deployment, clone Git project into a folder of your choosing.
- Change into the local Git repository of the project.
- Run
git pull
. It is possible that there will be a conflict with thedb.env
, if the development password has changed. - Make sure that
db.env
contains deployment values. - If this is the first deployment, create a file
deployment.env
containing all necessary environment variables (cf.application.conf
). - Make sure that all necessary environment variables are set in
deployment.env
, and these variables contain the desired deployment values. As a reference, one can use the.env
file - every key present in the.env
file needs to be set in thedeployment.env
as well. Caveat: Avoid#
symbols, because these can behave differently betweendotenv
and Docker. - Run
docker compose up
(possibly withsudo
). If you want to deploy a specific version, update the image fromlatest
to the desired tag in thedocker-compose.yml
. - If this is the first deployment,
connect to a bash in the Docker
db
container, and perform the database setup steps described above. - If this is the first deployment, restart the service (only the back end is sufficient). There is a tricky optimisation in the code: Some of the CNF tables are kept in memory to avoid queries (particularly in the case of statistics). However, the tables are loaded only once at the start of the service. Since the CNF database is initially empty, so are the memory values.