mysql

package module
v0.30.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 6 Imported by: 10

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithConfigFile

func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption

func WithDatabase

func WithDatabase(database string) testcontainers.CustomizeRequestOption

func WithDefaultCredentials

func WithDefaultCredentials() testcontainers.CustomizeRequestOption

func WithPassword

func WithPassword(password string) testcontainers.CustomizeRequestOption

func WithScripts

func WithScripts(scripts ...string) testcontainers.CustomizeRequestOption

func WithUsername

func WithUsername(username string) testcontainers.CustomizeRequestOption

Types

type MySQLContainer

type MySQLContainer struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

MySQLContainer represents the MySQL container type used in the module

func RunContainer

func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*MySQLContainer, error)

RunContainer creates an instance of the MySQL container type

Example
// runMySQLContainer {
ctx := context.Background()

mysqlContainer, err := mysql.RunContainer(ctx,
	testcontainers.WithImage("mysql:8.0.36"),
	mysql.WithConfigFile(filepath.Join("testdata", "my_8.cnf")),
	mysql.WithDatabase("foo"),
	mysql.WithUsername("root"),
	mysql.WithPassword("password"),
	mysql.WithScripts(filepath.Join("testdata", "schema.sql")),
)
if err != nil {
	log.Fatalf("failed to start container: %s", err)
}

// Clean up the container
defer func() {
	if err := mysqlContainer.Terminate(ctx); err != nil {
		log.Fatalf("failed to terminate container: %s", err)
	}
}()
// }

state, err := mysqlContainer.State(ctx)
if err != nil {
	log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
}

fmt.Println(state.Running)
Output:

true
Example (Connect)
ctx := context.Background()

mysqlContainer, err := mysql.RunContainer(ctx,
	testcontainers.WithImage("mysql:8.0.36"),
	mysql.WithConfigFile(filepath.Join("testdata", "my_8.cnf")),
	mysql.WithDatabase("foo"),
	mysql.WithUsername("root"),
	mysql.WithPassword("password"),
	mysql.WithScripts(filepath.Join("testdata", "schema.sql")),
)
if err != nil {
	log.Fatalf("failed to start container: %s", err)
}

defer func() {
	if err := mysqlContainer.Terminate(ctx); err != nil {
		log.Fatalf("failed to terminate container: %s", err)
	}
}()

connectionString, _ := mysqlContainer.ConnectionString(ctx)

db, err := sql.Open("mysql", connectionString)
if err != nil {
	log.Fatalf("failed to connect to MySQL: %s", err) // nolint:gocritic
}
defer db.Close()

if err = db.Ping(); err != nil {
	log.Fatalf("failed to ping MySQL: %s", err)
}
stmt, err := db.Prepare("SELECT @@GLOBAL.tmpdir")
if err != nil {
	log.Fatalf("failed to prepare statement: %s", err)
}
defer stmt.Close()
row := stmt.QueryRow()
tmpDir := ""
err = row.Scan(&tmpDir)
if err != nil {
	log.Fatalf("failed to scan row: %s", err)
}

fmt.Println(tmpDir)
Output:

/tmp

func (*MySQLContainer) ConnectionString

func (c *MySQLContainer) ConnectionString(ctx context.Context, args ...string) (string, error)

func (*MySQLContainer) MustConnectionString added in v0.30.0

func (c *MySQLContainer) MustConnectionString(ctx context.Context, args ...string) string

MustConnectionString panics if the address cannot be determined.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL