populator

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

populator

Go License GitHub release codecov Made by Alexey Burmistrov

This package was created for testing purposes, to give the ability to seed a database with records from simple .yaml files. Populator respects the order in files, so you can handle foreign_keys just by placing them in the right order.

Install

Using go
go get -u github.com/MrEhbr/populator

Example usage

package foo_test

import (
 "database/sql"
 "os"
 "testing"

 "github.com/MrEhbr/populator"
 "github.com/MrEhbr/populator/postgres"
)

var fixtures = `
- table: users
  rows:
    - id: 1
      name: "foo"
      settings:
        foo: "bar"
    - id: 2
      name: "bar"
      settings:
        - foo: "bar"
`

func TestFoo(t *testing.T) {
 db, err := sql.Open("postgres", os.Getenv("POSTGRES_DSN"))
 if err != nil {
  t.Fatalf("Unable to connect to database: %v", err)
 }
 defer db.Close()

 engine := postgres.New(postgres.NewSQL(db), postgres.DisableForeignKeyCheck())
 seeder := populator.New(
  populator.WithEngine(engine),
  populator.WithParser(populator.YAMLParse),
 )

 if err := seeder.From(fixtures); err != nil {
  t.Fatalf("Unable to seed database: %v", err)
 }
}

License

© 2020 [Alexey Burmistrov]

Licensed under the Apache License, Version 2.0 (LICENSE). See the COPYRIGHT file for more details.

SPDX-License-Identifier: Apache-2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Query string
	Args  []interface{}
}

type Engine

type Engine interface {
	Build(Fixtures) ([]Command, error)
	Exec(cmds []Command) error
}

type Fixture

type Fixture struct {
	Table string
	Rows  []map[string]interface{}
}

type Fixtures

type Fixtures []Fixture

func JSONParse

func JSONParse(r io.Reader) (Fixtures, error)

func YAMLParse

func YAMLParse(r io.Reader) (Fixtures, error)

func (Fixtures) Tables

func (f Fixtures) Tables() []string

type Option

type Option func(*Populator)

Option defines options for Populator.

func WithEngine

func WithEngine(db Engine) Option

func WithParser

func WithParser(parser Parser) Option

type Parser

type Parser func(r io.Reader) (Fixtures, error)

type Populator

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

func New

func New(options ...Option) *Populator

func (*Populator) From

func (p *Populator) From(content string) error

func (*Populator) Load

func (p *Populator) Load(file ...string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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