File tree Expand file tree Collapse file tree 9 files changed +110
-7
lines changed
{{cookiecutter.app_name}} Expand file tree Collapse file tree 9 files changed +110
-7
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,26 @@ var _ = Describe("Create template", func() {
28
28
})
29
29
})
30
30
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
+
31
51
Context ("given docker-compose.dev.yml" , func () {
32
52
It ("contains project name at container_name" , func () {
33
53
content := tests .ReadFile ("docker-compose.dev.yml" )
Original file line number Diff line number Diff line change
1
+ DATABASE_URL = postgres://postgres:postgres@0.0.0.0:5432/{{cookiecutter.app_name}}_development?sslmode=disable
Original file line number Diff line number Diff line change
1
+ DATABASE_URL = postgres://postgres:postgres@0.0.0.0:5433/{{cookiecutter.app_name}}_test?sslmode=disable
Original file line number Diff line number Diff line change @@ -14,10 +14,16 @@ jobs:
14
14
with :
15
15
go-version : 1.16.x
16
16
17
+ - name : Create ENV file
18
+ run : cp ".env.example" ".env"
19
+
17
20
- name : Linters
18
21
uses : golangci/golangci-lint-action@v2
19
22
with :
20
23
version : v1.29
21
24
25
+ - name : Install dependencies
26
+ run : make install-dependencies
27
+
22
28
- name : Test
23
29
run : make test
Original file line number Diff line number Diff line change 1
1
tmp /
2
+ .env
Original file line number Diff line number Diff line change 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
2
7
3
8
env-setup :
4
9
docker-compose -f docker-compose.dev.yml up -d
5
10
6
11
env-teardown :
7
12
docker-compose -f docker-compose.dev.yml down
8
13
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
10
34
air -c cmd/api/.air.toml
11
35
12
36
install-dependencies :
13
37
go get github.com/cosmtrek/air@v1.15.1
38
+ go get github.com/pressly/goose/cmd/goose
14
39
go mod tidy
15
40
16
41
test :
17
42
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 ./...
19
45
docker-compose -f docker-compose.test.yml down
46
+
47
+ wait-for-postgres :
48
+ $(shell DATABASE_URL=$(DATABASE_URL ) ./bin/wait-for-postgres.sh)
Original file line number Diff line number Diff line change 12
12
13
13
### Development
14
14
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
+
15
21
#### 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 ` .
17
22
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 ` .
18
28
19
- ``` sh
29
+
30
+ ``` make
20
31
make install-dependencies
21
32
```
22
33
23
34
#### Start development server
24
35
25
- ``` sh
36
+ ``` make
26
37
make dev
27
38
```
28
39
@@ -32,6 +43,32 @@ The application runs locally at http://localhost:8080
32
43
33
44
Execute all unit tests:
34
45
35
- ``` sh
46
+ ``` make
36
47
make test
37
48
```
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
+ ```
Original file line number Diff line number Diff line change
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
File renamed without changes.
You can’t perform that action at this time.
0 commit comments