From c3516dd184f4ed0222b1a967ca9552b001f309a5 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Tue, 4 Jan 2022 15:42:24 +0530 Subject: [PATCH] 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. --- .env.example | 4 ++++ app/boot_levels.go | 2 +- store/connect.go | 25 ++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 23cbb3253..374bcfaec 100644 --- a/.env.example +++ b/.env.example @@ -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? diff --git a/app/boot_levels.go b/app/boot_levels.go index 686391929..ba5c48cf7 100644 --- a/app/boot_levels.go +++ b/app/boot_levels.go @@ -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 } diff --git a/store/connect.go b/store/connect.go index 2d54582ba..6d7270783 100644 --- a/store/connect.go +++ b/store/connect.go @@ -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