generated from nimblehq/git-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
973fa05
[gh-91] Move database matter to database folder
carryall c91172e
[gh-91] Remove migration run on start
carryall 50450a5
[gh-91] Fix test
carryall b08976f
[gh-91] Update code blocks in readme
carryall f7141b1
[gh-91] Add Alex and Best
carryall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 🙇🏼