Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BD-15: Integrating SQL-migrate #22

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
include .env
MIGRATE=docker-compose exec web migrate -path=migration -database "mysql://${DB_USERNAME}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_NAME}" -verbose
export
RUNNER=docker-compose exec web sql-migrate

ifeq ($(p),host)
RUNNER=sql-migrate
endif

MIGRATE=$(RUNNER)

dev:
gin appPort ${SERVER_PORT} -i run server.go
gin appPort ${ServerPort} -i run server.go

migrate-status:
$(MIGRATE) status

migrate-up:
$(MIGRATE) up

migrate-down:
$(MIGRATE) down

redo:
@read -p "Are you sure to reapply the last migration? [y/n]" -n 1 -r; \
if [[ $$REPLY =~ ^[Yy] ]]; \
then \
$(MIGRATE) redo; \
fi

force:
@read -p "Which version do you want to force?" VERSION; \
$(MIGRATE) force $$VERSION
Expand All @@ -20,10 +40,10 @@ drop:

create:
@read -p "What is the name of migration?" NAME; \
${MIGRATE} create -ext sql -seq -dir migration $$NAME
$(MIGRATE) new $$NAME

crud:
bash automate/scripts/crud.sh
bash automate/scripts/crud.sh

.PHONY: migrate-up migrate-down force goto drop create

Expand Down
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Boilerplate API template includes all the common packages and setup used for API
### Development

- Copy `.env.example` to `.env` and update according to requirement.
- Create `serviceAccountKey.json` file for firebase admin sdk.
- Create `serviceAccountKey.json` file for firebase admin sdk and copy serviceAccountKey.json.example file content for basic setup.
- To run `docker-compose up` (with default configuration will run at `5000` and adminer runs at `5001`)

#### Run Boilerplate CLI 🖥
Expand All @@ -16,15 +16,16 @@ Boilerplate API template includes all the common packages and setup used for API

#### Migration Commands 🛳

| Command | Desc |
| ------------------- | ---------------------------------------------- |
| `make migrate-up` | runs migration up command |
| `make migrate-down` | runs migration down command |
| `make force` | Set particular version but don't run migration |
| `make goto` | Migrate to particular version |
| `make drop` | Drop everything inside database |
| `make create` | Create new migration file(up & down) |
| `make crud` | Create crud template |
| Command | Desc |
| ---------------------------- | ---------------------------------------------- |
| `make migrate-status` | See status for migration file status |
| `make create` | Create new migration file(up & down) |
| `make migrate-up` | runs migration up command |
| `make migrate-down` | runs migration down command |
| `make force` | Set particular version but don't run migration |
| `make goto` | Migrate to particular version |
| `make drop` | Drop everything inside database |
| `make crud` | Create CRUD template |

### Implemented Feature

Expand Down
6 changes: 2 additions & 4 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ func bootstrap(
logger.Zap.Info("------------------------")
logger.Zap.Info("------ Boilerplate 📺 ------")
logger.Zap.Info("------------------------")

logger.Zap.Info("Migrating DB schema...")
go func() {
if env.Environment == "production"{
migrations.Migrate()
}
logger.Zap.Info("Migrating DB schema...")
migrations.Migrate()
middlewares.Setup()
routes.Setup()
logger.Zap.Info("🌱 seeding data...")
Expand Down
9 changes: 9 additions & 0 deletions dbConfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
development:
dialect: mysql
datasource: ${DB_USERNAME}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_NAME}?parseTime=true
dir: migration/

production:
dialect: mysql
datasource: ${DB_USERNAME}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_NAME}?parseTime=true
dir: migration/
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
volumes:
- .:/clean_web
env_file: .env
container_name: boilerplate-web
container_name: clean-web
depends_on:
- database

Expand All @@ -28,17 +28,17 @@ services:
[
"--character-set-server=utf8mb4",
"--collation-server=utf8mb4_unicode_ci",
"--default-authentication-plugin=mysql_native_password",
"--default-authentication-plugin=mysql_native_password"
]
ports:
- 33066:3306
volumes:
- boilerplate_db:/var/lib/mysql
- clean_db:/var/lib/mysql

adminer:
image: adminer
ports:
- ${ADMINER_PORT}:8080

volumes:
boilerplate_db:
clean_db:
16 changes: 2 additions & 14 deletions docker/web.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,9 @@ RUN echo $GOPATH

RUN go install github.com/go-delve/delve/cmd/dlv@latest

COPY . /clean_web

ARG VERSION="4.13.0"

RUN set -x \
&& apk add --no-cache git \
&& git clone --branch "v${VERSION}" --depth 1 --single-branch https://github.com/golang-migrate/migrate /tmp/go-migrate
RUN go install github.com/rubenv/sql-migrate/...@latest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use go install in one line to install multiple go packages


WORKDIR /tmp/go-migrate

RUN set -x \
&& CGO_ENABLED=0 go build -tags 'mysql' -ldflags="-s -w" -o ./migrate ./cmd/migrate \
&& ./migrate -version

RUN cp /tmp/go-migrate/migrate /usr/bin/migrate
COPY . /clean_web

WORKDIR /clean_web

Expand Down
14 changes: 8 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ require (
github.com/gin-gonic/gin v1.8.1
github.com/go-playground/validator/v10 v10.11.1
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/google/uuid v1.3.0
github.com/joho/godotenv v1.4.0
github.com/manifoldco/promptui v0.9.0
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/pkg/errors v0.9.1
github.com/rubenv/sql-migrate v1.3.0
github.com/spf13/viper v1.14.0
go.uber.org/fx v1.18.2
go.uber.org/zap v1.24.0
golang.org/x/oauth2 v0.3.0
golang.org/x/text v0.5.0
golang.org/x/text v0.6.0
google.golang.org/api v0.104.0
gorm.io/driver/mysql v1.4.4
gorm.io/gorm v1.24.2
Expand Down Expand Up @@ -53,14 +55,14 @@ require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-gorp/gorp/v3 v3.0.2 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand All @@ -72,7 +74,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -88,10 +90,10 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/dig v1.15.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be // indirect
golang.org/x/net v0.3.0 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
Loading