Giter VIP home page Giter VIP logo

dbpopulate's Introduction

dbpopulate Build Status

CLI tool written in Go to populate an SQL database from JSON or YAML data.

Installation

$ go get github.com/claudetech/dbpopulate

Usage

The only required option is --database-url. It can be of the form postgres://POSTGRES_URL, mysql://MYSQL_URL or sqlite3://PATH_TO_DB. You can also use the DATABASE_URL environment variable instead.

$ dbpopulate --debug --env=development --database-url=sqlite3://mydata.db
$ dbpopulate --quiet --database-url=postgres://localhost/foobar?sslmode=disable
$ dbpopulate --database-url=mysql://foobar:password@tcp(localhost:3306)/foobar --fixtures-path=/path/to/my/fixtures

CLI options

The available CLI options (with their short form and environment variable equivalents) are:

  • --database-url (-u, $DATABASE_URL): Database URL
  • --fixtures-path (-p, $FIXTURES_PATH): Path to the directory containing fixtures
  • --env (-e, $GO_ENV): Environment (used to look for subdirectories)
  • --debug (-d, $DEBUG): Activate debug mode (more log)
  • --quiet (-q, $QUIET): Activate quiet mode (less log)

dbpopulate uses dotenv to load environment variables, so you can put a .env file at the top of your project with the needed settings and use the dbpopulate command without any options.

Fixture files

By default, dbpopulate will read all the files ending in .yml, .yaml or .json present in FIXTURES_PATH and FIXTURES_PATH/$GO_ENV directory. If the latter does not exist, it will be ignored.

FIXTURES_PATH defaults to ./fixtures and can be changed using the --fixtures-path option. You can change GO_ENV by setting the GO_ENV environment variable or with the --env flag.

dbpopulate uses a unique key combination (only the id by default), to distinguish the records. Existing records already are not inserted again.

Here is a sample fixture file.

countries:
  - id: 1
    name: 'France'
  - id: 2
    name: 'Japan'

users:
  - id: 1
    name: 'tuvistavie'
    country_id: 1

where each key is a table name, and each value are the records to add. If you want to avoid passing the id and use another unique key for the records, you can use the following form:

countries:
  keys: [name]
  data:
      - name: 'France'
      - name: 'Japan'

You can use a single, or multiple keys to distinguish the records. An error will be raised if any of the keys is missing.

Here is an example in JSON:

{
  "regions": {
    "keys": ["name", "country_id"],
    "data": [{
      "country_id": 1,
      "name": "Ile de france",
      "order": 10
    }, {
      "country_id": 2,
      "name": "Tokyo",
      "order": 20
    }]
  },
  "prefectures": [{
    "id": 1,
    "region_id": 2,
    "name": "千代田"
  }]
}

Contributing

Please feel free to add support for other DB drivers, or other seed files format if you need.

To increasing logging level, you can pass the --debug flag or set the DEBUG environment to anything not empty.

dbpopulate's People

Contributors

iwark avatar

Stargazers

 avatar Nate-Wilkins avatar  avatar  avatar Kazushi Kawamura avatar Sergey Mezentsev avatar Samuel Rounce avatar Yusuke Abe avatar  avatar

Watchers

James Cloos avatar Daniel Perez avatar Kory Yahagi avatar Yuya Soneoka avatar  avatar  avatar

Forkers

iwark pombredanne

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.