Skip to content

Commit 256b454

Browse files
authored
Merge pull request #94 from nimblehq/bug/gh-91-move-db-migration-after-db-init
Fix: Move database migration right after the database initializing
2 parents 55cc93c + f7141b1 commit 256b454

File tree

9 files changed

+110
-58
lines changed

9 files changed

+110
-58
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Team
2-
# @junan is the Team Lead and the others are team members
3-
* @junan @carryall @hoangmirs @Lahphim @malparty
2+
# @carryall is the Team Lead and the others are team members
3+
* @carryall @hoangmirs @malparty @Nihisil @suphanatjarukulgowit
44

55
# Engineering Leads
66
CODEOWNERS @nimblehq/engineering-leads

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ Check this [wiki](https://github.com/nimblehq/gin-templates/wiki/Directories) fo
2626

2727
- Download **latest** version of gin-templates.
2828

29-
```
29+
```sh
3030
go get github.com/nimblehq/gin-templates
3131
```
3232

3333
- Build the binary file.
3434

35-
```
35+
```sh
3636
go build -o $GOPATH/bin/nimble-gin github.com/nimblehq/gin-templates
3737
```
3838

3939
- Create the project using gin-templates. Note that the **main** branch is being used by default. Refer to [this wiki page](https://github.com/nimblehq/gin-templates/wiki/Commands) for instructions on how to use a different branch.
4040

41-
```
41+
```sh
4242
nimble-gin create
4343
```
4444

cmd/create_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ var _ = Describe("Create template", func() {
124124
})
125125

126126
Context("given bootstrap/database.go", func() {
127-
It("contains project name at helpers import", func() {
127+
It("contains project name at database import", func() {
128128
cookiecutter := tests.Cookiecutter{AppName: "test-gin-templates"}
129129
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
130130

131131
content := tests.ReadFile("bootstrap/database.go")
132132

133-
expectedContent := `"github.com/nimblehq/test-gin-templates/helpers"`
133+
expectedContent := `"github.com/nimblehq/test-gin-templates/database"`
134134

135135
Expect(content).To(ContainSubstring(expectedContent))
136136
})
@@ -295,7 +295,7 @@ var _ = Describe("Create template", func() {
295295
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
296296
content := tests.ReadFile("go.mod")
297297

298-
expectedContent := "github.com/sirupsen/logrus v1.8.1"
298+
expectedContent := "github.com/sirupsen/logrus"
299299

300300
Expect(content).To(ContainSubstring(expectedContent))
301301
})
@@ -313,13 +313,26 @@ var _ = Describe("Create template", func() {
313313
Expect(content).To(ContainSubstring(expectedContent))
314314
})
315315

316-
It("contains logrus package import in bootstrap/database.go", func() {
316+
Context("given database/database.go", func() {
317+
It("contains project name at helpers import", func() {
318+
cookiecutter := tests.Cookiecutter{AppName: "test-gin-templates"}
319+
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
320+
321+
content := tests.ReadFile("database/database.go")
322+
323+
expectedContent := `"github.com/nimblehq/test-gin-templates/helpers"`
324+
325+
Expect(content).To(ContainSubstring(expectedContent))
326+
})
327+
})
328+
329+
It("contains logrus package import in database/database.go", func() {
317330
cookiecutter := tests.Cookiecutter{
318331
AppName: "test-gin-templates",
319332
UseLogrus: tests.Yes,
320333
}
321334
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
322-
content := tests.ReadFile("bootstrap/database.go")
335+
content := tests.ReadFile("database/database.go")
323336

324337
expectedContent := "github.com/nimblehq/test-gin-templates/helpers/log"
325338

{{cookiecutter.app_name}}/Dockerfile

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
FROM golang:1.20-buster
22

3-
ARG DATABASE_URL
4-
53
ENV GIN_MODE=release
64

75
WORKDIR /app
@@ -10,15 +8,9 @@ WORKDIR /app
108
COPY go.mod go.sum ./
119
RUN go mod download
1210

13-
# Install goose for migration
14-
RUN go get github.com/pressly/goose/cmd/goose
15-
1611
# Copy codebase
1712
COPY . .
1813

19-
# Run the migration
20-
RUN goose -dir database/migrations -table "migration_versions" postgres "$DATABASE_URL" up
21-
2214
# Build go application
2315
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main ./cmd/api
2416

{{cookiecutter.app_name}}/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ dev:
3434
forego start -f Procfile.dev
3535

3636
install-dependencies:
37-
go get github.com/cosmtrek/air@v1.15.1
38-
go get github.com/pressly/goose/cmd/goose
39-
go get github.com/ddollar/forego
37+
go install github.com/cosmtrek/air@v1.41.1
38+
go install github.com/pressly/goose/v3/cmd/goose@v3.9.0
39+
go install github.com/ddollar/forego@latest
4040
go mod tidy
4141
{% if cookiecutter._web_variant == "yes" %}npm install{% endif %}
4242

4343
test:
4444
docker-compose -f docker-compose.test.yml up -d
45-
ENV=test make db/migrate
45+
make wait-for-postgres
4646
go test -v -p 1 -count=1 ./...
4747
docker-compose -f docker-compose.test.yml down
4848

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package bootstrap
2+
3+
import (
4+
"github.com/nimblehq/{{cookiecutter.app_name}}/database"
5+
)
6+
7+
func Init() {
8+
LoadConfig()
9+
10+
InitDatabase(database.GetDatabaseURL())
11+
}
Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,9 @@
11
package bootstrap
22

33
import (
4-
"fmt"
5-
"strings"
6-
{% if cookiecutter.use_logrus == "no" %}"log"
7-
{% endif %}
8-
"github.com/nimblehq/{{cookiecutter.app_name}}/helpers"
9-
{% if cookiecutter.use_logrus == "yes" %}"github.com/nimblehq/{{cookiecutter.app_name}}/helpers/log"
10-
{% endif %}
11-
"github.com/gin-gonic/gin"
12-
"github.com/spf13/viper"
13-
"gorm.io/driver/postgres"
14-
"gorm.io/gorm"
4+
"github.com/nimblehq/{{cookiecutter.app_name}}/database"
155
)
166

17-
func InitDatabase() {
18-
db, err := gorm.Open(postgres.Open(getDatabaseURL()), &gorm.Config{})
19-
if err != nil {
20-
log.Fatalf("Failed to connect to %v database: %v", gin.Mode(), err)
21-
} else {
22-
viper.Set("database", db)
23-
log.Println(strings.Title(gin.Mode()) + " database connected successfully.")
24-
}
25-
}
26-
27-
func getDatabaseURL() string {
28-
if gin.Mode() == gin.ReleaseMode {
29-
return viper.GetString("DATABASE_URL")
30-
}
31-
32-
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
33-
viper.GetString("db_username"),
34-
viper.GetString("db_password"),
35-
viper.GetString("db_host"),
36-
helpers.GetStringConfig("db_port"),
37-
helpers.GetStringConfig("db_name"),
38-
)
7+
func InitDatabase(databaseURL string) {
8+
database.InitDatabase(databaseURL)
399
}

{{cookiecutter.app_name}}/cmd/api/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import (
1212
)
1313

1414
func main() {
15-
bootstrap.LoadConfig()
16-
17-
bootstrap.InitDatabase()
15+
bootstrap.Init()
1816

1917
r := bootstrap.SetupRouter()
2018

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package database
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
{% if cookiecutter.use_logrus == "no" %}"log"
7+
{% endif %}
8+
9+
"github.com/nimblehq/{{cookiecutter.app_name}}/helpers"
10+
{% if cookiecutter.use_logrus == "yes" %}"github.com/nimblehq/{{cookiecutter.app_name}}/helpers/log"
11+
{% endif %}
12+
13+
"github.com/gin-gonic/gin"
14+
"github.com/pressly/goose/v3"
15+
"github.com/spf13/viper"
16+
"gorm.io/driver/postgres"
17+
"gorm.io/gorm"
18+
)
19+
20+
var database *gorm.DB
21+
22+
func InitDatabase(databaseURL string) {
23+
db, err := gorm.Open(postgres.Open(databaseURL), &gorm.Config{})
24+
if err != nil {
25+
log.Fatalf("Failed to connect to %v database: %v", gin.Mode(), err)
26+
} else {
27+
viper.Set("database", db)
28+
log.Println(strings.Title(gin.Mode()) + " database connected successfully.")
29+
}
30+
31+
migrateDB(db)
32+
}
33+
34+
func migrateDB(db *gorm.DB) {
35+
sqlDB, err := db.DB()
36+
if err != nil {
37+
log.Fatalf("Failed to convert gormDB to sqlDB: %v", err)
38+
}
39+
40+
err = goose.Up(sqlDB, "database/migrations", goose.WithAllowMissing())
41+
if err != nil {
42+
log.Fatalf("Failed to migrate database: %v", err)
43+
}
44+
45+
log.Println("Migrated database successfully.")
46+
}
47+
48+
func GetDB() *gorm.DB {
49+
if database == nil {
50+
InitDatabase(GetDatabaseURL())
51+
}
52+
53+
return database
54+
}
55+
56+
func GetDatabaseURL() string {
57+
if gin.Mode() == gin.ReleaseMode {
58+
return viper.GetString("DATABASE_URL")
59+
}
60+
61+
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
62+
viper.GetString("db_username"),
63+
viper.GetString("db_password"),
64+
viper.GetString("db_host"),
65+
helpers.GetStringConfig("db_port"),
66+
helpers.GetStringConfig("db_name"),
67+
)
68+
}

0 commit comments

Comments
 (0)