-
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
Setup database #20
Setup database #20
Conversation
a82da6e
to
8bc695e
Compare
653c7cf
to
9d35a28
Compare
"github.com/spf13/viper" | ||
) | ||
|
||
func GetConfigPrefix() string { |
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.
Since this method is only used internally, how about not exporting it?
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.
I export this function because I want to write unit test. config_test
's package is helpers_test
.
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.
I saw that but I am not sure of the value-added to have such test as it will be implicitly tested in the other public methods.
"gorm.io/gorm" | ||
) | ||
|
||
// Get database connection from viper config |
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.
Why separating this from bootstrap/database.go
? Couldn't this be together? I don't see the point of having a helper for that 🤔 Also note that I did not see where GetDBConnection
is actually used. I would assume that InitDatabase
would use it but it's not. So for now, it seems like an unused method.
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.
Gorm does not have any function to get database connection after initialize so I need to create a function to get database connection from viper config. I think bootstrap
should contains only initializer stuffs. That's why I decide to put GetDBConnection
function at helpers. But yes this function is unused for now, I will remove this in this PR and will put it again in the PR that need to use database connection.
Removed in 28bb423
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.
Helpers use should be minimized. It should not be your go-to place for code 🙈 bootstrap
should indeed contain all initialization code but that also include any secondary methods like such helpers too. Keeping the implementation code close to where the methods are declared is a best practice. Especially, in this case, the "helper" method does not need to be shared as it will be used only in bootstrap
and nowhere else.
85a5a08
to
07a2823
Compare
4102249
to
0e97e61
Compare
What happened 👀
Resolve #4
env-setup
andenv-teardown
make command to run and stop database container.toml
config file withdev
andtest
database url.gorm
as an ORM.Insight 📝
I choose Viper because Viper is one of the most popular config management tools that support TOML format as TOML widely use for config type in Go.
You can get value from toml config file using viper as following.
You can get value using viper like this
How about
release
environment?Since this release is focusing only Heroku, all env on Heroku doesn't contain prefix and TOML file doesn't support environment injection so I need to create function
GetConfigPrefix
vary by gin.Mode.You can get the config value using helpers function for configs that depend on
gin.Mode()
and can use viper directly for config that not depend ongin.Mode()
.Proof Of Work 📹
Run development server (This will read database url from
debug.database_url
inconfig/app.toml
)Run test (This will read database url from
test.database_url
inconfig/app.toml
)Run application with release environment without provide
DATABASE_URL
Run application with release environment with
DATABASE_URL