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

[#91] Fix: Move database migration right after the database initializing #94

Merged
merged 5 commits into from
Mar 28, 2023
Merged
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
4 changes: 2 additions & 2 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Team
# @junan is the Team Lead and the others are team members
* @junan @carryall @hoangmirs @Lahphim @malparty
# @carryall is the Team Lead and the others are team members
* @carryall @hoangmirs @malparty @Nihisil @suphanatjarukulgowit

# Engineering Leads
CODEOWNERS @nimblehq/engineering-leads
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ Check this [wiki](https://github.com/nimblehq/gin-templates/wiki/Directories) fo

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

```
```sh
go get github.com/nimblehq/gin-templates
```

- Build the binary file.

```
```sh
go build -o $GOPATH/bin/nimble-gin github.com/nimblehq/gin-templates
```

- 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.

```
```sh
nimble-gin create
```

Expand Down
23 changes: 18 additions & 5 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ var _ = Describe("Create template", func() {
})

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

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

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

Expect(content).To(ContainSubstring(expectedContent))
})
Expand Down Expand Up @@ -296,7 +296,7 @@ var _ = Describe("Create template", func() {
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
content := tests.ReadFile("go.mod")

expectedContent := "github.com/sirupsen/logrus v1.8.1"
expectedContent := "github.com/sirupsen/logrus"

Expect(content).To(ContainSubstring(expectedContent))
})
Expand All @@ -314,13 +314,26 @@ var _ = Describe("Create template", func() {
Expect(content).To(ContainSubstring(expectedContent))
})

It("contains logrus package import in bootstrap/database.go", func() {
Context("given database/database.go", func() {
It("contains project name at helpers import", func() {
cookiecutter := tests.Cookiecutter{AppName: "test-gin-templates"}
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)

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

expectedContent := `"github.com/nimblehq/test-gin-templates/helpers"`

Expect(content).To(ContainSubstring(expectedContent))
})
})

It("contains logrus package import in database/database.go", func() {
cookiecutter := tests.Cookiecutter{
AppName: "test-gin-templates",
UseLogrus: tests.Yes,
}
cookiecutter.CreateProjectFromGinTemplate(currentTemplatePath)
content := tests.ReadFile("bootstrap/database.go")
content := tests.ReadFile("database/database.go")

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

Expand Down
8 changes: 0 additions & 8 deletions {{cookiecutter.app_name}}/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
FROM golang:1.18-buster

ARG DATABASE_URL

ENV GIN_MODE=release

WORKDIR /app
Expand All @@ -10,15 +8,9 @@ WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download

# Install goose for migration
RUN go get github.com/pressly/goose/cmd/goose

# Copy codebase
COPY . .

# Run the migration
RUN goose -dir database/migrations -table "migration_versions" postgres "$DATABASE_URL" up

# Build go application
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main ./cmd/api

Expand Down
8 changes: 4 additions & 4 deletions {{cookiecutter.app_name}}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ dev:
forego start -f Procfile.dev

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

test:
docker-compose -f docker-compose.test.yml up -d
ENV=test make db/migrate
make wait-for-postgres
go test -v -p 1 -count=1 ./...
docker-compose -f docker-compose.test.yml down

Expand Down
11 changes: 11 additions & 0 deletions {{cookiecutter.app_name}}/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bootstrap

import (
"github.com/nimblehq/{{cookiecutter.app_name}}/database"
)

func Init() {
LoadConfig()

InitDatabase(database.GetDatabaseURL())
}
36 changes: 3 additions & 33 deletions {{cookiecutter.app_name}}/bootstrap/database.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
package bootstrap

import (
"fmt"
"strings"
{% if cookiecutter.use_logrus == "no" %}"log"
{% endif %}
"github.com/nimblehq/{{cookiecutter.app_name}}/helpers"
{% if cookiecutter.use_logrus == "yes" %}"github.com/nimblehq/{{cookiecutter.app_name}}/helpers/log"
{% endif %}
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/nimblehq/{{cookiecutter.app_name}}/database"
)

func InitDatabase() {
db, err := gorm.Open(postgres.Open(getDatabaseURL()), &gorm.Config{})
if err != nil {
log.Fatalf("Failed to connect to %v database: %v", gin.Mode(), err)
} else {
viper.Set("database", db)
log.Println(strings.Title(gin.Mode()) + " database connected successfully.")
}
}

func getDatabaseURL() string {
if gin.Mode() == gin.ReleaseMode {
return viper.GetString("DATABASE_URL")
}

return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
viper.GetString("db_username"),
viper.GetString("db_password"),
viper.GetString("db_host"),
helpers.GetStringConfig("db_port"),
helpers.GetStringConfig("db_name"),
)
func InitDatabase(databaseURL string) {
database.InitDatabase(databaseURL)
}
4 changes: 1 addition & 3 deletions {{cookiecutter.app_name}}/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
)

func main() {
bootstrap.LoadConfig()

bootstrap.InitDatabase()
bootstrap.Init()

r := bootstrap.SetupRouter()

Expand Down
68 changes: 68 additions & 0 deletions {{cookiecutter.app_name}}/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package database

import (
"fmt"
"strings"
{% if cookiecutter.use_logrus == "no" %}"log"
{% endif %}

"github.com/nimblehq/{{cookiecutter.app_name}}/helpers"
{% if cookiecutter.use_logrus == "yes" %}"github.com/nimblehq/{{cookiecutter.app_name}}/helpers/log"
{% endif %}

"github.com/gin-gonic/gin"
"github.com/pressly/goose/v3"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

var database *gorm.DB

func InitDatabase(databaseURL string) {
db, err := gorm.Open(postgres.Open(databaseURL), &gorm.Config{})
if err != nil {
log.Fatalf("Failed to connect to %v database: %v", gin.Mode(), err)
} else {
viper.Set("database", db)
log.Println(strings.Title(gin.Mode()) + " database connected successfully.")
}

migrateDB(db)
}

func migrateDB(db *gorm.DB) {
sqlDB, err := db.DB()
if err != nil {
log.Fatalf("Failed to convert gormDB to sqlDB: %v", err)
}

err = goose.Up(sqlDB, "database/migrations", goose.WithAllowMissing())
if err != nil {
log.Fatalf("Failed to migrate database: %v", err)
}

log.Println("Migrated database successfully.")
}

func GetDB() *gorm.DB {
if database == nil {
InitDatabase(GetDatabaseURL())
}

return database
}

func GetDatabaseURL() string {
if gin.Mode() == gin.ReleaseMode {
return viper.GetString("DATABASE_URL")
}

return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
viper.GetString("db_username"),
viper.GetString("db_password"),
viper.GetString("db_host"),
helpers.GetStringConfig("db_port"),
helpers.GetStringConfig("db_name"),
Comment on lines +61 to +66
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we take this chance to drop the use of config/app.toml file? And change to use .env files instead.
With that file, we will get confused when adding new environment variables, as we don't know where we should put them

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As this PR is opened for so long, let's do it in another PR. I've created an issue to do it 🙇🏼

)
}