golang-crud-example
golang-crud-example copied to clipboard
WIP : this is a playground, code is messy, exploring various ideas...
golang CRUD example
Prerequisite
Docker
set up the env vars below in this file ~/.env/golang-crud-example
env | grep ^PG
PGPASSWORD=my_password
PGHOST=localhost
PGPORT=5432
PGUSER=postgres
PGDATABASE=golang_crud_example_dev
Easier setup from scratch
- ./manage/destroy_db_container (optional)
- ./manage/create_db_container
- ./manage/create_db
- ./manage/seed_db
- ./manage/psql # test the connection
- [HTTP_SOCKET=localhost:8080] ./manage/run
Miscellaneous
Migrations
Reset the db
./manage/reset_db
Run one migration
./manage/psql-pipe -a < ./db/migrations/up/1.sql
Revert one migration
./manage/psql-pipe -a < ./db/migrations/down/1.sql
Run multiple migrations
cat ./db/migrations/down/{02..01}.sql | ./manage/psql-pipe -a
cat ./db/migrations/up/{01..02}.sql | ./manage/psql-pipe -a
or
find ./db/migrations/down/ -type f -name "*.sql" | sort -rn | xargs cat | ./manage/psql-pipe
find ./db/migrations/up/ -type f -name "*.sql" | sort -n | xargs cat | ./manage/psql-pipe
Get current migration version
./manage/psql-pipe -c "SELECT max(version) FROM migrations"
3. Dump the schema
./manage/pg_dump -s $PGDATABASE >./db/schema.sql
Develop queries
cat ./queries/addresses.sql | ./manage/psql-pipe