how to run rake db:migrate on irb - ruby

I am trying to run a migration script on irb but it returns syntax error.
irb(main):008:0> rake db:migrate:up VERSION=20171006190045
SyntaxError: (irb):8: syntax error, unexpected tLABEL
rake db:migrate:up VERSION=20171006190045
tried rake db:migrate:redo VERSION=20171006190045
Also tried this
irb(main):012:0> require 'db/migrate/20171006190045_update_details.rb'
LoadError: cannot load such file -- db/migrate/20171006190045_update_details.rb
from (irb):12

rake is not meant to be run within IRB. I agree with #spickermann. You can run it in rails console by using system command.
$ rails c
> system("rake db:migrate:up VERSION=20171006190045")
or simply in terminal
$ rake db:migrate:up VERSION=20171006190045

rake is not meant to be run within IRB. It is a command line program like IRB. Just exit IRB and run it in your terminal.

irb does not respond to rake. Exit out of irb like so:
irb(main):008:0> exit
Then simply run your rake command:
rake db:migrate:up VERSION=20171006190045

No, it is not necessary to run it using system command.
Rails.application.load_tasks
Rake::Task['my_task'].invoke

Related

Rspreadsheet gem

I want to use the rspreadsheet gem, first I tried with bundler but each time there is this error: `require ': cannot load such file - rspreadsheet.
So I installed the gem directly and when I run my program to test:
workbook = Rspreadsheet.open('./print.ods')
sheet = workbook.worksheet(1)
It tells me: uninitialized constant Rspreadsheet (NameError).
Assuming your script is named script.rb and you have already run bundle install could you try running bundle exec ruby script.rb to see if that works?

Ruby "no such file to load" error running spec from rake task

I have written tests using rspec. When I run default rake task by running
bundle exec rake spec
it gives following error, even though previously it used to work fine.
`require': no such file to load -- sinatra/base (LoadError)
but if I run tests using simple
ruby spec/humongous_spec.rb
It runs perfectly fine. I don't what is wrong with this.
Add this line to your Rakefile
require 'bundler/setup'
Bundler.require

Problems running rspec 2.8.0.rc1 within rbenv defined ruby 1.9.2p290 environment

This works:
[rails31]$ ruby -S rspec ./spec/models/domain_spec.rb
*.
Pending:
Domain add some examples to (or delete) /home/keith/Code/elements2/spec/models/domain_spec.rb
# Not Yet Implemented
# ./spec/models/domain_spec.rb:4
Finished in 0.04241 seconds
2 examples, 0 failures, 1 pending
However I'm trying to run from guard, which executes the following command:
[rails31]$ /home/keith/.rbenv/versions/1.9.2-p290/bin/ruby -S rspec ./spec/models/domain_spec.rb
/home/keith/.rbenv/versions/1.9.2-p290/bin/ruby: no Ruby script found in input (LoadError)
Breaking that down enough to execute I get this:
[rails31]$ /home/keith/.rbenv/versions/1.9.2-p290/bin/ruby ./spec/models/domain_spec.rb
/home/keith/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rspec-core-2.8.0.rc1/lib/rspec/core/configuration.rb:335:in `rescue in debug=': (RuntimeError)
**************************************************
no such file to load -- ruby-debug
If you have it installed as a ruby gem, then you need to either require
'rubygems' or configure the RUBYOPT environment variable with the value
'rubygems'.
...
This is actually where I started in the first place - I know rspec respects the "dont require rubygems" rule, so maybe I need to run rake. The big problem I have with this error is that "ruby-debug" does NOT exist for ruby1.9 - it should be ruby-debug19 - so whats happening here?
So anyway, I tried with Rake:
[rails31]$ rake spec ./spec/models/domain_spec.rb
(in /home/keith/Code/elements2)
rake aborted!
uninitialized constant Rake::DSL
...
I've tried Googling the problem but nothing obvious is coming up, so really I'm stumped
UPDATE:
Well after reading this post I've managed to resolve the rake issue, however now when I run rake I get this:
[rails31]$ rake spec
(in /home/keith/Code/elements2)
/home/keith/.rbenv/versions/1.9.2-p290/bin/ruby -S rspec ./spec/models/domain_spec.rb
/home/keith/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rspec-core-2.8.0.rc1/lib/rspec/core/configuration.rb:335:in `rescue in debug=': (RuntimeError)
...

Rails 3 migration error

I am new to Ruby and Rails, and whenever I attempt to generate my database using rake db:migrate I get the following error:
rake aborted!
You have already activated rake 0.9.3.beta.1, but your Gemfile requires rake 0.9.2. Consider using bundle exec.
I am using Ruby 1.9.2 and Rails 3.
Consider using bundle exec.
There's your clue. Try this instead:
bundle exec rake db:migrate

rake command looks for Rakefile up the directory chain?

I am using Rails3 and accidentally I ran rake command from within lib directory. rake command successfully ran. I think rake command looks for Rakefile all the up the chain. Is that true?
Yes. According to http://docs.rubyrake.org/user_guide/chapter02.html you can avoid that by:
rake -N

Resources