go-bank
go-bank copied to clipboard
Backend system for a simple banking application developed to be used as a boilerplate for best practices around developing and working with golang based API's, GORM with SQLC and PostgresSQL as a data...
trafficstars
A Simple Bank
Features
- Create and manage account
- Owner
- Balance
- Currency
- Record all balance changes for each account
- Create an account entry for each change for each account
- Money transfer transaction
- Perform money transfer between 2 accounts consistently within a transaction
Pre-requisites
- Install
docker for desktop - Execute
brew install golang-migrate sqlc - Execute
go install github.com/golang/mock/[email protected]
Database Design
- Design DB schema using dbdiagram.io
- Export the queries onto
/dbdiagrams.io
- Export the queries onto
- Save and share DB diagram within the team
- Generate SQL code to create database in a target database engine i.e. postgres/MySQL/SQLServer
Docker and Postgres
- Execute
docker pull postgres:12-alpineto get the postgres image - Execute
docker run --name postgres12 -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:12-alpineto run the postgres container - Execute
docker logs postgres12to see the logs - Execute
docker exec -it postgres12 psql -U rootto connect to the postgres container and login asrootuser - Connect to postgres container and execute the queries from
/dbdiagrams.ioto create the tables
DB migration
- Execute
migrate -versionto verify that thegolang-migratehas been installed - Execute
migrate create -ext sql -dir db/migration -seq init_schemato generate migration files*.up.sqlis used to migrate up to a new version usingmigrate up*.down.sqlis used to migrate down to an older version usingmigrate down- Copy the sql quesries generated from
dbdiagram.ioto*.up.sql - Add
DROP TABLEqueries to*.down.sql
- Execute
make migrateupto migrate data upwards to a new version - Execute
make migratedownto revert migration to a previous version - Manage migrations in future with
migrtion up/downcommands
DB and Docker Setup for development
- Execute
make postgresto run postgres container on local docker setup - Execute
make createdbto create thesimple_bankdatabase - Execute
make migrateupto setup tables and initial database state - If required,
- Execute
make dropdbto drop database - Execute
make migratedownto migrate or revert database state to a previous version
- Execute
Generate CRUD Golang code from SQL
- Execute
make sqlcto auto generate CRUD functionalities - Execute
make mockto generate mock DB