Skip to content

Commit

Permalink
chore: apply all MARIADB_ env vars to MYSQL_
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Aug 30, 2023
1 parent 787cf31 commit 078dbb3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
9 changes: 9 additions & 0 deletions docs/modules/mariadb.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ for MariaDB. E.g. `testcontainers.WithImage("mariadb:11.0.3")`.
[Custom Image](../../modules/mariadb/mariadb_test.go) inside_block:withConfigFile
<!--/codeinclude-->

!!!info
From MariaDB [docs](https://github.com/docker-library/docs/tree/master/mariadb#environment-variables):

From tag 10.2.38, 10.3.29, 10.4.19, 10.5.10 onwards, and all 10.6 and later tags,
the `MARIADB_*` equivalent variables are provided. `MARIADB_*` variants will always be
used in preference to `MYSQL_*` variants.

The MariaDB module will take all the environment variables that start with `MARIADB_` and duplicate them with the `MYSQL_` prefix.

#### Wait Strategies

If you need to set a different wait strategy for MariaDB, you can use `testcontainers.WithWaitStrategy` with a valid wait strategy
Expand Down
29 changes: 21 additions & 8 deletions modules/mariadb/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,23 @@ func WithDefaultCredentials() testcontainers.CustomizeRequestOption {
req.Env["MARIADB_ALLOW_EMPTY_ROOT_PASSWORD"] = "yes"
delete(req.Env, "MARIADB_PASSWORD")
}
}
}

// https://github.com/docker-library/docs/tree/master/mariadb#environment-variables
// From tag 10.2.38, 10.3.29, 10.4.19, 10.5.10 onwards, and all 10.6 and later tags,
// the MARIADB_* equivalent variables are provided. MARIADB_* variants will always be
// used in preference to MYSQL_* variants.
req.Env["MYSQL_ROOT_PASSWORD"] = req.Env["MARIADB_ROOT_PASSWORD"]
req.Env["MYSQL_USER"] = req.Env["MARIADB_USER"]
req.Env["MYSQL_PASSWORD"] = req.Env["MARIADB_PASSWORD"]
req.Env["MYSQL_DATABASE"] = req.Env["MARIADB_DATABASE"]
// https://github.com/docker-library/docs/tree/master/mariadb#environment-variables
// From tag 10.2.38, 10.3.29, 10.4.19, 10.5.10 onwards, and all 10.6 and later tags,
// the MARIADB_* equivalent variables are provided. MARIADB_* variants will always be
// used in preference to MYSQL_* variants.
func withMySQLEnvVars() testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
// look up for MARIADB environment variables and apply the same to MYSQL
for k, v := range req.Env {
if strings.HasPrefix(k, "MARIADB_") {
// apply the same value to the MYSQL environment variables
mysqlEnvVar := strings.ReplaceAll(k, "MARIADB_", "MYSQL_")
req.Env[mysqlEnvVar] = v
}
}
}
}

Expand Down Expand Up @@ -126,6 +134,11 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
opt.Customize(&genericContainerReq)
}

// Apply MySQL environment variables after user customization
// In future releases of MariaDB, they could remove the MYSQL_* environment variables
// at all. Then we can remove this customization.
withMySQLEnvVars().Customize(&genericContainerReq)

username, ok := req.Env["MARIADB_USER"]
if !ok {
username = rootUser
Expand Down

0 comments on commit 078dbb3

Please sign in to comment.