fixtures

package module
v0.0.0-...-8d7ddb7 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2019 License: MPL-2.0 Imports: 7 Imported by: 35

README

go-fixtures

Django style fixtures for Golang's excellent built-in database/sql library. Currently only YAML fixtures are supported.

Travis Status for RichardKnop/go-fixtures godoc for RichardKnop/go-fixtures codecov for RichardKnop/go-fixtures


There are two reserved values you can use for datetime fields:

  • ON_INSERT_NOW() will only be used when a row is being inserted
  • ON_UPDATE_NOW() will only be used when a row is being updated

Example YAML fixture:

---

- table: 'some_table'
  pk:
    id: 1
  fields:
    string_field: 'foobar'
    boolean_field: true
    created_at: 'ON_INSERT_NOW()'
    updated_at: 'ON_UPDATE_NOW()'

- table: 'other_table'
  pk:
    id: 2
  fields:
    int_field: 123
    boolean_field: false
    created_at: 'ON_INSERT_NOW()'
    updated_at: 'ON_UPDATE_NOW()'

- table: 'join_table'
  pk:
    some_id: 1
    other_id: 2

Example integration for your project:

package main

import (
	"database/sql"
	"io/ioutil"
	"log"

	"github.com/RichardKnop/go-fixtures"
	"github.com/urfave/cli"
	// Drivers
	_ "github.com/lib/pq"
)

var (
	cliApp *cli.App
)

func init() {
	cliApp = cli.NewApp()
	cliApp.Name = "your-project"
	cliApp.Usage = "Project's usage"
	cliApp.Author = "Your Name"
	cliApp.Email = "your@email"
	cliApp.Version = "0.0.0"
}

func main() {
	db, err := sql.Connect("postgres", "user=foo dbname=bar sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	cliApp.Commands = []cli.Command{
		{
			Name:  "loaddata",
			Usage: "load data from fixture",
			Action: func(c *cli.Context) error {
				data, err := ioutil.ReadFile(c.Args().First())
				if err != nil {
					return err
				}

				if err := fixtures.Load(data, db, "postgres"); err != nil {
					return err
				}
			},
		},
		{
			Name:  "runserver",
			Usage: "run web server",
			Action: func(c *cli.Context) error {
				// Run your web server here
				return nil
			},
		},
	}

	cliApp.Run(os.Args)
}

Documentation

Overview

Package fixtures implements Django like fixtures for Postgres and MySQL

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(data []byte, db *sql.DB, driver string) error

Load processes a YAML fixture and inserts/updates the database accordingly

func LoadFile

func LoadFile(filename string, db *sql.DB, driver string) error

LoadFile ...

func LoadFiles

func LoadFiles(filenames []string, db *sql.DB, driver string) error

LoadFiles ...

func NewFileError

func NewFileError(filename string, cause error) error

NewFileError ...

func NewProcessingError

func NewProcessingError(row int, cause error) error

NewProcessingError ...

Types

type Row

type Row struct {
	Table  string
	PK     map[string]interface{}
	Fields map[string]interface{}
	// contains filtered or unexported fields
}

Row represents a single database row

func (*Row) GetInsertColumns

func (row *Row) GetInsertColumns() []string

GetInsertColumns returns a slice of column names for INSERT query

func (*Row) GetInsertColumnsLength

func (row *Row) GetInsertColumnsLength() int

GetInsertColumnsLength returns number of columns for INSERT query

func (*Row) GetInsertPlaceholders

func (row *Row) GetInsertPlaceholders(driver string) []string

GetInsertPlaceholders returns a slice of placeholders for INSERT query

func (*Row) GetInsertValues

func (row *Row) GetInsertValues() []interface{}

GetInsertValues returns a slice of values for INSERT query

func (*Row) GetInsertValuesLength

func (row *Row) GetInsertValuesLength() int

GetInsertValuesLength returns number of values for INSERT query

func (*Row) GetPKValues

func (row *Row) GetPKValues() []interface{}

GetPKValues returns a slice of primary key values

func (*Row) GetUpdateColumns

func (row *Row) GetUpdateColumns() []string

GetUpdateColumns returns a slice of column names for UPDATE query

func (*Row) GetUpdateColumnsLength

func (row *Row) GetUpdateColumnsLength() int

GetUpdateColumnsLength returns number of columns for UDPATE query

func (*Row) GetUpdatePlaceholders

func (row *Row) GetUpdatePlaceholders(driver string) []string

GetUpdatePlaceholders returns a slice of placeholders for UPDATE query

func (*Row) GetUpdateValues

func (row *Row) GetUpdateValues() []interface{}

GetUpdateValues returns a slice of values for UPDATE query

func (*Row) GetUpdateValuesLength

func (row *Row) GetUpdateValuesLength() int

GetUpdateValuesLength returns number of values for UDPATE query

func (*Row) GetWhere

func (row *Row) GetWhere(driver string, i int) string

GetWhere returns a where condition based on primary key with placeholders

func (*Row) Init

func (row *Row) Init()

Init loads internal struct variables

Jump to

Keyboard shortcuts

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