Building the makefile
A makefile is a file that is used by the make
utility; it contains a set of tasks consisting of different combined shell scripts. Makefiles are most used to perform operations such as compiling source code, installing executables, performing checks, and many more. The make
utility is available for both macOS and Linux, while in Windows, you need to use Cygwin (https://www.cygwin.com/) or NMake (https://docs.microsoft.com/en-us/cpp/build/reference/nmake-reference).
We will create the makefile to automate the steps that we have performed in this chapter. This will make it easy to do the process repetitively when required without typing it manually. We are going to create a makefile that will do tasks such as the following:
- Bringing up/down Postgres
- Generating code using sqlc
The makefile can be seen in the chapter1
directory; the following shows a snippet of the script:
.. .PHONY : postgresup postgresdown psql createdb teardown_recreate generate postgresup: docker run --name test-postgres -v $(PWD):/usr/share/chapter1 -e POSTGRES_PASSWORD=$(DB_PWD) -p 5432:5432 -d $(DB_NAME) ... # task to create database without typing it manually createdb: docker exec -it test-postgres psql $(PSQLURL) -c "\i /usr/share/chapter1/db/schema.sql" ...
With the makefile, you can now bring up the database easily using this command:
make postgresup
The following is used to bring down the database:
make postgresdown
sqlc will need to be invoked to regenerate the auto-generated code whenever changes are made to the schema and SQL queries. You can use the following command to regenerate the files:
make generate