百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

replicate

> 编程语言
开源

在 Ruby 环境之间导出和加载关系对象。

579 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

在 Ruby 环境之间导出和加载关系对象。

Dump and load relational objects between Ruby environments.

This repository is archived and no longer actively maintained by @rtomayko as of 2017-11-08. Issues and PRs documenting current issues have been intentionally left open for informational purposes.

The project started at GitHub to simplify the process of getting real production data into development and staging environments. We use it to replicate entire repository data (including associated issue, pull request, commit comment, etc. records) from production to our development environments with a single command. It's excessively useful for troubleshooting issues, support requests, and exception reports as well as for establishing real data for evaluating design concepts.

Synopsis

Installing

$ gem install replicate

Dumping objects

Evaluate a Ruby expression, dumping all resulting objects to standard output:

$ replicate -r ./config/environment -d "User.find(1)" > user.dump
==> dumped 4 total objects:
Profile        1
User           1
UserEmail      2

The -r ./config/environment option is used to require environment setup and model instantiation code needed by the ruby expression.

Dumping many objects with a dump script

Dump scripts are normal ruby source files evaluated in the context of the dumper. The dump(object) method is used to put objects into the dump stream.

ruby
# config/replicate/dump-stuff.rb
require 'config/environment'

%w[rtomayko/tilt rtomayko/bcat].each do |repo_name|
  repo = Repository.find_by_name_with_owner(repo_name)
  dump repo
  dump repo.commit_comments
  dump repo.issues
end

Run the dump script:

$ replicate -d config/replicate/dump-stuff.rb > repos.dump
==> dumped 1479 total objects:
AR::Habtm                   101
CommitComment                95
Issue                       101
IssueComment                427
IssueEvent                  308
Label                         5
Language                     19
LanguageName                  1
Milestone                     3
Organization                  4
Profile                      82
PullRequest                  44
PullRequestReviewComment      8
Repository                   20
Team                          4
TeamMember                    6
User                         89
UserEmail                   162

Loading many objects:

$ replicate -r ./config/environment -l  loaded 1479 total objects:
AR::Habtm                   101
CommitComment                95
Issue                       101
IssueComment                427
IssueEvent                  308
Label                         5
Language                     19
LanguageName                  1
Milestone                     3
Organization                  4
Profile                      82
PullRequest                  44
PullRequestReviewComment      8
Repository                   20
Team                          4
TeamMember                    6
User                         89
UserEmail                   162

Dumping and loading over ssh

$ remote_command="replicate -r /app/config/environment -d 'User.find(1234)'"
$ ssh example.org "$remote_command" |replicate -r ./config/environment -l

ActiveRecord

Basic support for dumping and loading ActiveRecord objects is included. The tests pass under ActiveRecord versions 2.2.3, 2.3.5, 2.3.14, 3.0.10, 3.1.0, and 3.2.0 under MRI 1.8.7 as well as under MRI 1.9.2.

To use customization macros in your models, require the replicate library after ActiveRecord (in e.g., config/initializers/libraries.rb):

ruby
require 'active_record'
require 'replicate'

ActiveRecord support works sensibly without customization so this isn't strictly necessary to use the replicate command. The following sections document the available customization macros.

Association Dumping

The baked in support adds some more or less sensible default behavior for all subclasses of ActiveRecord::Base such that dumping an object will bring in objects related via belongs_to and has_one associations.

Unlike 1:1 associations, has_many and has_and_belongs_to_many associations are not automatically included. Doing so would quickly lead to the entire database being sucked in. It can be useful to mark specific associations for automatic inclusion using the replicate_associations macro. For instance, to always include EmailAddress records belonging to a User:

ruby
class User  [:email_addresses]

Natural Keys

By default, the loader attempts to create a new record with a new primary key id for all objects. This can lead to unique constraint errors when a record already exists with matching attributes. To update existing records instead of creating new ones, define a natural key for the model using the replicate_natural_key macro:

ruby
class User  [:profile]

Validations and Callbacks

IMPORTANT: All ActiveRecord validations and callbacks are disabled on the loading side. While replicate piggybacks on AR for relationship information and uses ActiveRecord::Base#save to write objects to the database, it's designed to act as a simple dump / load tool.

It's sometimes useful to run certain types of callbacks on replicate. For instance, you might want to create files on disk or load information into a separate data store any time an object enters the database. The best way to go about this currently is to override the model's load_replicant class method:

ruby
class User  name, 'email' => email }
    dumper.write self.class, id, attributes, self
  end
end

load_replicant

The load side calls ::load_replicant(type, id, attributes) on the class to load each object into the current environment. The method must return an [id, object] tuple:

ruby
class User
  def self.load_replicant(type, id, attributes)
    user = User.new
    user.name  = attributes['name']
    user.email = attributes['email']
    user.save!
    [user.id, user]
  end
end

How it works

The dump format is designed for streaming relational data. Each object is encoded as a [type, id, attributes] tuple and marshalled directly onto the stream. The type (class name string) and id must form a distinct key when combined, attributes must consist of only string keys and simply typed values.

Relationships between objects in the stream are managed as follows:

  • An object's attributes may encode references to objects that precede it in the stream using a simple tuple format: [:id, 'User', 1234].

  • The dump side ensures that objects are written to the dump stream in "reference order" such that when an object A includes a reference attribute to an object B, B is guaranteed to arrive before A.

  • The load side maintains a mapping of ids from the dumping system to the newly replicated objects on the loading system. When the loader encounters a reference value [:id, 'User', 1234] in an object's attributes, it converts it to the load side id value.

Dumping and loading happens in a streaming fashion. There is no limit on the number of objects included in the stream.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言