ghost_postgres

package module
v0.0.0-...-590e4db Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2017 License: MIT Imports: 18 Imported by: 0

README

ghost-postgres

Create ephemeral postgresql databases, e.g. for testing

Inspired by testing.postgresql for python.

Getting Started

This has been tested on ubuntu 14.04. It will likely work on other ubuntu versions and possibly other *NIX distros.

It's been tested the PostgreSQL 9.5, and should work with 9.x.

Setup

Install PostgreSQL 9.x; on Ubuntu (14.04):

sudo apt-get install postgresql postgresql-contrib postgresql-common postgresql-client-common
Example
package main

import (
    "github.com/recursionpharma/ghost-postgres"
)

func main() {
	gp := ghost_postgres.New()
	defer gp.Terminate()
	err := gp.Prepare()
	if err != nil {
		fmt.Println(err)
		return
	}
	db, err := gp.Open()
	if err != nil {
		fmt.Println(err)
		return
	}
	if _, err := db.Exec("CREATE TABLE test ( id SERIAL NOT NULL, text VARCHAR(100) NOT NULL, PRIMARY KEY (id));"); err != nil {
		fmt.Println(err)
		return
	}
	if _, err := db.Exec("INSERT INTO test (text) VALUES ('Hello, World');"); err != nil {
		fmt.Println(err)
		return
	}
	var s string
	if err := db.QueryRow("SELECT text FROM test").Scan(&s); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(s)
	// Output: Hello, World
}

Directory Structure

code/ghost-postgres/
|-- examples_test.go
|   Examples
|-- file.go
|   File utilities
|-- .gitignore
|   Ignored git files
|-- LICENSE
|   Software license (MIT)
|-- postgresql.go
|   The main code and interface
|-- postgresql_test.go
|   Tests
|-- README.md
|   This file
|-- .travis.yml
|   Travis config file
`-- util.go
    Helper functions

The above file tree was generated with tree -a -L 1 --charset ascii.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GhostPostgres

type GhostPostgres struct {
	Host          string
	Port          int
	User          string
	Dbname        string
	InitdbArgs    []string
	InitdbGlobs   []string
	PostgresGlobs []string
	TmpDir        string
	Prefix        string
	Dir           string
	Postgres      *exec.Cmd
	ConnOptions   []string
	Logger        *log.Logger
	DefaultDbname string
}

func New

func New() *GhostPostgres

func (*GhostPostgres) ConnString

func (gp *GhostPostgres) ConnString() string

ConnString returns a connection string for the testing database

func (*GhostPostgres) ConnStringFor

func (gp *GhostPostgres) ConnStringFor(dbname string) string

ConnStringFor returns a connection string for the database with the specified name

func (*GhostPostgres) Create

func (gp *GhostPostgres) Create() error

Create creates the database on the running postgres server.

func (*GhostPostgres) DataDir

func (gp *GhostPostgres) DataDir() (string, error)

DataDir returns the data directory for the database.

func (*GhostPostgres) Destroy

func (gp *GhostPostgres) Destroy() error

Destroy removes the directory for the database.

func (*GhostPostgres) Init

func (gp *GhostPostgres) Init() error

Init runs initdb to create the PostgreSQL database.

func (*GhostPostgres) Open

func (gp *GhostPostgres) Open() (*sql.DB, error)

Open opens a connection to the testing database

func (*GhostPostgres) OpenFor

func (gp *GhostPostgres) OpenFor(dbname string) (*sql.DB, error)

OpenFor opens a connection to the database with the specified name

func (*GhostPostgres) Prepare

func (gp *GhostPostgres) Prepare() error

Prepare returns a functioning ephemeral database or an error.

func (*GhostPostgres) Start

func (gp *GhostPostgres) Start() error

Start starts the postgresql server and waits for it to become available.

func (*GhostPostgres) Stop

func (gp *GhostPostgres) Stop() error

Stop stops the running postgresql server. It first tries to interrupt the process, and then kills it.

func (*GhostPostgres) Terminate

func (gp *GhostPostgres) Terminate() error

Terminate stops and destroys the database.

func (*GhostPostgres) URL

func (gp *GhostPostgres) URL() string

URL returns a URL string for the testing database

func (*GhostPostgres) URLFor

func (gp *GhostPostgres) URLFor(dbname string) string

URLFor returns a URL string for the database with the specified name

Jump to

Keyboard shortcuts

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