tmpmysql

package module
v0.0.0-...-64dcbcb Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2014 License: MIT Imports: 10 Imported by: 0

README

tmpmysqld

tmpmysqld allows you to spin up temporary instances of mysqld for testing purposes:

func TestMySQLServer(t *testing.T) {
	server, err := NewMySQLServer("test")
	if err != nil {
		t.Fatal(err)
	}
	defer server.Stop()

	if _, err := server.DB.Exec(`
CREATE TABLE things (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL
)
`); err != nil {
		t.Error(err)
    }

    // use temporary mysqld instance
    ...
}

For documentation, check godoc.

Documentation

Overview

Package tmpmysql provides the ability to spin up temporary mysqld instances for testing purposes.

Example
if !IsMySQLInstalled() {
	panic("MySQL not installed")
}

server, err := NewMySQLServer("tmpmysqld_test")
if err != nil {
	panic(err)
}
defer server.Stop()

if _, err := server.DB.Exec(`
CREATE TABLE things (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL
)
`); err != nil {
	panic(err)
}

if _, err := server.DB.Exec(`
INSERT INTO things (name) VALUES ("one"), ("two")
`); err != nil {
	panic(err)
}

rows, err := server.DB.Query(`SELECT id, name FROM things ORDER BY id`)
if err != nil {
	panic(err)
}
defer rows.Close()

for rows.Next() {
	var id int64
	var name string
	if err := rows.Scan(&id, &name); err != nil {
		panic(err)
	}
	fmt.Printf("%d=%s\n", id, name)
}
Output:

1=one
2=two

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsMySQLInstalled

func IsMySQLInstalled() bool

IsMySQLInstalled returns true if the various required components of MySQL are available.

tmpmysqld requires mysqld, mysql_install_db, and mysql_config to be on the path of the running process.

Types

type MySQLServer

type MySQLServer struct {
	DB *sql.DB
	// contains filtered or unexported fields
}

A MySQLServer is a temporary instance of mysqld.

func NewMySQLServer

func NewMySQLServer(name string) (*MySQLServer, error)

NewMySQLServer returns a new mysqld instance running on the given port, with the given database created and selected as the current database.

func (*MySQLServer) Stop

func (s *MySQLServer) Stop() error

Stop terminates the mysqld instance and deletes the temporary directory which contains the database files.

Jump to

Keyboard shortcuts

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