diff --git a/README.md b/README.md index b3f0158..3331bf6 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,11 @@ EvilSeed.configure do |config| # Or for certain association only root.limit_associations_size(10, 'forum.questions') + + # Limit the depth of associations to be dumped from the root level + # All traverses through has_many, belongs_to, etc are counted + # So forum.subforums.subforums.questions.answers will be 5 levels deep + root.limit_deep(10) end # Everything you can pass to +where+ method will work as constraints: @@ -95,6 +100,20 @@ EvilSeed.configure do |config| # This will remove the columns even if the model is not a root node and is # dumped via an association. config.ignore_columns("Profile", :name) + + # Disable foreign key nullification for records that are not included in the dump + # By default, EvilSeed will nullify foreign keys for records that are not included in the dump + config.dont_nullify = true + + # Unscope relations to include soft-deleted records etc + # This is useful when you want to include all records, including those that are hidden by default + # By default, EvilSeed will abide default scope of models + config.unscoped = true + + # Verbose mode will print out the progress of the dump to the console along with writing the file + # By default, verbose mode is off + config.verbose = true + config.verbose_sql = true end ``` diff --git a/lib/evil_seed/configuration.rb b/lib/evil_seed/configuration.rb index df032ca..b69a954 100644 --- a/lib/evil_seed/configuration.rb +++ b/lib/evil_seed/configuration.rb @@ -13,7 +13,7 @@ def initialize @record_dumper_class = RecordDumper @verbose = false @verbose_sql = false - @unscoped = true + @unscoped = false @dont_nullify = false @ignored_columns = Hash.new { |h, k| h[k] = [] } end diff --git a/lib/evil_seed/configuration/root.rb b/lib/evil_seed/configuration/root.rb index 12a518a..933314a 100644 --- a/lib/evil_seed/configuration/root.rb +++ b/lib/evil_seed/configuration/root.rb @@ -26,7 +26,7 @@ def exclude(*association_patterns) @exclusions += association_patterns end - # Exclude some of associations from the dump + # Include some excluded associations back to the dump # @param association_patterns Array Patterns to exclude associated models from dump def include(*association_patterns) @inclusions += association_patterns diff --git a/test/db/models.rb b/test/db/models.rb index 5fa4217..a71ffc6 100644 --- a/test/db/models.rb +++ b/test/db/models.rb @@ -49,6 +49,8 @@ class Answer < ActiveRecord::Base has_many :votes, as: :votable has_many :voters, through: :votes, source: :user + + default_scope { where(deleted_at: nil) } end class TrackingPixel < ActiveRecord::Base diff --git a/test/db/schema.rb b/test/db/schema.rb index 30c20de..308bc29 100644 --- a/test/db/schema.rb +++ b/test/db/schema.rb @@ -40,6 +40,7 @@ def create_schema! t.boolean :best, default: false t.text :text t.references :author + t.datetime :deleted_at t.timestamps null: false end add_foreign_key :answers, :users, column: :author_id, on_delete: :nullify diff --git a/test/db/seeds.rb b/test/db/seeds.rb index 2a2f77c..25b8a64 100644 --- a/test/db/seeds.rb +++ b/test/db/seeds.rb @@ -43,6 +43,12 @@ author: User.find_by!(login: 'bob'), ) +answer = question.answers.create!( + text: 'Oops, I was wrong', + author: User.find_by!(login: 'eva'), + deleted_at: Time.current, +) + answer.votes.create!(user: User.find_by!(login: 'eva')) question_attrs = %w[first second third fourth fifth].map { |name| { name: name, forum: forums.first } } diff --git a/test/evil_seed/dumper_test.rb b/test/evil_seed/dumper_test.rb index e086c77..dfbc7b9 100644 --- a/test/evil_seed/dumper_test.rb +++ b/test/evil_seed/dumper_test.rb @@ -42,6 +42,7 @@ def test_it_dumps_tree_structures_with_foreign_keys assert_match(/'fourth'/, result) assert_match(/'fifth'/, result) assert result.index(/'One'/) < result.index(/'Descendant forum'/) + refute_match(/'Oops, I was wrong'/, result) end def test_limits_being_applied @@ -59,5 +60,22 @@ def test_limits_being_applied assert_match(/'fourth'/, result) refute_match(/'fifth'/, result) end + + def test_it_applies_unscoping_and_inclusions + configuration = EvilSeed::Configuration.new + configuration.root('Forum', name: 'Descendant forum') do |root| + root.include(/forum(\.parent(\.questions(\.answers)?)?)?\z/) + root.exclude(/.\..+/) + end + configuration.unscoped = true + + io = StringIO.new + EvilSeed::Dumper.new(configuration).call(io) + result = io.string + File.write(File.join('tmp', "#{__method__}.sql"), result) + assert io.closed? + assert_match(/'Descendant forum'/, result) + assert_match(/'Oops, I was wrong'/, result) + end end end