MrEhbr / populator

Tool for seeding a database with records from simple .yaml files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Tool for seeding a database with records from simple .yaml files.

License:Apache License 2.0


Languages

Language:Go 89.6%Language:Shell 8.3%Language:Makefile 1.1%Language:JavaScript 1.0%