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.