seedbank icon indicating copy to clipboard operation
seedbank copied to clipboard

Seed files inside engines dont work

Open pmackay opened this issue 10 years ago • 6 comments

I'm attempting to define seed data inside an engine in a subdirectory of my app. Running the db:seed command from inside the engine doesnt work, showing:

Don't know how to build task 'db:seed:original'

This is with a seeds.rb in the /db directory. In the root app /db directory it works fine.

pmackay avatar Aug 12 '14 12:08 pmackay

Good point. I think I will look into this more.

Note to self (or anybody that wants to have a go :)

ActiveRecord::Tasks::DatabaseTasks defines a seed_loader;

      def seed_loader
        @seed_loader ||= Rails.application
      end

Then it provides load_seed to call load_seed on the seed_loader

      def load_seed
        if seed_loader
          seed_loader.load_seed
        else
          raise "You tried to load seed data, but no seed loader is specified. Please specify seed " +
                "loader with ActiveRecord::Tasks::DatabaseTasks.seed_loader = your_seed_loader\n" +
                "Seed loader should respond to load_seed method"
        end
      end

By default Rails.application provides the load_seed method. So it should be fair and reasonable to replace that method as provided in Railties.

james2m avatar Oct 14 '14 19:10 james2m

+1. I have an engine that provides a access to a secondary database, and I can't use Seedbank as a utility for that engine nor the host application.

My engine provides a secondary DB folder that holds all the migrations, schema, seeds, etc. All the rake db:.. tasks have been set up in a namespace enginename:db:..., which was easy to do by simply overriding some Rails.application.config.paths in the environment setup task.

But Seedbank hardcodes all its tasks at early runtime, before I'm able to override Rails.application.config.paths['db/seeds.rb'] and Seedbank.seeds_root.

I understand the custom-named tasks that are setup using whatever directory names exist have to be setup at runtime, but surely db:seed:common, db:seed:environment, and thus db:seed could be set to pull their paths dynamically when they are called. And if Seedbank.seeds_root relied on Rails.application.config.paths['db'], getting these working in any namespaced rake environment would require zero additional code from engine developers.

Food for thought, I know that's some work (or otherwise I'd have a pull request for you already).

zrisher avatar Jan 20 '15 09:01 zrisher

You should be able to manually load the tasks while inside the engine. For example by adding

Seedbank.seeds_root = File.expand_path('db/seeds', YourEngine::Engine.root) # assuming db/seeds/**/* are where you seed files are
Seedbank.load_tasks

to the engine's Rakefile.

nanzhong avatar Feb 05 '16 19:02 nanzhong

@nanzhong this is not working for me. @zrisher were you able to solve this problem?

jimedelstein avatar Feb 23 '16 14:02 jimedelstein

@jimedelstein Negative.

zrisher avatar Feb 24 '16 22:02 zrisher

It was never designed to work with Engines, I'm just open to the idea, but don't have the time to hack on it that right now. Would happily take a PR.

james2m avatar Aug 17 '16 18:08 james2m