Skip to content

Commit 8dfe56a

Browse files
authored
Merge pull request #28 from nimblehq/feature/migration
Setup migration
2 parents 22b8cb9 + 80f834b commit 8dfe56a

File tree

9 files changed

+110
-7
lines changed

9 files changed

+110
-7
lines changed

cmd/create_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ var _ = Describe("Create template", func() {
2828
})
2929
})
3030

31+
Context("given .env.example", func() {
32+
It("contains project name at DATABASE_URL", func() {
33+
content := tests.ReadFile(".env.example")
34+
35+
fileContainProjectName := strings.Contains(content, "DATABASE_URL=postgres://postgres:postgres@0.0.0.0:5432/test-gin-templates_development?sslmode=disable")
36+
37+
Expect(fileContainProjectName).To(BeTrue())
38+
})
39+
})
40+
41+
Context("given .env.test", func() {
42+
It("contains project name at DATABASE_URL", func() {
43+
content := tests.ReadFile(".env.test")
44+
45+
fileContainProjectName := strings.Contains(content, "DATABASE_URL=postgres://postgres:postgres@0.0.0.0:5433/test-gin-templates_test?sslmode=disable")
46+
47+
Expect(fileContainProjectName).To(BeTrue())
48+
})
49+
})
50+
3151
Context("given docker-compose.dev.yml", func() {
3252
It("contains project name at container_name", func() {
3353
content := tests.ReadFile("docker-compose.dev.yml")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DATABASE_URL=postgres://postgres:postgres@0.0.0.0:5432/{{cookiecutter.app_name}}_development?sslmode=disable

{{cookiecutter.app_name}}/.env.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DATABASE_URL=postgres://postgres:postgres@0.0.0.0:5433/{{cookiecutter.app_name}}_test?sslmode=disable

{{cookiecutter.app_name}}/.github/workflows/test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ jobs:
1414
with:
1515
go-version: 1.16.x
1616

17+
- name: Create ENV file
18+
run: cp ".env.example" ".env"
19+
1720
- name: Linters
1821
uses: golangci/golangci-lint-action@v2
1922
with:
2023
version: v1.29
2124

25+
- name: Install dependencies
26+
run: make install-dependencies
27+
2228
- name: Test
2329
run: make test

{{cookiecutter.app_name}}/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
tmp/
2+
.env

{{cookiecutter.app_name}}/Makefile

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,48 @@
1-
.PHONY: env-setup env-teardown dev install-dependencies test
1+
include .env
2+
ifdef ENV
3+
include .env.$(ENV)
4+
endif
5+
6+
.PHONY: env-setup env-teardown db/migrate db/rollback migration/create migration/status dev install-dependencies test wait-for-postgres
27

38
env-setup:
49
docker-compose -f docker-compose.dev.yml up -d
510

611
env-teardown:
712
docker-compose -f docker-compose.dev.yml down
813

9-
dev: env-setup
14+
db/migrate:
15+
make wait-for-postgres
16+
goose -dir database/migrations -table "migration_versions" postgres "$(DATABASE_URL)" up
17+
18+
db/rollback:
19+
make wait-for-postgres
20+
goose -dir database/migrations -table "migration_versions" postgres "$(DATABASE_URL)" down
21+
22+
migration/create:
23+
ifndef MIGRATION_NAME
24+
$(error MIGRATION_NAME is required)
25+
endif
26+
goose -dir database/migrations create $(MIGRATION_NAME) sql
27+
28+
migration/status:
29+
goose -dir database/migrations -table "migration_versions" postgres "$(DATABASE_URL)" status
30+
31+
dev:
32+
make env-setup
33+
make db/migrate
1034
air -c cmd/api/.air.toml
1135

1236
install-dependencies:
1337
go get github.com/cosmtrek/air@v1.15.1
38+
go get github.com/pressly/goose/cmd/goose
1439
go mod tidy
1540

1641
test:
1742
docker-compose -f docker-compose.test.yml up -d
18-
BRANCH=$(BRANCH) go test -v -p 1 -count=1 ./...
43+
ENV=test make db/migrate
44+
go test -v -p 1 -count=1 ./...
1945
docker-compose -f docker-compose.test.yml down
46+
47+
wait-for-postgres:
48+
$(shell DATABASE_URL=$(DATABASE_URL) ./bin/wait-for-postgres.sh)

{{cookiecutter.app_name}}/README.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,28 @@
1212

1313
### Development
1414

15+
#### Create an ENV file
16+
17+
To start the development server, `.env` file must be created.
18+
19+
- Copy `.env.example` file and rename to `.env`
20+
1521
#### Build dependencies
16-
[`air`](https://github.com/cosmtrek/air) is used for live reloading. It needs to be built as a binary file in `$GOPATH`.
1722

23+
- [`air`](https://github.com/cosmtrek/air) is used for live reloading
24+
25+
- [`goose`](https://github.com/pressly/goose) is used for database migration.
26+
27+
They need to be built as a binary file in `$GOPATH`.
1828

19-
```sh
29+
30+
```make
2031
make install-dependencies
2132
```
2233

2334
#### Start development server
2435

25-
```sh
36+
```make
2637
make dev
2738
```
2839

@@ -32,6 +43,32 @@ The application runs locally at http://localhost:8080
3243

3344
Execute all unit tests:
3445

35-
```sh
46+
```make
3647
make test
3748
```
49+
50+
### Migration
51+
52+
#### Create migration
53+
54+
```make
55+
make migration/create MIGRATION_NAME={migration name}
56+
```
57+
58+
#### List the migration status
59+
60+
```make
61+
make migration/status
62+
```
63+
64+
#### Migrate the database
65+
66+
```make
67+
make db/migrate
68+
```
69+
70+
#### Rollback the migration
71+
72+
```make
73+
make db/rollback
74+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
until psql $DATABASE_URL -c "\q" > /dev/null 2>&1; do
6+
>&2 echo "Postgres is unavailable - sleeping"
7+
sleep 1
8+
done

0 commit comments

Comments
 (0)