Documentation
¶
Overview ¶
Package mysqlbox provides MySQLBox.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// ContainerName specifies the MySQL container name. If blank, it will be generated as "mysqlbox-<random id>".
ContainerName string
// Image specifies what Docker image to use. If blank, it defaults to "mysql:8".
Image string
// Database specifies the name of the database to create. If blank, it defaults to "testing".
Database string
// RootPassword specifies the password of the MySQL root user. If blank, the password is set to empty unless
// RandomRootPassword is true.
RootPassword string
// RandomRootPassword sets the password of the MySQL root user to a random value.
RandomRootPassword bool
// MySQLPort specifies which port the MySQL server port (3306) will be bound to in the container.
MySQLPort int
// InitialSQL specifies an SQL script stored in a file or a buffer that will be run against the Database
// when the MySQL server container is started.
InitialSQL *Data
// DoNotCleanTables specifies a list of MySQL tables in Database that will not be cleaned when CleanAllTables()
// is called.
DoNotCleanTables []string
}
Config contains MySQLBox settings.
func (*Config) LoadDefaults ¶
func (c *Config) LoadDefaults()
LoadDefaults initializes some blank attributes of Config to default values.
type Data ¶
type Data struct {
// contains filtered or unexported fields
}
Data contains data.
func DataFromBuffer ¶
DataFromBuffer can be used to load data from a byte array.
func DataFromReader ¶
DataFromReader can be used to load data from a reader object.
type MySQLBox ¶
type MySQLBox struct {
// contains filtered or unexported fields
}
func Start ¶
Start creates a Docker container that will run a MySQL server. The passed Config object contains settings for the container, the MySQL service, and initial data. To stop the created container, call the function returned by StopFunc.
Example ¶
package main
import (
"log"
"github.com/virgild/testutils/mysqlbox"
)
func main() {
// Start the MySQL server container
b, err := mysqlbox.Start(&mysqlbox.Config{})
if err != nil {
log.Printf("MySQLBox failed to start: %s\n", err.Error())
return
}
// Query the database
_, err = b.DB().Query("SELECT * FROM users LIMIT 5")
if err != nil {
log.Printf("select failed: %s\n", err.Error())
return
}
// Stop the container
err = b.Stop()
if err != nil {
log.Printf("stop container failed: %s\n", err.Error())
}
}
func (*MySQLBox) CleanAllTables ¶
func (b *MySQLBox) CleanAllTables()
CleanAllTables truncates all tables in the Database, except those provided in Config.DoNotCleanTables.
func (*MySQLBox) CleanTables ¶
CleanTables truncates the specified tables in the Database.
func (*MySQLBox) ContainerName ¶
ContainerName returns the name of the created container.