CLI tool written in Go to populate an SQL database from JSON or YAML data.
$ go get github.com/claudetech/dbpopulate
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
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.
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": "千代田"
}]
}
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.