diff --git a/fxsql/README.md b/fxsql/README.md index 77f3edcb..97d22a97 100644 --- a/fxsql/README.md +++ b/fxsql/README.md @@ -114,6 +114,16 @@ modules: - "connection:ping" ``` +For security reasons, you should avoid to hardcode DSN sensible parts (like the password) in your config files, you can use the [env vars placeholders](https://github.com/ankorstore/yokai/tree/main/fxconfig#configuration-env-var-placeholders) instead: + +```yaml +# ./configs/config.yaml +modules: + sql: + driver: mysql + dsn: "${MYSQL_USER}:${MYSQL_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/${MYSQL_DATABASE}?parseTime=True" +``` + Available SQL operations: - `connection:begin` @@ -331,4 +341,6 @@ modules: dsn: ":memory:" ``` +In `test` mode, the module won't automatically close the database connection on shutdown, to allow database manipulation after the `RunTest()` execution. + You can find tests examples in this module own [tests](module_test.go). \ No newline at end of file diff --git a/fxsql/module.go b/fxsql/module.go index 5576b19c..425a4bb1 100644 --- a/fxsql/module.go +++ b/fxsql/module.go @@ -87,7 +87,7 @@ func NewFxSQLDatabase(p FxSQLDatabaseParam) (*sql.DB, error) { // lifecycles p.LifeCycle.Append(fx.Hook{ OnStop: func(_ context.Context) error { - if yokaisql.FetchSystem(p.Config.GetString("modules.sql.driver")) != yokaisql.SqliteSystem { + if !p.Config.IsTestEnv() { return db.Close() } diff --git a/fxsql/module_test.go b/fxsql/module_test.go index e491903d..9f3bd884 100644 --- a/fxsql/module_test.go +++ b/fxsql/module_test.go @@ -23,6 +23,7 @@ import ( ) func TestModule(t *testing.T) { + t.Setenv("APP_ENV", "test") t.Setenv("APP_CONFIG_PATH", "testdata/config") t.Setenv("SQL_DRIVER", "sqlite") t.Setenv("SQL_DSN", ":memory:") @@ -193,9 +194,13 @@ func TestModuleWithMigrationShutdown(t *testing.T) { err := app.Start(ctx) assert.NoError(t, err) + + err = app.Stop(ctx) + assert.NoError(t, err) } func TestModuleErrorWithInvalidDriver(t *testing.T) { + t.Setenv("APP_ENV", "test") t.Setenv("APP_CONFIG_PATH", "testdata/config") t.Setenv("SQL_DRIVER", "invalid") @@ -222,6 +227,7 @@ func TestModuleErrorWithInvalidDriver(t *testing.T) { } func TestModuleErrorWithInvalidDsn(t *testing.T) { + t.Setenv("APP_ENV", "test") t.Setenv("APP_CONFIG_PATH", "testdata/config") t.Setenv("SQL_DRIVER", "mysql") t.Setenv("SQL_DSN", "invalid") @@ -249,6 +255,7 @@ func TestModuleErrorWithInvalidDsn(t *testing.T) { } func TestModuleErrorWithInvalidSeed(t *testing.T) { + t.Setenv("APP_ENV", "test") t.Setenv("APP_CONFIG_PATH", "testdata/config") t.Setenv("SQL_DRIVER", "sqlite") t.Setenv("SQL_DSN", ":memory:") diff --git a/fxsql/testdata/config/config.test.yaml b/fxsql/testdata/config/config.test.yaml new file mode 100644 index 00000000..e1ce688b --- /dev/null +++ b/fxsql/testdata/config/config.test.yaml @@ -0,0 +1,10 @@ +modules: + log: + level: debug + output: test + trace: + processor: + type: test +config: + hook_name: "test hook" + seed_value: "test seed value" diff --git a/fxsql/testdata/config/config.yaml b/fxsql/testdata/config/config.yaml index 8ba39a8a..a4f660e8 100644 --- a/fxsql/testdata/config/config.yaml +++ b/fxsql/testdata/config/config.yaml @@ -1,13 +1,6 @@ app: name: test - version: 0.1.0 modules: - log: - level: debug - output: test - trace: - processor: - type: test sql: driver: ${SQL_DRIVER} dsn: ${SQL_DSN} @@ -25,6 +18,3 @@ modules: arguments: true exclude: - "connection:ping" -config: - hook_name: "test hook" - seed_value: "test seed value"