Documentation
¶
Overview ¶
Package mysqlbox provides MySQLBox, a utility for starting a MySQL server in a Docker container.
It creates a ready to use MySQL server in a Docker container that can be used in tests. The Start() function returns a MySQLBox that has a running MySQL server. It has a Stop() function that stops the container. The DB() function returns a connected sql.DB that can be used to send queries to MySQL.
Index ¶
- Variables
- type Config
- type Data
- type MySQLBox
- func (b *MySQLBox) CleanAllTables() error
- func (b *MySQLBox) CleanTables(tables ...string) error
- func (b *MySQLBox) ConnectDB(dbname string) (*sql.DB, string, error)
- func (b *MySQLBox) ContainerName() (string, error)
- func (b *MySQLBox) DB() (*sql.DB, error)
- func (b *MySQLBox) DBAddr() string
- func (b *MySQLBox) DSN() (string, error)
- func (b *MySQLBox) MustCleanAllTables()
- func (b *MySQLBox) MustCleanTables(tables ...string)
- func (b *MySQLBox) MustContainerName() string
- func (b *MySQLBox) MustDB() *sql.DB
- func (b *MySQLBox) MustDSN() string
- func (b *MySQLBox) MustStart(c *Config) *MySQLBox
- func (b *MySQLBox) MustStop()
- func (b *MySQLBox) RootPassword() string
- func (b *MySQLBox) Stop() error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTimeout represents a timeout in an operation. ErrTimeout = errors.New("operation timed out") )
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 name>". 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. RootPassword string // 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 // Stdout is an optional writer where the container log stdout will be sent to. Stdout io.Writer // Stderr is an optional writer where the container log stderr will be sent to. Stderr io.Writer // LoggedErrors is an optional list of strings that will contain error messages from the container stderr logs. LoggedErrors *[]string // StartTimeout is the maximum time to wait for the container to start and MySQL ready to accept connections. // The default is 30 seconds. StartTimeout time.Duration // StopTimeout is the amount of time to wait for the container to gracefully stop when Stop() is called. // When the timeout is reached, the container is forcefully stopped. StopTimeout time.Duration }
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 DataFromFile ¶ added in v0.4.0
DataFromFile can be used to load data from a file.
func DataFromReader ¶
DataFromReader can be used to load data from a reader object.
type MySQLBox ¶
type MySQLBox struct {
// contains filtered or unexported fields
}
MySQLBox is an interface to a MySQL server running in a Docker container.
func Start ¶
Start creates a Docker container that runs an instance of MySQL server. The passed Config object contains settings for the container, the MySQL service, and initial data. To stop the created container, call the Stop() method. Start() returns an ErrTimeout if the container MySQL service cannot accept connections within the timeout period (see Config.StartTimeout). When ErrTimeout is returned, the container is still running and an instance of MySQLBox is returned along with the error.
Example ¶
package main import ( "log" "github.com/virgild/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 db, err := b.DB() if err != nil { log.Printf("db error: %s\n", err.Error()) return } _, err = 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()) } }
Output:
func (*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) ConnectDB ¶ added in v0.4.2
ConnectDB returns a DB connection and the DSN for the specified database.
func (*MySQLBox) ContainerName ¶
ContainerName returns the name of the created container.
func (*MySQLBox) DSN ¶ added in v0.3.1
DSN returns the MySQL database DSN that can be used to connect to the MySQL service.
func (*MySQLBox) MustCleanAllTables ¶
func (b *MySQLBox) MustCleanAllTables()
MustCleanAllTables truncates all tables in the Database, except those provided in Config.DoNotCleanTables.
func (*MySQLBox) MustCleanTables ¶
MustCleanTables truncates the specified tables in the Database.
func (*MySQLBox) MustContainerName ¶
MustContainerName returns the name of the created container.
func (*MySQLBox) MustDSN ¶ added in v0.3.1
MustDSN returns the MySQL database DSN that can be used to connect to the MySQL service.
func (*MySQLBox) MustStart ¶ added in v0.4.2
MustStart is the same as Start() but panics instead of returning an error.
func (*MySQLBox) RootPassword ¶ added in v0.4.2
RootPassword returns the MySQL root user password.