https://github.com/dev-cloverlab/carpenter
Carpenter is a tool to manage DB schema and data
https://github.com/dev-cloverlab/carpenter
cli go mariadb migration mysql
Last synced: 27 days ago
JSON representation
Carpenter is a tool to manage DB schema and data
- Host: GitHub
- URL: https://github.com/dev-cloverlab/carpenter
- Owner: dev-cloverlab
- License: mit
- Created: 2016-11-16T01:40:09.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-05-21T14:48:05.000Z (about 7 years ago)
- Last Synced: 2025-12-17T07:12:54.104Z (6 months ago)
- Topics: cli, go, mariadb, migration, mysql
- Language: Go
- Size: 84 KB
- Stars: 42
- Watchers: 12
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# carpenter
[](https://github.com/dev-cloverlab/carpenter)
[](https://github.com/dev-cloverlab/carpenter)
carpenter is a tool to manage DB schema and data inspired by [naoina/migu](https://github.com/naoina/migu).
By using this, you can manage the database structures and data as text (JSON, CSV).
carpenter can restore the database structure and data from text, or can export them to text in easy.
**supported databases are MySQL|MariaDB only currently**
## Install
```
% brew tap dev-cloverlab/carpenter
% brew install carpenter
```
for Gophers.
```
% go get -u github.com/dev-cloverlab/carpenter
```
# How to use
carpenter has four simple commands are classified database `structure` and `data`. For each command is also available to use as indivisually.
## Commands for structure
### design
`design` command can export database structure as JSON. By doing below, exports JSON file named `table.json` to current directory.
```
% carpenter -s test -d "root:@tcp(127.0.0.1:3306)" design -d ./
```
When you want to separate files for each tables, you can set `-s` option.
options:
- `-s` export JSON files are separated for each table (default off)
- `-p` pretty output (default off)
- `-d` export directory path
Each option has alternative long name. Please see the help for details.
### build
`build` command can restore database structure from JSON files. By doing below, generate the difference SQLs between tables and JSON files and execute them.
```
% carpenter -s test -d "root:@tcp(127.0.0.1:3306)" build -d .
```
When you want to just show the generated SQLs, you can set `--dry-run` global option.
## Commands for data
### export
`export` command can export data as CSV files. By doing below, export data as CSV files for each table.
```
% carpenter -s test -d "root:@tcp(127.0.0.1:3306)" export -d .
```
When you want to select exporting tables, you can set regular expression to `-r` option like below.
```
% carpenter -s test -d "root:@tcp(127.0.0.1:3306)" export -r "^master_*$" -d .
```
### import
`import` command can import CSV files to tables. By doing below, generate the difference SQLs between tables and CSV files and execute them.
```
% carpenter -s test -d "root:@tcp(127.0.0.1:3306)" import -d .
```
When you want to just show the generated SQLs, you can set `--dry-run` global option.
## Architecture
Explain how carpenter syncronizes text and database.
MySQL(MariaDB) has information table that has table, column, index and partition information. carpenter refers that and translate it to JSON, and unmarshal it to struct. Both of structs that are made from database and files can compare each field type and etc. When some difference are found for each field, carpenter generates SQLs for resolve differences.
For example:
```
// about member table
// database
+-------+--------------+------+
| Field | Type | Null |
+-------+--------------+------+
| name | varchar(255) | NO |
| email | varchar(255) | NO |
+-------+--------------+------+
// file
+--------+--------------+------+
| Field | Type | Null |
+--------+--------------+------+
| name | varchar(255) | NO |
| email | varchar(255) | NO |
| gender | tinyint(4) | NO |
+--------+--------------+------+
```
To generate this.
```sql
alter table `member` add `gender` tinyint(4) not null after `email`
```
carpenter can generate SQLs at various scenes like:
- CREATE
- DROP
- ALTER
- INSERT
- REPLACE
- DELETE
These SQLs are generated by difference of both information structs.
## Contribution
1. Fork ([https://github.com/dev-cloverlab/carpenter/fork](https://github.com/dev-cloverlab/carpenter/fork))
1. Create a feature branch
1. Commit your changes
1. Rebase your local changes against the master branch
1. Run test suite with the `go test ./...` command and confirm that it passes
1. Run `gofmt -s`
1. Create a new Pull Request
## Author
[@hatajoe](https://twitter.com/hatajoe)
## Licence
MIT