Allow version in DB_DSN for dev mode

If server is executed through Makefile we set BUILD_VERSION in .env and use it to replace {version} in DB_DSN.

This would help developers to automatically use different db when switching between versions.
This commit is contained in:
Vivek Patel
2022-01-07 13:04:17 +05:30
parent ff6cadc075
commit c3516dd184
3 changed files with 29 additions and 2 deletions
+4
View File
@@ -36,6 +36,10 @@ HTTP_LOG_REQUESTS=true
MONITOR_INTERVAL=5min
# Database to use
#
# If you are in development environment,
# you can use {version} with database name, that will be replaced with build version
# IE. DB_DSN=corteza:corteza@tcp(localhost:3306)/corteza_{version}?collation=utf8mb4_general_ci
DB_DSN=corteza:corteza@tcp(localhost:3306)/corteza?collation=utf8mb4_general_ci
# Log database queries?
+1 -1
View File
@@ -181,7 +181,7 @@ func (app *CortezaApp) InitStore(ctx context.Context) (err error) {
if app.Store == nil {
defer sentry.Recover()
app.Store, err = store.Connect(ctx, app.Opt.DB.DSN)
app.Store, err = store.Connect(ctx, app.Log, app.Opt.DB.DSN, app.Opt.Environment.IsDevelopment())
if err != nil {
return err
}
+24 -1
View File
@@ -3,6 +3,9 @@ package store
import (
"context"
"fmt"
"go.uber.org/zap"
"os"
"regexp"
"strings"
)
@@ -14,7 +17,27 @@ var (
registered = make(map[string]ConnectorFn)
)
func Connect(ctx context.Context, dsn string) (s Storer, err error) {
// Connect returns store based on dsn from environment.
//
// If you are in development environment,
// you can use {version} with database name, that will be replaced with build version
// suppose build version is `20xx.x.x-dev-1` then database_name_{version} will be `database_name_20xx_x_x_dev_1`,
//
// IE. `BUILD_VERSION=20xx.x.x-dev-1` and
// `DB_DSN=corteza:corteza@tcp(localhost:3306)/corteza_{version}?collation=utf8mb4_general_ci`
// will be `DB_DSN=corteza:corteza@tcp(localhost:3306)/corteza_20xx_x_x_dev_1?collation=utf8mb4_general_ci`
func Connect(ctx context.Context, log *zap.Logger, dsn string, isDevelopment bool) (s Storer, err error) {
if isDevelopment {
if strings.Contains(dsn, "{version}") {
log.Warn("You're using DB_DSN with {version}, It is still in EXPERIMENTAL phase")
log.Warn("Should be used only for development mode")
log.Warn("You may experience instability")
}
expr := regexp.MustCompile(`[.\-]+`)
version := expr.ReplaceAllString(os.Getenv("BUILD_VERSION"), "_")
dsn = strings.Replace(dsn, "{version}", version, 1)
}
var storeType = strings.SplitN(dsn, "://", 2)[0]
if storeType == "" {
// Backward compatibility