rake db:seed is not working using neo4j gem - ruby-on-rails

I have stated working on neo4j with rails using the gem 'neo4j', I want to seed some data in neo4j database. But whenever I am trying to do rake db:seed, it says
rake aborted!
Don't know how to build task 'db:seed'
I have checked all the rake tasks using rake -T, and there is no rake db:seed.
Does any one have any idea?

The Neo4j gem doesn't have a seed command. The command you're trying to use is provided by ActiveRecord. We'd love to add this functionality in, though, and if you'd like to help, we'd gladly accept a PR and/or contribute to the process. For now, open up an issue at https://github.com/neo4jrb/neo4j/issues and we can add it to the roadmap.

Finally got the solutions.
Create a file seed.rake under lib/tasks and put the code
namespace :db do
desc 'Load the seed data from db/seeds.rb'
task :seed => :environment do
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
load(seed_file) if File.exist?(seed_file)
end
end

Check apps root directory, is there a Rakefile?
Make a file with name "Rakefile" and enter these line
"#!/usr/bin/env rake
require File.expand_path('../config/application', FILE)
APP_NAME::Application.load_tasks

As others mentioned before, rails db:seed is a built-in feature backed by ActiveRecord. Assuming you are using neo4jrb without ActiveRecord, you won't be able to use this command to seed database any more.
However, you can use seedbank gem to recover this ability.
It does not depend on ActiveRecord like seed_fu and other seeding gems do. So it will work just fine with neo4jrb.
The process is rather simple.
put seedbank in your Gemfile and bundle install.
create directory db/seeds/
create a seeds file under the db/seeds/ directory
for example
#db/seeds/show.seeds.rb
Show.create(name: "Better Call Saul", producer: "Vince Gilligan")
run rake db:seed:show

Related

`rake db:seed:shop:curtain` fail with “Don't know how to build task 'db:seed:shop:curtain'”?

ruby 2.1.5, rails 4.2, rake
installed gem "seedbank"
seedbank (0.3.0)
I have following structure:
seeds/shop/curtain/
I want run all seed files from folder curtain
rake db:seed:shop:curtain
rake aborted!
Don't know how to build task 'db:seed:shop:curtain'
What wrong, explain me please. Thank you
I have following structure: seed/shop/curtain/
As per seedbank documentation the structure should be
db/seeds/shop/curtain.seeds.rb
So you need to rename seed to seeds
and add curtain.seeds.rb inside shop folder so you will get
rake db:seed:shop:curtain
EDIT
Not sure if seedbank supports 2-3 level of hierarchy so move the curtain folder directly inside seeds and try
rake db:seed:curtain
This will load the seed data from
db/seeds.rb
db/seeds/*.seeds.rb
db/seeds/curtain/*.seeds.rb

rake aborted! cannot load such file Ruby on Rails

I am trying to run a ruby file that updates the database using the rake task . But overtime I run the code, I get an error saying :
rake aborted!
cannot load such file -- app/services/insert_data
The ruby file 'insert_data' is located under the app directory in a folder named 'services'
Here is the rake task I created to run it:
require 'app/services/insert_data'
namespace :record_generate do
task :test do
ruby "app/services/insert_data"
end
end
Please help in removing this error.
To be able to use Rails environment within a rake task you need to declare that:
namespace :record_generate do
task :test => :environment do |_, args|
ruby "app/services/insert_data"
end
end
I think you should try placing your app/services/insert_data inside the script directory. I'm assuming that your ruby file is not a rails model with logic. I would try this.
Get your file and place it in the /script/ directory.
Place these line of code at the top of your file
#! /usr/bin/env ruby
require_relative "../config/environment"
Now on your command line you can do execute this:
rails runner name_of_your_file
That is one way of doing it. If you specifically need to use a rake task I can try posting an answer for that too.
Inside your task :test block try -
Rake.application.rake_require "#{Rails.root}/app/services/insert_data"

Rails's Rake task takes exceptionally long to execute? [duplicate]

rake --tasks takes about 18s to run. This is just the time it takes to load all the tasks, as a result any task I define will take at least this amount of time to run :
$time rake --tasks
rake db:clean # Cleaning up database
rake passenger:restart # Restart Application
rake spec # Run specs
real 0m18.816s
user 0m7.306s
sys 0m5.665s
My Rakefile :
$: << "."
require "rubygems"
require "rspec/core/rake_task"
desc "Run those specs"
task :spec do
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w{--colour --format progress}
t.pattern = 'spec/*_spec.rb'
end
end
task :default => :spec
Any idea why rake takes to much times ?
Thanks
Try spring
Command line will look like:
spring rake -T
It will take more time running the first time, but subsequent runs will be very fast.
This solution worked for me: Faster rake tasks in Rails.
I had to do a little variation where I created a lib/tasks/no_rails directory and put all the Rake files which do not need Rails in there and loaded only those using the above method.
I like the solution Pratik mentions for the general case of loading rails for tasks that need it and not for those that don't, for any rake task without having to remember beforehand.
A less-invasive method to run a rake task that doesn't need rails is to use the -f rake option to tell rake to use a particular Rakefile. This way, rake won't go looking for rake tasks in all of rails.
For example, assuming your task above is in a file called Rakefile at the top level of your project and your Rakefile doesn't do anything that loads Rails like require File.expand_path('../config/application', __FILE__), you can do:
$ rake -f Rakefile spec
and it should run your spec task much faster. Try $ time rake -f Rakefile -T; I did this with a rails-independent Rakefile of mine and got:
real 0m1.543s
user 0m1.308s
sys 0m0.201s
The downside is you have to remember to specify this option every time, and not to specify it if you want to run a rake task from rails like rake db:migrate.
The entire rails environment has to be loaded, therefore even simple rake tasks such as rake --tasks take a while. Opening a console with rails console or script/console takes a similar time. You may try to hack Ruby or Rails to speed up rake, but too much optimization can be bad if you want to switch to a newer version later. Since the rails environment must be loaded, cleaning up routes may also help.

How to seed the production database using the Capistrano gem?

I am using Ruby on Rails 3.0.9 and I would like to seed the production database in order to add some record without re-building all the database (that is, without delete all existing records but just adding some of those not existing yet). I would like to do that because the new data is needed to make the application to work.
So, since I am using the Capistrano gem, I run the cap -T command in the console in order to list all available commands and to know how I can accomplish what I aim:
$ cap -T
=> ...
=> cap deploy:seed # Reload the database with seed data.
=> ...
I am not sure on the word "Reload" present in the "Reload the database with seed data." sentence. So, my question is: if I run the cap deploy:seed command in the console on my local machine will the seeding process delete all existing data in the production database and then populate it or will that command just add the new data in that database as I aim to do?
If you are using bundler, then the capistrano task should be:
namespace :deploy do
desc "reload the database with seed data"
task :seed do
run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}"
end
end
and it might be placed in a separate file, such as lib/deploy/seed.rb and included in your deploy.rb file using following command:
load 'lib/deploy/seed'
This worked for me:
task :seed do
puts "\n=== Seeding Database ===\n"
on primary :db do
within current_path do
with rails_env: fetch(:stage) do
execute :rake, 'db:seed'
end
end
end
end
capistrano 3, Rails 4
Using Capistrano 3, Rails 4, and SeedMigrations, I created a Capistrano seed.rb task under /lib/capistrano/tasks:
namespace :deploy do
desc 'Runs rake db:seed for SeedMigrations data'
task :seed => [:set_rails_env] do
on primary fetch(:migration_role) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, "db:seed"
end
end
end
end
after 'deploy:migrate', 'deploy:seed'
end
My seed migrations are now completely separate from my schema migrations, and ran following the db:migrate process. What a joy! :)
Try adding something like this in your deploy.rb:
namespace :deploy do
desc "reload the database with seed data"
task :seed do
run "cd #{current_path}; rake db:seed RAILS_ENV=#{rails_env}"
end
end
cap deploy:seed should basically be a reference to rake db:seed. It should not delete existing data, unless you specified it to do so in your seed.rb.
Best assumption for the word "Reload" is that :seed is a stateless command, I does not automatically know where it left off, like regular rails migrations. So technically you would always be "reloading" the seed, every time you run it. ...A wild guess, but it sounds good, no?
Please view Javier Vidal answer below
After a discussion with capistrano-rails gem authors I decided to implement this kind of tasks in a separate gem. I think this helps to follow the DRY idea and not implementing the same task over and over again.
I hope it helps you: https://github.com/dei79/capistrano-rails-collection

Rails - Populate test database with development data

Is there any trivial way to copy the data from developmenet database into the test one? I know theres a way to copy schema and recreate database, but is there any rake task to populate test database with development one?
You can use mysql directly:
mysqldump app_development | mysql app_test
You can use:
rake db:test:clone
To copy the development db into test.
For all databases:
rake db:test:clone && rake db:seed RAILS_ENV='test'
With Postgres, copy the database like so:
CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;
If you just want to clone the development DB in its entirety, what's wrong with just copying the development.sqlite3 and renaming it test.sqlite3? You can automate the process by setting up a batch file (or its equivalent on your OS) that you can run from the command line.
This will work locally, but I just realized you might be thinking a non-local environment, in which case it probably won't.
An alternative method if you use seeds (db/seeds.rb)
First, add a rake task for example to lib/tasks/test_seed.rake with this code:
namespace :db do
namespace :test do
task :prepare => :environment do
Rake::Task["db:seed"].invoke
end
end
end
Then whenever you changed your database structure / content through migration and seeds, you can run
rake:db:test:prepare
To copy the schema and seed data.
So the complete steps would be:
rake db:migrate
rake db:seed
rake db:test:prepare

Resources