dockerdb

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: MIT Imports: 12 Imported by: 0

README

dockerdb

PkgGoDev

This repository contains a package for fast database deployment in Docker container.

Tested Vendors

  1. PosgreSQL
  2. MySQL

Why dockerdb?

изображение

Usage

Download and install it:

go get github.com/egorgasay/dockerdb

Import it in your code:

import "github.com/egorgasay/dockerdb"

The first launch should look like this:

vdb, err := dockerdb.New(ctx, config)
if err != nil {
  log.Fatal(err)
}

If the database was turned off, then you can turn it on using:

err := vdb.Run(ctx)
if err != nil {
  log.Fatal(err)
}

Example

package main

import (
	"context"
	"fmt"
	"log"
	
	"https://github.com/egorgasay/dockerdb"

    _ "github.com/lib/pq"
)

func main() {
	// Specify the data that is needed to run the database
	config := dockerdb.CustomDB{
		DB: dockerdb.DB{
			Name:     "admin",
			User:     "admin",
		Password: "admin",
		},
		Port:   "45217",
		Vendor: dockerdb.Postgres15,
	}
      
	ctx := context.TODO()
	vdb, err := dockerdb.New(ctx, config)
	if err != nil {
		log.Fatal(err)
	}
      
      // testing db is working
	var answer string
	err = vdb.DB.QueryRow("SELECT 'db is up'").Scan(&answer)
	if err != nil {
		log.Fatal(err)
	}
    
	fmt.Println(answer)
    
	if err = vdb.Stop(ctx); err != nil {
		log.Fatal(err)
	}
}

Example 2 (Unimplemented db)

package main

import (
    "context"
	"fmt"
	"log"
	
	"https://github.com/egorgasay/dockerdb"

    _ "github.com/lib/pq"
)

func main() {
	// Specify the data that is needed to run the database
	config := dockerdb.CustomDB{
		DB: dockerdb.DB{
			Name:     "admin",
			User:     "admin",
			Password: "admin",
		},
		Port:   "45217",
		Vendor: "postgres:10",

		PortDocker: "5432/tcp",
		EnvDocker:  []string{"POSTGRES_DB=" + "DBNAME", "POSTGRES_USER=" + "USERNAME",
			"POSTGRES_PASSWORD=" + "PASSWORD"},
	}

	// This will allow you to upload the image to your computer. 
	ctx := context.TODO()
	err := dockerdb.Pull(ctx, "postgres:10")
	if err != nil {
		log.Fatal(err)
	}

	vdb, err := dockerdb.New(ctx, config)
	if err != nil {
		log.Fatal(err)
	}

	// testing db is working
	var answer string
	err = vdb.DB.QueryRow("SELECT 'db is up'").Scan(&answer)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(answer)

	if err = vdb.Stop(ctx); err != nil {
		log.Fatal(err)
	}
	
	fmt.Println("db is down")
}

Documentation

Overview

Package dockerdb allows user to create virtual databases using docker. Tested with PostgreSQL, MySQL, MS SQL.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/egorgasay/dockerdb"
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/lib/pq"
)

func main() {
	config := dockerdb.CustomDB{
		DB: dockerdb.DB{
			Name:     "admin",
			User:     "admin",
			Password: "admin",
		},
		Port:   "45217",
		Vendor: dockerdb.Postgres15,
	}

	// This will allow you to upload the image to your computer.
	ctx := context.TODO()
	err := dockerdb.Pull(ctx, dockerdb.Postgres15)
	if err != nil {
		log.Fatal(err)
	}

	vdb, err := dockerdb.New(ctx, config)
	if err != nil {
		log.Fatal(err)
	}

	var answer string
	err = vdb.DB.QueryRow("SELECT 'db is up'").Scan(&answer)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(answer)

	if err = vdb.Stop(ctx); err != nil {
		log.Fatal(err)
	}

	fmt.Println("db is down")
}
Output:

Index

Examples

Constants

View Source
const (
	Postgres15 = "postgres:15"
	Postgres14 = "postgres:14"
	Postgres13 = "postgres:13"
	Postgres12 = "postgres:12"
	Postgres11 = "postgres:11"

	MySQL5Image = "mysql:5.7"
	MySQL8Image = "mysql:8"
)

Variables

View Source
var (
	ErrUnknown     = errors.New("unknown error")
	ErrUnsupported = errors.New("unsupported db vendor")
)

Functions

func Build

func Build(conf CustomDB) (connStr string, err error)

Build builds connection string by CustomDB config.

func Pull added in v1.0.0

func Pull(ctx context.Context, image string) error

Pull pulls an image from net. WARNING!! USE IT CAREFULLY! DOWNLOADING SOME DB IMAGES MAY TAKE SOME TIME. Tested with PostgreSQL, MySQL, MS SQL.

func Run added in v1.1.0

func Run(ctx context.Context, ID string) error

Run launches a docker container by ID.

func SetMaxWaitTime added in v1.2.0

func SetMaxWaitTime(sec time.Duration)

Types

type CustomDB

type CustomDB struct {
	DB     DB
	Port   string
	Vendor string

	// Optional if you are using a supported vendor
	PortDocker nat.Port
	EnvDocker  []string
	// contains filtered or unexported fields
}

type DB

type DB struct {
	Name     string
	User     string
	Password string
}

type VDB

type VDB struct {
	ID string

	DB         *sql.DB
	ConnString string
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, conf CustomDB) (*VDB, error)

New creates a new docker container and launches it

func (*VDB) Kill

func (ddb *VDB) Kill(ctx context.Context, signal string) (err error)

Kill kills the docker container.

func (*VDB) Pause

func (ddb *VDB) Pause(ctx context.Context) (err error)

Pause suspends the docker container.

func (*VDB) Restart

func (ddb *VDB) Restart(ctx context.Context) (err error)

Restart stops and starts a container again.

func (*VDB) Run

func (ddb *VDB) Run(ctx context.Context) (err error)

Run launches the docker container.

func (*VDB) Stop

func (ddb *VDB) Stop(ctx context.Context) (err error)

Stop stops the docker container.

func (*VDB) Unpause

func (ddb *VDB) Unpause(ctx context.Context) (err error)

Unpause resumes the docker container.

Jump to

Keyboard shortcuts

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