Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
E

evil-seed

> 后端框架
Open source

A Gem for creating partial anonymized dumps of your database using your app model relations.

566 stars0 likes5 views
WebsiteGitHub

About

A Gem for creating partial anonymized dumps of your database using your app model relations.

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 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyactiverecorddatabasedatabase-dumpmysql

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

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