evilmartians / evil-seed

A Gem for creating partial anonymized dumps of your database using your app model relations.
MIT License
447 stars 19 forks source link

Please provide a full example for non ruby developper #5

Closed jrweb247 closed 6 years ago

jrweb247 commented 6 years ago

Hi,

I'm very interesting by your script but I not a ruby developer, so I don't know how to configure active records according to my mysql credentials !

thanks a lot

Envek commented 6 years ago

@jrweb247, hi!

This gem is not a standalone solution and aimed to be used with existing applications that are using ActiveRecord (Ruby on Rails or Sinatra or anything).


In case of Rails, this gem is intended to use an existing configuration of your application. So just configure your database.yml or specify DATABASE_URL environment variable.


If you want to use it as a standalone application, you can place exerything in single file like this:

#!/usr/bin/env ruby

require 'bundler/inline'

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

require 'tempfile'

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:

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

Here you need to

  1. Describe your database layout with ActiveRecord models (see http://guides.rubyonrails.org/active_record_basics.html)
  2. Connect to your database (see http://guides.rubyonrails.org/configuring.html#configuring-a-database)
  3. Start dumping
Envek commented 6 years ago

@jrweb247, feel free to reopen this issue if you have any questions left.