A Gem for creating partial anonymized dumps of your database using your app model relations.
A Gem for creating partial anonymized dumps of your database using your app model relations.
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.
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.
Add this line to your application's Gemfile:
gem 'evil-seed', require: falseAnd then execute:
$ bundleOr install it yourself as:
$ gem install evil-seed…Just call the #dump method and pass a path where you want your SQL dump file to appear!
require 'evil_seed'
EvilSeed.dump('path/to/new_dump.sql')Specify roots for dictionaries and system-wide models like Role at the top without constraints and with all associations excluded.
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.
Look at the resulted dump: there are some useful debug comments.
This gem has been tested against:
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:
ActiveRecord::Base.connection.execute(File.read('path/to/new_dump.sql'))Reset primary key sequences after restoration, so default seeds can be generated afterwards and your app will work as expected:
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.reset_pk_sequence!(table)
endTo 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
class Category < ActiveRecord::Base has_many :translations, class_name: "Category::Translation" end
class Category::Translation < ActiveRecord::Base belongs_to :category, inverse_of: :translations end
EvilSeed.configure do |config| config.root("Category", "id < ?", 1000) end
ActiveRecord::Base.establish_connection(ENV.fetch("DATABASE_URL"))
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.rbAfter 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.
Bug reports and pull requests are welcome on GitHub at https://github.com/palkan/evil-seed.
The gem is available as open source under the terms of the MIT License.
No open issues yet, or sync has not completed.