Skip to content

Commit

Permalink
chore: expose getter/setter for DSN envars (#1444)
Browse files Browse the repository at this point in the history
This centralises the DSN envar name format, and hopefully makes it
easier to move away from envars soon.
  • Loading branch information
matt2e authored May 9, 2024
1 parent 5476984 commit 53fa41c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
7 changes: 3 additions & 4 deletions go-runtime/ftl/ftltest/ftltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,9 @@ func WithSecret[T ftl.SecretType](secret ftl.SecretValue[T], value T) Option {
// )
func WithDatabase(dbHandle ftl.Database) Option {
return func(ctx context.Context, state *OptionsState) error {
envarName := fmt.Sprintf("FTL_POSTGRES_DSN_%s_%s", strings.ToUpper(ftl.Module()), strings.ToUpper(dbHandle.Name))
originalDSN, ok := os.LookupEnv(envarName)
if !ok {
return fmt.Errorf("missing DSN for database %s: expected to find it at the environment variable %s", dbHandle.Name, envarName)
originalDSN, err := modulecontext.GetDSNFromEnvar(ftl.Module(), dbHandle.Name)
if err != nil {
return err
}

// convert DSN by appending "_test" to table name
Expand Down
3 changes: 2 additions & 1 deletion integration/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
ftlexec "github.com/TBD54566975/ftl/internal/exec"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/modulecontext"
"github.com/TBD54566975/scaffolder"
)

Expand Down Expand Up @@ -271,7 +272,7 @@ func createDBAction(module, dbName string, isTest bool) action {

func createDB(t testing.TB, module, dbName string, isTestDb bool) {
// envars do not include test suffix
t.Setenv(fmt.Sprintf("FTL_POSTGRES_DSN_%s_%s", strings.ToUpper(module), strings.ToUpper(dbName)),
t.Setenv(modulecontext.DSNEnvarName(module, dbName),
fmt.Sprintf("postgres://postgres:secret@localhost:54320/%s?sslmode=disable", dbName))

// insert test suffix if needed when actually setting up db
Expand Down
17 changes: 17 additions & 0 deletions internal/modulecontext/from_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,20 @@ func DatabasesFromEnvironment(ctx context.Context, module string) (map[string]Da
}
return databases, nil
}

// DSNEnvarName returns the name of the environment variable that is expected to hold the DSN for a database.
//
// The format is FTL_POSTGRES_DSN_<MODULE>_<DBNAME>
func DSNEnvarName(module, name string) string {
return fmt.Sprintf("FTL_POSTGRES_DSN_%s_%s", strings.ToUpper(module), strings.ToUpper(name))
}

// GetDSNFromEnvar returns the DSN for a database from an environment variable.
func GetDSNFromEnvar(module, name string) (string, error) {
envarName := DSNEnvarName(module, name)
dsn, ok := os.LookupEnv(envarName)
if !ok {
return "", fmt.Errorf("missing DSN for database %s: expected to find it at the environment variable %s", name, envarName)
}
return dsn, nil
}
3 changes: 1 addition & 2 deletions internal/modulecontext/from_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
func TestFromEnvironment(t *testing.T) {
ctx := log.ContextWithNewDefaultLogger(context.Background())

t.Setenv("FTL_POSTGRES_DSN_ECHO_ECHO", "postgres://echo:echo@localhost:5432/echo")

t.Setenv(DSNEnvarName("echo", "echo"), "postgres://echo:echo@localhost:5432/echo")
databases, err := DatabasesFromEnvironment(ctx, "echo")
assert.NoError(t, err)

Expand Down

0 comments on commit 53fa41c

Please sign in to comment.