Rake Stats task failure with code_statistics file missing issue - ruby-on-rails

Have a legacy application built on Rails 3.2.16, Ruby 1.9.3 which is being assessed for updates. Apart from the obvious in that it's running on outdated versions of both Ruby and Rails, I'm stuck with an error when trying to gather the stats.
When executing the command:
rake stats
I'm getting the error:
rake stats
rake aborted!
cannot load such file -- code_statistics
This is also correlated via the 'Rails Console' whereby issuing the command require 'code_statistics' also produces a similar error.
Now I can confirm the following
Rake task exists as shown by the rake -T giving command output of rake stats # Report code statistics (KLOCs, etc...
CodeStatistics class is in the railities library
I suspect that it's a path or file issue with potentially an environment setting that I'm missing Rails 3.2.
Any suggestions ?
Regards
Grant

Ok - so solved the problem. It was a path issue as first suspected.
After some digging in this legacy system I'm reviewing there were path changes made in rspec.rake and cuke.rake files which indicated a parent directory. Changing the path
from:
require 'code_statistics'
to
require 'rails/code_statistics'
has now resolved the issue such that rake stats gives the usual results.

Related

What is the difference between rails and rake?

What is the difference between commands rake and rails in Ruby?
Which one is faster and why?
The difference is in what binary is being called.
If you were to call bundle exec which rake within your Rails app root directory, you'd get something like /home/[USERNAME]/.rbenv/versions/2.5.5/bin/rake and for bundle exec which rails, you'd get /home/[USERNAME]/.rbenv/versions/2.5.5/bin/rails. From there you can cat (cat /home/[USERNAME]/.rbenv/versions/2.5.5/bin/rake) both these paths and see similar code is being ran for each, but the end of the files is different.
rails
gem "railties", version
load Gem.bin_path("railties", "rails", version)
rake
gem "rake", version
load Gem.bin_path("rake", "rake", version)
Here they're both calling load on Gem.bin_path but with different arguments, which are attempting to load separate gems in. You can follow the code further by running a irb/pry/rails console, and setting up the needed require 'rubygems' and version = ">= 0.a", then run Gem.bin_path("railties", "rails", version) and Gem.bin_path("rake", "rake", version) to see what the load is actually trying to run. I'll admit it'll become a bit of a rabbit hole before you come across the logic that eventually ends up identifying a rake task argument passed to rails and it proxy's it to Rake and stop there and defer to this SO answer for the rest.
When rails is ran and passed arguments which were intended to be ran by rake, it will attempt to first find if it was an actual argument intended to be given to the rails command, determine that it wasn't, then attempt to run it as a rake command for you for overall naming simplicity added in by the Rails team in Rails v4.
So which is faster to run? rake for actual rake tasks, as it'll bypass the extra logic in needing to determine it was being passed rake arguments. But also rails specific arguments cannot be ran with rake e.g. bundle exec rake generate will not work (unless you have a generate task). If in doubt, run bundle exec rails --help and in at least Rails v5, it'll output which arguments are rails specific and which are rake specific.
rake is a Make-like program implemented in Ruby.
rails is a web framework, which also has some rake tasks.
This means that you can have a ruby program with rake but without rails, but not the other way around.
By itself, rake will be faster because you don't need to load the whole rails application.
But when launching a rake task, it can have dependencies, for example the :environment dependency in a rails app, which tells rake to load the rails environment and quite a bit of your application depending on the current environment.
In this case, the initialization of a rake task may take as long as a rails command.
Please note that the actual task run needs also to be taken into account, it can be very short or take several minutes.
For example, rake db:migrate, which is a rails task available by default, runs the migrations on the database, which can be time-consuming if the database is already populated and/or you have a lot of migrations

FATAL: database «sampleapp_production» does not exits

First time here, I hope do it right.
I'm following the railstutorial 3.2 and I'm in section 5.4, in the paragraph above to listing 5.32.
(In fact, you can just type rake by itself; the default behavior of rake is to
run the test suite.)
I used rake by itselft but I have a error since then. When I run:
"$ bundle exec rake spec" I get this error.
http://pastebin.com/F0wrEkT1
My database.yml is:
http://pastebin.com/tWAgeFTV
My problem is that I don't know why it is asking for production database when I didn't use it yet. And whe I look for the issue I don't find topics about (almost I didn't find it).
Do you have some clues to star looking or to know what is happening?
Thanks a lot.
I'm not sure why it is looking for the production DB but you may want to skip ahead. Just read a little further:
(In fact, you can just type rake by itself; the default behavior of rake is to run the test suite.) The only caveat is that using rake to run the tests for the current sample application will raise an error since it requires the test database to be prepared properly, a step we’ll defer to Section 6.2.1.
It recommends running:
bundle exec rake db:test:prepare

Cannot run rake with cron tasks

I am unable to run rake tasks with cron. The error it throws up is:-
/Library/Ruby/Gems/1.8/gems/bundler-1.2.0/lib/bundler/runtime.rb:199: warning: Insecure world writable dir /Library/Ruby/Gems/1.8 in PATH, mode 040777
/Library/Ruby/Gems/1.8/gems/bundler-1.2.0/lib/bundler/spec_set.rb:90:in `materialize': Could not find unf_ext-0.0.5 in any of the sources (Bundler::GemNotFound)
However, when I run the same rake task directly from the terminal, it works fine. In case of the cron job, as well as while running the rake task directly from the terminal, i cd into the (exact) same directory. Starting the rails server works as well.
What is the problem and how should I resolve this?
It could be an issue with your daemon user and your user when you're running it directly. Or it could be a problem with Ruby and/or your cron file commands.
Firstly, use this gem for making cron jobs with rake tasks:
https://github.com/javan/whenever
Then install RVM since you're using the standard ruby library provided by OSX and that can lead to some problems when you're developing various projects. This should fix the permissions error that you have displayed.

Installing migrations from engine won't work

I created a new engine in Rails 3.1.3 and apparently there's that rake task that copies over all migrations. I tried following rake abc:install:migrations which threw:
rake aborted!
Don't know how to build task 'abc:install:migrations'
(See full trace by running task with --trace)
I also tried rake abc_engine:install:migrations with the same result.
Then I read bundle exec rake railties:install:migrations or bundle exec rake railties:install:migrations FROM=abc_engine should do the trick too but no success. Nothing was copied even though no error was thrown.
My migrations are located in db/migrate/ within the engine folder and I ran all commands above from spec/dummy/
Does anyone know how to use this new rake task in order to copy migrations from the engine?
I ran this instead:
rake railties:install:migrations
And my migrations were copied from the engine.
Hope this helps.
I finally got found/got lucky with my (similar) issue. For the first error, it just disappeared, not sure why. Then I figured out that I didn't created the migrations using the usual file name format, so the ActiveRecord::Migrator.migrations method was ignoring them.
If the app you're mounting the engine to doesn't already have ActiveRecord (i.e. you're introducing ActiveRecord to your host app for the first time by mounting the engine), you can get this error as well. Specifically, you'll get this error if you don't have require "active_record/railtie" in your application.rb, or if it's commented out. That line is what enables the rake railties:install:migrations task, which is defined here. rake railties:install:migrations is, in turn, called by the rake abc_engine:install:migrations task here.
Tl;dr: try adding require "active_record/railtie" to your application.rb if it's not already there.

Spree installation issue : rake aborted! couldn't parse YAML at line 6 column 50

I am trying to install the spree gem using the following instructions http://spreecommerce.com/resources/quick-start
I am getting following error on step 4.6(Populating the Database)
$ rake db:bootstrap
.....
.......
rake aborted!
couldn't parse YAML at line 6 column 50
....
.....
I am using ruby 1.9.2p0, rails 3.0.3 and spree 0.40.2.
Can anyone please tell me what could be the problem?
There's probably an error in a YAML file, not surprisingly. It's likely a fixture used in the bootstrapping process.
You can always run rake with the --trace option to get a better sense of what it was trying to do. Have a look through your fixture files to see if any are invalid, especially at the position indicated.
In boot.rb file inside config directory of my rails project home I added following and it worked.
require 'yaml'
YAML::ENGINE.yamler= 'syck'
I followed instruction from http://www.ruby-forum.com/topic/1002689

Resources