Skip to content

WIP : this is a playground, code is messy, exploring various ideas...

Notifications You must be signed in to change notification settings

benjamin-thomas/golang-crud-example

Repository files navigation

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

  1. ./manage/destroy_db_container (optional)
  2. ./manage/create_db_container
  3. ./manage/create_db
  4. ./manage/seed_db
  5. ./manage/psql # test the connection
  6. [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

About

WIP : this is a playground, code is messy, exploring various ideas...

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published