seedbank
seedbank copied to clipboard
Seed files inside engines dont work
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.
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.
+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).
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 this is not working for me. @zrisher were you able to solve this problem?
@jimedelstein Negative.
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.