-
-
Notifications
You must be signed in to change notification settings - Fork 605
feat: add TiDB module #3575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add TiDB module #3575
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # TiDB | ||
|
|
||
| Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a> | ||
|
|
||
| ## Introduction | ||
|
|
||
| The Testcontainers module for TiDB. TiDB is a MySQL-compatible distributed SQL database. | ||
|
|
||
| ## Adding this module to your project dependencies | ||
|
|
||
| Please run the following command to add the TiDB module to your Go dependencies: | ||
|
|
||
| ``` | ||
| go get github.com/testcontainers/testcontainers-go/modules/tidb | ||
| ``` | ||
|
|
||
| ## Usage example | ||
|
|
||
| <!--codeinclude--> | ||
| [Creating a TiDB container](../../modules/tidb/examples_test.go) inside_block:runTiDBContainer | ||
| <!--/codeinclude--> | ||
|
|
||
| ## Module Reference | ||
|
|
||
| ### Run function | ||
|
|
||
| The TiDB module exposes one entrypoint function to create the container, and this function receives three parameters: | ||
|
|
||
| ```golang | ||
| func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) | ||
| ``` | ||
|
|
||
| - `context.Context`, the Go context. | ||
| - `string`, the Docker image to use. | ||
| - `testcontainers.ContainerCustomizer`, a variadic argument for passing options. | ||
|
|
||
| #### Image | ||
|
|
||
| Use the second argument in the `Run` function to set a valid Docker image. | ||
| In example: `Run(context.Background(), "pingcap/tidb:v8.4.0")`. | ||
|
|
||
| ### Container Options | ||
|
|
||
| When starting the TiDB container, you can pass options in a variadic way to configure it. | ||
|
|
||
| !!!tip | ||
| You can find all the available configuration and environment variables for the TiDB Docker image on [Docker Hub](https://hub.docker.com/r/pingcap/tidb). | ||
|
|
||
| !!!info | ||
| TiDB is MySQL-compatible, so you can use any MySQL driver (e.g. `github.com/go-sql-driver/mysql`) to connect and execute SQL statements after the container is ready. | ||
|
|
||
| {% include "../features/common_functional_options_list.md" %} | ||
|
|
||
| ### Container Methods | ||
|
|
||
| #### ConnectionString | ||
|
|
||
| This method returns the connection string to connect to the TiDB container, using the default `4000` port. | ||
| It's possible to pass extra parameters to the connection string, e.g. `charset=utf8mb4`, in a variadic way. | ||
|
|
||
| <!--codeinclude--> | ||
| [Get connection string](../../modules/tidb/tidb_test.go) inside_block:connectionString | ||
| <!--/codeinclude--> | ||
|
|
||
| #### MustConnectionString | ||
|
|
||
| Same as `ConnectionString`, but panics if an error occurs while getting the connection string. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| include ../../commons-test.mk | ||
|
|
||
| .PHONY: test | ||
| test: | ||
| $(MAKE) test-tidb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package tidb_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
|
iyiola-dev marked this conversation as resolved.
|
||
| "log" | ||
|
|
||
| _ "github.com/go-sql-driver/mysql" | ||
|
|
||
| "github.com/testcontainers/testcontainers-go" | ||
| "github.com/testcontainers/testcontainers-go/modules/tidb" | ||
| ) | ||
|
|
||
| func ExampleRun() { | ||
| // runTiDBContainer { | ||
| ctx := context.Background() | ||
|
|
||
| ctr, err := tidb.Run(ctx, | ||
| "pingcap/tidb:v8.4.0", | ||
| ) | ||
| defer func() { | ||
| if err := testcontainers.TerminateContainer(ctr); err != nil { | ||
| log.Printf("failed to terminate container: %s", err) | ||
| } | ||
| }() | ||
| if err != nil { | ||
| log.Printf("failed to start container: %s", err) | ||
| return | ||
| } | ||
| // } | ||
|
|
||
| state, err := ctr.State(ctx) | ||
| if err != nil { | ||
| log.Printf("failed to get container state: %s", err) | ||
| return | ||
| } | ||
|
|
||
| fmt.Println(state.Running) | ||
|
|
||
| // Output: | ||
| // true | ||
| } | ||
|
|
||
| func ExampleRun_connect() { | ||
| ctx := context.Background() | ||
|
|
||
| ctr, err := tidb.Run(ctx, | ||
| "pingcap/tidb:v8.4.0", | ||
| ) | ||
| defer func() { | ||
| if err := testcontainers.TerminateContainer(ctr); err != nil { | ||
| log.Printf("failed to terminate container: %s", err) | ||
| } | ||
| }() | ||
| if err != nil { | ||
| log.Printf("failed to start container: %s", err) | ||
| return | ||
| } | ||
|
|
||
| connectionString, err := ctr.ConnectionString(ctx) | ||
| if err != nil { | ||
| log.Printf("failed to get connection string: %s", err) | ||
| return | ||
| } | ||
|
|
||
| db, err := sql.Open("mysql", connectionString) | ||
| if err != nil { | ||
| log.Printf("failed to connect to TiDB: %s", err) | ||
| return | ||
| } | ||
| defer db.Close() | ||
|
|
||
| if err = db.Ping(); err != nil { | ||
| log.Printf("failed to ping TiDB: %s", err) | ||
| return | ||
| } | ||
|
|
||
| var result int | ||
| row := db.QueryRow("SELECT 1") | ||
| if err = row.Scan(&result); err != nil { | ||
| log.Printf("failed to scan row: %s", err) | ||
| return | ||
| } | ||
|
|
||
| fmt.Println(result) | ||
|
|
||
| // Output: | ||
| // 1 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| module github.com/testcontainers/testcontainers-go/modules/tidb | ||
|
|
||
| go 1.25.0 | ||
|
|
||
| toolchain go1.25.7 | ||
|
|
||
| require ( | ||
| github.com/go-sql-driver/mysql v1.7.1 | ||
| github.com/stretchr/testify v1.11.1 | ||
| github.com/testcontainers/testcontainers-go v0.40.0 | ||
| ) | ||
|
|
||
| require ( | ||
| dario.cat/mergo v1.0.2 // indirect | ||
| github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect | ||
| github.com/Microsoft/go-winio v0.6.2 // indirect | ||
| github.com/cenkalti/backoff/v4 v4.3.0 // indirect | ||
| github.com/cespare/xxhash/v2 v2.3.0 // indirect | ||
| github.com/containerd/errdefs v1.0.0 // indirect | ||
| github.com/containerd/errdefs/pkg v0.3.0 // indirect | ||
| github.com/containerd/log v0.1.0 // indirect | ||
| github.com/containerd/platforms v0.2.1 // indirect | ||
| github.com/cpuguy83/dockercfg v0.3.2 // indirect | ||
| github.com/davecgh/go-spew v1.1.1 // indirect | ||
| github.com/distribution/reference v0.6.0 // indirect | ||
| github.com/docker/docker v28.5.2+incompatible // indirect | ||
| github.com/docker/go-connections v0.6.0 // indirect | ||
| github.com/docker/go-units v0.5.0 // indirect | ||
| github.com/ebitengine/purego v0.9.1 // indirect | ||
| github.com/felixge/httpsnoop v1.0.4 // indirect | ||
| github.com/go-logr/logr v1.4.3 // indirect | ||
| github.com/go-logr/stdr v1.2.2 // indirect | ||
| github.com/go-ole/go-ole v1.2.6 // indirect | ||
| github.com/google/uuid v1.6.0 // indirect | ||
| github.com/klauspost/compress v1.18.0 // indirect | ||
| github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect | ||
| github.com/magiconair/properties v1.8.10 // indirect | ||
| github.com/moby/docker-image-spec v1.3.1 // indirect | ||
| github.com/moby/go-archive v0.1.0 // indirect | ||
| github.com/moby/patternmatcher v0.6.0 // indirect | ||
| github.com/moby/sys/sequential v0.6.0 // indirect | ||
| github.com/moby/sys/user v0.4.0 // indirect | ||
| github.com/moby/sys/userns v0.1.0 // indirect | ||
| github.com/moby/term v0.5.0 // indirect | ||
| github.com/morikuni/aec v1.0.0 // indirect | ||
| github.com/opencontainers/go-digest v1.0.0 // indirect | ||
| github.com/opencontainers/image-spec v1.1.1 // indirect | ||
| github.com/pkg/errors v0.9.1 // indirect | ||
| github.com/pmezard/go-difflib v1.0.0 // indirect | ||
| github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect | ||
| github.com/shirou/gopsutil/v4 v4.25.12 // indirect | ||
| github.com/sirupsen/logrus v1.9.3 // indirect | ||
| github.com/tklauser/go-sysconf v0.3.16 // indirect | ||
| github.com/tklauser/numcpus v0.11.0 // indirect | ||
| github.com/yusufpapurcu/wmi v1.2.4 // indirect | ||
| go.opentelemetry.io/auto/sdk v1.2.1 // indirect | ||
| go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect | ||
| go.opentelemetry.io/otel v1.41.0 // indirect | ||
| go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.41.0 // indirect | ||
| go.opentelemetry.io/otel/metric v1.41.0 // indirect | ||
| go.opentelemetry.io/otel/sdk v1.41.0 // indirect | ||
| go.opentelemetry.io/otel/trace v1.41.0 // indirect | ||
| go.opentelemetry.io/proto/otlp v1.9.0 // indirect | ||
| golang.org/x/crypto v0.45.0 // indirect | ||
| golang.org/x/sys v0.41.0 // indirect | ||
| google.golang.org/protobuf v1.36.11 // indirect | ||
| gopkg.in/yaml.v3 v3.0.1 // indirect | ||
| ) | ||
|
|
||
| replace github.com/testcontainers/testcontainers-go => ../.. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.