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

evil-seed

> 后端框架
开源

用于利用应用程序模型关系创建部分匿名数据库备份的 Gem。

566 stars0 点赞5 次浏览
访问官网GitHub

工具介绍

用于利用应用程序模型关系创建部分匿名数据库备份的 Gem。

EvilSeed

EvilSeed is a tool for creating partial anonymized dump of your database based on your app models.

EvilSeed is built by Evil Martians, an American design and engineering consultancy for developer tools, AI, and cybersecurity startups.

Motivation

Using production-like data in your staging environment could be very useful, especially for debugging intricate production bugs.

The easiest way to achieve this is to use production database backups. But that's not an option for rather large applications for two reasons:

  • production dump can be extremely large, and it just can't be dumped and restored in a reasonable time

  • you should care about sensitive data (anonymization).

EvilSeed aims to solve these problems.

Installation

Add this line to your application's Gemfile:

ruby
gem 'evil-seed', require: false

And then execute:

$ bundle

Or install it yourself as:

$ gem install evil-seed

Usage

Configuration

…

Creating dump

Just call the #dump method and pass a path where you want your SQL dump file to appear!

ruby
require 'evil_seed'
EvilSeed.dump('path/to/new_dump.sql')

Caveats, tips, and tricks

  1. Specify roots for dictionaries and system-wide models like Role at the top without constraints and with all associations excluded.

  2. Use exclude aggressively. You will be amazed, how much your app's models graph is connected. This, in conjunction with the fact that this gem traverses associations in deep-first fashion, sometimes leads to unwanted results: some records will get into dump even if you don't want them.

  3. Look at the resulted dump: there are some useful debug comments.

Database compatibility

This gem has been tested against:

  • PostgreSQL: any version that works with ActiveRecord should work
  • MySQL: any version that works with ActiveRecord should work
  • SQLite: 3.7.11 or newer is required (with support for inserting multiple rows at a time)

Restoring dump

Resulting dump is a plain SQL file: you can restore it using any SQL client like psql, mysql, sqlite3, etc.

If you need to do it from Ruby, you can use the following code:

ruby
ActiveRecord::Base.connection.execute(File.read('path/to/new_dump.sql'))

Restoration tips and tricks

  1. Reset primary key sequences after restoration, so default seeds can be generated afterwards and your app will work as expected:

    ruby
    ActiveRecord::Base.connection.tables.each do |table|
      ActiveRecord::Base.connection.reset_pk_sequence!(table)
    end
  2. To restore dumps with circular dependencies between records in PostgreSQL you can make all foreign keys deferrable beforehand (by default they are not) and restore the dump in a transaction with all foreign keys deferred.

    Code to defer, restore, undefer:

…

ruby #!/usr/bin/env ruby

require 'bundler/inline'

gemfile do source 'https://rubygems.org' gem 'activerecord' gem 'evil-seed' gem 'mysql2' end

Describe your database layout with ActiveRecord models.

See http://guides.rubyonrails.org/active_record_basics.html

class Category < ActiveRecord::Base has_many :translations, class_name: "Category::Translation" end

class Category::Translation < ActiveRecord::Base belongs_to :category, inverse_of: :translations end

Configure evil-seed itself

EvilSeed.configure do |config| config.root("Category", "id < ?", 1000) end

Connect to your database.

See http://guides.rubyonrails.org/configuring.html#configuring-a-database)

ActiveRecord::Base.establish_connection(ENV.fetch("DATABASE_URL"))

Create dump in dump.sql file in the same directory as this script

EvilSeed.dump(File.join(dir, "dump.sql").to_s)


And launch it like so:

```sh
DATABASE_URL=mysql2://user:pass@host/db ruby path/to/your/script.rb

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/palkan/evil-seed.

License

The gem is available as open source under the terms of the MIT License.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

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

> 标签

Rubyactiverecorddatabasedatabase-dumpmysql

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

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架