mysqlbox

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: May 7, 2021 License: MIT Imports: 20 Imported by: 0

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 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. 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

func DataFromBuffer(buf []byte) *Data

DataFromBuffer can be used to load data from a byte array.

func DataFromReader

func DataFromReader(reader io.Reader) *Data

DataFromReader can be used to load data from a reader object.

type MySQLBox

type MySQLBox struct {
	// contains filtered or unexported fields
}

func Start

func Start(c *Config) (*MySQLBox, error)

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())
	}
}
Output:

func (*MySQLBox) CleanAllTables

func (b *MySQLBox) CleanAllTables()

CleanAllTables truncates all tables in the Database, except those provided in Config.DoNotCleanTables.

func (*MySQLBox) CleanTables

func (b *MySQLBox) CleanTables(tables ...string)

CleanTables truncates the specified tables in the Database.

func (*MySQLBox) ContainerName

func (b *MySQLBox) ContainerName() string

ContainerName returns the name of the created container.

func (*MySQLBox) DB

func (b *MySQLBox) DB() *sql.DB

DB returns an sql.DB connected to the running MySQL server.

func (*MySQLBox) DBx

func (b *MySQLBox) DBx() *sqlx.DB

DBx returns an sqlx.DB connected to the running MySQL server.

func (*MySQLBox) Stop

func (b *MySQLBox) Stop() error

Stop stops the MySQL container.

func (*MySQLBox) URL

func (b *MySQLBox) URL() string

URL returns the MySQL database URL that can be used to connect tohe MySQL service.

Jump to

Keyboard shortcuts

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