Convenience Scripts¶
These are designed to make it easier to perform various everyday tasks in the project. They try to be transparent by exposing the underlying commands they execute so that users can have an idea of what's happening and try to learn the commands if they wish.
Scripts¶
scripts/
├── buildrun.sh
├── check-migrations.sh
├── createsuperuser.sh
├── db.sh
├── erd.sh
├── lint.sh
├── loadenv.sh
├── logs.sh
├── migrate.sh
├── precommit-check.sh
├── run.sh
├── start-local.sh
├── test.sh
└── update-dependencies.sh
These scripts assume you are using bash.
-
buildrun.sh - clean, build, and run containers in background mode
- Pass in
-v
to remove data volume, which resets the local database. - See the script file for more options.
- Pass in
-
check-migrations.sh - check if migrations are up to date
-
createsuperuser.sh - create a default superuser
- This assumes that
DJANGO_SUPERUSER_USERNAME
andDJANGO_SUPERUSER_PASSWORD
are set in.env.docker
- This assumes that
-
db.sh - connect to the database in the
db
container- This is a different route than
manage.py dbshell
, which requires thepsql
executable in theweb
container
- This is a different route than
-
erd.sh - generate ER diagram
- The image is saved to
app/erd.png
- This script is dependent on the
graphviz
package
- The image is saved to
-
lint.sh - lint and and auto-format code
-
logs.sh - view/tail container logs
-
migrate.sh - run database migrations inside container
- Add
<app> <migration_number>
to migrate to that database state. Ex:migrate.sh core 0010
- Add
-
precommit-check.sh - sanity checks before committing code
- Call
buildrun.sh
,lint.sh
, andtest.sh
- Call
-
run.sh - start the development server in Docker, with some options
- Pass in
-h
to show usage
- Pass in
-
shell.sh - open a shell on the terminal
- Pass in
-h
to show usage
- Pass in
-
test.sh - run tests and generate test coverage report
- Use the
-k
flag to filter tests. For exampletest.sh -k program_area
will select only tests with "program_area" in the name. - use
--help
to see other script options. - use
--help-pytest
to see pytest options that can be added.
- Use the
-
update-dependencies.sh - update python dependencies to the latest versions
Script Elements¶
Header¶
set -euo pipefail
is a combination of the followingset -e
exits the script immediately if a command failsset -u
exits the script if an undefined variable is usedset -o pipefail
exits the script if a pipe command fails
IFS=$'\n\t'
sets the internal field separator to newlines and tabs
Why do we use this?
- This was the header to set up what's known as the unofficial bash strict mode, which is supposed to help people write better bash scripts.
- Some of it is outdated like the reasoning to use
IFS=$'\n\t'
is solved by theshellcheck
tool.- There are posts warning of
set -e
not being safe in some cases. - The
IFS
setting may not be needed since we use shellcheck to make sure variables are quoted properly. - More link
- There are posts warning of
Debugging¶
We use set -x
pairs to enable debug mode, which prints the commands that are executed in between them. The idea is to expose the underlying commands so we can learn them if we want to.
set -x
enables debug modeset +x
disables debug mode. Use{ set +x; } 2>&-;
to hide theset +x
from echoing to the terminal. It executes in a subshell to disable debug mode and redirects stderr to/dev/null
- The disable command is not necessary if the script ends right away.
SCRIPT_DIR¶
We use SCRIPT_DIR="$(dirname "$0")"
to get the directory of the script file, which is useful for calling other scripts in the same directory.
Extra arguments¶
-
"$@"
contains all the arguments to the script -
"$1"
is the first argument -
For example of reading commandline options, see
run.sh
andtest.sh
.