Ferry - 方便数据传输的ruby gem
Ferry是一个命令行工具rubygem设计用于Rails数据迁移和操作。
Ferry使用场景
查看示例:ferry_demo app or our GitHub pages site for guidance on using Ferry!
Rails数据迁移和操作的用例
- 将数据导出成各种文件格式(.csv, .yml, .sql)
- 从各种文件格式导入数据
- 将数据迁移到第三方主机(Amazon S3, Oracle)
- 数据迁移到不同的数据库
安装
Add this line to your Rails application's Gemfile:
gem 'ferry'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ferry
To view what Ferry can do for you just run:
$ ferry --help
导出
Ferry can export data from a database connected to a Rails app into a CSV or YAML file. We currently only support exporting of SQLite3, MySQL2, and PostgreSQL databases.
Run ferry --to_csv [environment] [table]
in your Rails directory to export to csv:
$ ferry --to_csv production users
Running the above command will export the "users" table from the database connected to the "production" environment. A csv file populated with the "users" table data will be created at /db/csv/test/users.csv (the path will be created and if there is a users.csv it will be overwritten).
Run ferry --to_yaml [environment] [table]
in your Rails directory to export to yaml:
$ ferry --to_yaml development users
Similarly, running the above command in the Rails directory will export the "users" table from the database connected to the "development" environment. A yaml file populated with the "users" table data will be created at /db/yaml/test/users.csv (the path will be created and if there is a users.csv it will be overwritten).
导入
Ferry can import a csv file of validated records into a table of a Rails-connected database. The csv file must:
- Have headers that match field names of the table
- Have values that meet the table's constraints (i.e. required fields, correct data types, unique PKs, etc.)
Run ferry --import [environment] [table] [file path]
in your Rails directory to import a csv to a database table:
$ ferry --import development users db/csv/import_data.csv
Running the above command will import the import_data.csv to the "users" table in the "development" environment.