Why are DB tasks missing from rake? - ruby-on-rails

When I run bundle exec rake db:setup I get an error saying that rake doesn't know how to build that task. I then run bundle exec rake -T and see that db tasks are missing (all output below). How can I access the db tasks?
--application.rb--
require File.expand_path('../boot', __FILE__)
require 'rails'
require 'active_model/railtie'
require 'active_job/railtie'
require 'action_controller/railtie'
require 'action_view/railtie'
require 'sprockets/railtie'
Bundler.require(*Rails.groups)
module LofocatsUi
class Application < Rails::Application
end
end
--rake command--
bos-mp478:lofocats_ui rabdelaz$ bundle exec rake db:setup --trace
rake aborted!
Don't know how to build task 'db:setup' (See the list of available tasks with `rake --tasks`)
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/gems/rake-12.3.2/lib/rake/task_manager.rb:59:in `[]'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/gems/rake-12.3.2/lib/rake/application.rb:159:in `invoke_task'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/gems/rake-12.3.2/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/gems/rake-12.3.2/lib/rake/application.rb:116:in `each'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/gems/rake-12.3.2/lib/rake/application.rb:116:in `block in top_level'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/gems/rake-12.3.2/lib/rake/application.rb:125:in `run_with_threads'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/gems/rake-12.3.2/lib/rake/application.rb:110:in `top_level'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/gems/rake-12.3.2/lib/rake/application.rb:83:in `block in run'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/gems/rake-12.3.2/lib/rake/application.rb:186:in `standard_exception_handling'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/gems/rake-12.3.2/lib/rake/application.rb:80:in `run'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/bin/rake:23:in `load'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#lofocats-ui/bin/rake:23:in `<top (required)>'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/lib/ruby/site_ruby/2.4.0/bundler/cli/exec.rb:74:in `load'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/lib/ruby/site_ruby/2.4.0/bundler/cli/exec.rb:74:in `kernel_load'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/lib/ruby/site_ruby/2.4.0/bundler/cli/exec.rb:28:in `run'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/lib/ruby/site_ruby/2.4.0/bundler/cli.rb:424:in `exec'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/lib/ruby/site_ruby/2.4.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/lib/ruby/site_ruby/2.4.0/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/lib/ruby/site_ruby/2.4.0/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/lib/ruby/site_ruby/2.4.0/bundler/cli.rb:27:in `dispatch'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/lib/ruby/site_ruby/2.4.0/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/lib/ruby/site_ruby/2.4.0/bundler/cli.rb:18:in `start'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/bin/bundle:30:in `block in <main>'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/lib/ruby/site_ruby/2.4.0/bundler/friendly_errors.rb:124:in `with_friendly_errors'
/Users/rabdelaz/.rvm/rubies/ruby-2.4.3/bin/bundle:22:in `<main>'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#global/bin/ruby_executable_hooks:24:in `eval'
/Users/rabdelaz/.rvm/gems/ruby-2.4.3#global/bin/ruby_executable_hooks:24:in `<main>'
--tasks list--
bos-mp478:lofocats_ui rabdelaz$ bundle exec rake -T
rake about # List versions of all Rails frameworks and the environment
rake assets:clean[keep] # Remove old compiled assets
rake assets:clobber # Remove compiled assets
rake assets:environment # Load asset compile environment
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake cache_digests:dependencies # Lookup first-level dependencies for TEMPLATE (like messages/show or comments/_comment.html)
rake cache_digests:nested_dependencies # Lookup nested dependencies for TEMPLATE (like messages/show or comments/_comment.html)
rake doc:app # Generate docs for the app -- also available doc:rails, doc:guides (options: TEMPLATE=/rdoc-template.rb, TITLE="Custom Title")
rake log:clear # Truncates all *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)
rake middleware # Prints out your Rack middleware stack
rake notes # Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)
rake notes:custom # Enumerate a custom annotation, specify with ANNOTATION=CUSTOM
rake rails:template # Applies the template supplied by LOCATION=(/path/to/template) or URL
rake rails:update # Update configs and some other initially generated files (or use just update:configs or update:bin)
rake routes # Print out all defined routes in match order, with names
rake secret # Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie sessions)
rake stats # Report code statistics (KLOCs, etc) from the application or engine
rake time:zones:all # Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids

Related

Where is db:create defined in Rails?

The following command is supposed to create a new database.
rails db:create
Where is this function defined? Or is this a pre-packaged function in rails?
It's in the databases.rake file of the framework:
namespace :create do
task all: :load_config do
ActiveRecord::Tasks::DatabaseTasks.create_all
end
ActiveRecord::Tasks::DatabaseTasks.for_each do |spec_name|
desc "Create #{spec_name} database for current environment"
task spec_name => :load_config do
db_config = ActiveRecord::DatabaseConfigurations.config_for_env_and_spec(Rails.env, spec_name)
ActiveRecord::Tasks::DatabaseTasks.create(db_config.config)
end
end
end
Whenever you doubt or want to know where a task has been defined, you can use the rails -W (or rake) command, passing the task:
$ rails -W db:create
rails db:create /path/databases.rake:26:in `block in <top (required)>'
rails db:create:all /path/databases.rake:20:in `block (2 levels) in <top (required)>'
Note it was introduced in the version 0.9 of Rake. This might or might not work depending on the versions which you're working with.

rake db:test:prepare erroring out due to PG error

Some of my rSpec tests are failing and I suspect it's due to the test database not being cleared out. When I try to run rake db:test:prepare, I'm getting the error below (i'm in Rails 3.2). Clearly, this looks like some kind of Postgres issue, looks like the rake task is trying to drop the test database so it can repopulate it. Production database schema loads fine.
I've looked for the rake task in lib/tasks/ but I haven't been able to find it. Anyone have any idea on what's going on?
osx_user-> rake db:test:prepare
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
rake aborted!
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
LINE 1: DROP DATABASE IF EXISTS ""
^
: DROP DATABASE IF EXISTS ""
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/postgresql_adapter.rb:650:in `async_exec'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/postgresql_adapter.rb:650:in `block in execute'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activesupport-3.2.14/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/postgresql_adapter.rb:649:in `execute'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/postgresql_adapter.rb:772:in `drop_database'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/railties/databases.rake:623:in `drop_database'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/railties/databases.rake:532:in `block (3 levels) in <top (required)>'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/railties/databases.rake:559:in `block (3 levels) in <top (required)>'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/bin/ruby_executable_hooks:15:in `eval'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:test:load => db:test:purge
(See full trace by running task with --trace)
Figured it out. My database.yml file for the development environment was set like this:
database: <%= ENV['DATABASE_NAME_TEST'] %>
The rake task was looking for an environmental variable that did not exist.

Enhance assets:precompile on Heroku to add non fingerptinted filenames

In a Rails 4 app, I want precompiled assets to have copies of some assets to be available with a non fingerprinted filename. The app is deployed on Heroku.
I have the following rake task in place:
namespace :app do
task :nonfingerprint_assets => :environment do
manifests = Rails.application.config.assets[:precompile]
manifests = manifests.slice(1, manifests.length)
assets = Dir.glob(File.join(Rails.root, 'public/assets/**/*'))
regex = /(-{1}[a-z0-9]{32}*\.{1}){1}/
assets.each do |file|
next if File.directory?(file) || file !~ regex
source = file.split('/')
source.push(source.pop.gsub(regex, '.'))
non_digested = File.join(source)
if manifests.any? {|m|
if m.is_a? Regexp
m.match non_digested
else
m.ends_with?(non_digested)
end
}
puts "** copy source: #{file}"
puts "** copy destination: #{non_digested}"
FileUtils.cp(file, non_digested)
end
end
end
end
Rake::Task['assets:precompile'].enhance do
puts "Invoke nonfingerprint_assets"
Rake::Task['app:nonfingerprint_assets'].invoke
end
This rake task is working fine on my local machine even in production environment. It creates a file in public/assets/application.js which is the copy of the same file with a digest in its name.
When I deploy this code to heroku I get the following error message which I don't understand:
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_
locales = false to avoid this message.
Invoke nonfingerprint_assets
** copy source: /tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/public/assets/application-51d1660b66655782ab137ee98c789fdd.js
** copy destination: /tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/public/assets/application.js
rake aborted!
No such file or directory - /tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/public/assets/application.js
/tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/lib/tasks/production.rake:26:in `block (3 levels) in <top (required)>'
/tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/lib/tasks/production.rake:9:in `each'
/tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/lib/tasks/production.rake:9:in `block (2 levels) in <top (required)>'
/tmp/build_48b86bff-83f9-4d21-8b21-712c1baef811/lib/tasks/production.rake:45:in `block in <top (required)>'
Tasks: TOP => app:nonfingerprint_assets
(See full trace by running task with --trace)
Please help me to understand what's going on.

Clockwork / Clockworkd produces 'No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)'

I'm running clockwork in the background using Clockworkd. If I just run clockwork, it executes the rake command successfully. If I run it via clockworkd, it produces an error. I'm not sure why it isn't working. Any advice / suggestions would be appreciated.
Starting clockworkd
RAILS_ENV=production clockworkd -c lib/clock.rb start --log
The error
I, [2013-09-05T17:53:01.035923 #2580] INFO -- : Triggering 'Get Updates'
rake aborted!
No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
/Users/lukesmith/.rvm/gems/ruby-1.9.3-p327/gems/rake-10.1.0/lib/rake/application.rb:632:in `raw_load_rakefile'
/Users/lukesmith/.rvm/gems/ruby-1.9.3-p327/gems/rake-10.1.0/lib/rake/application.rb:94:in `block in load_rakefile'
/Users/lukesmith/.rvm/gems/ruby-1.9.3-p327/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/Users/lukesmith/.rvm/gems/ruby-1.9.3-p327/gems/rake-10.1.0/lib/rake/application.rb:93:in `load_rakefile'
/Users/lukesmith/.rvm/gems/ruby-1.9.3-p327/gems/rake-10.1.0/lib/rake/application.rb:77:in `block in run'
/Users/lukesmith/.rvm/gems/ruby-1.9.3-p327/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/Users/lukesmith/.rvm/gems/ruby-1.9.3-p327/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/Users/lukesmith/.rvm/gems/ruby-1.9.3-p327/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
/Users/lukesmith/.rvm/gems/ruby-1.9.3-p327/bin/rake:23:in `load'
/Users/lukesmith/.rvm/gems/ruby-1.9.3-p327/bin/rake:23:in `<main>'
clock.rb
require 'clockwork'
require File.expand_path('../../config/boot', __FILE__)
require File.expand_path('../../config/environment', __FILE__)
include Clockwork
handler do |job|
puts "Running #{job}"
end
every(1.hour, 'Get Updates') { `rake get_updates` }
The process running your rake get_updates task may not be in the same working directory as your Rakefile is located.
You could either Dir.chdir '/dir/containing/Rakefile/', or else explicitly pass the path to the Rakefile using the -f option:
every(1.hour, 'Get Updates') do
Dir.chdir '/dir/containing/Rakefile/'
`rake get_updates`
end

issue with deployment of my ruby on rails app

I'm trying to solve a problem I'm having lately, deploying from a github production branch with cap: production deploy:migrations to my server.
I saw a potential solution was to follow this page: http://www.kudelabs.com/2012/03/28/rails-3-2-cap-deploy-with-assets
However I got errors at the rake assets:precompile part, running it locally. Terminal suggested I do
bundle exec rake assets:precompile, which I did, but I got the following:
root#ubuntu:~/myapp# bundle exec rake assets:precompile
DEPRECATION WARNING: require "activerecord" is deprecated and will be removed in Rails 3. Use require "active_record" instead. (called from /usr/lib/ruby/vendor_ruby/activerecord.rb:2)
/usr/bin/ruby1.8 /usr/local/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
DEPRECATION WARNING: require "activerecord" is deprecated and will be removed in Rails 3. Use require "active_record" instead. (called from /usr/lib/ruby/vendor_ruby/activerecord.rb:2)
rake aborted!
Received wrong number of arguments. [nil]
/var/lib/gems/1.8/gems/omniauth-1.1.0/lib/omniauth/strategy.rb:136:in `initialize'
/var/lib/gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/stack.rb:43:in `new'
/var/lib/gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/stack.rb:43:in `build'
/var/lib/gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/stack.rb:113:in `build'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/application.rb:282:in `inject'
/var/lib/gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/stack.rb:113:in `each'
/var/lib/gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/stack.rb:113:in `inject'
/var/lib/gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/stack.rb:113:in `build'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/engine.rb:470:in `build_middleware_stack'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/application/finisher.rb:31
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `instance_exec'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `run'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/initializable.rb:55:in `run_initializers'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `each'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `run_initializers'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/application.rb:136:in `initialize!'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in `send'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/christophecompaq/myapp/config/environment.rb:5
/var/lib/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
/var/lib/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
/var/lib/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
/var/lib/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/application.rb:103:in `require_environment!'
/var/lib/gems/1.8/gems/railties-3.2.3/lib/rails/application.rb:292:in `initialize_tasks'
/var/lib/gems/1.8/gems/actionpack-3.2.3/lib/sprockets/assets.rake:93
/var/lib/gems/1.8/gems/actionpack-3.2.3/lib/sprockets/assets.rake:60
Tasks: TOP => environment
(See full trace by running task with --trace)
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 /usr/local/bin/rake asset...]
/var/lib/gems/1.8/gems/actionpack-3.2.3/lib/sprockets/assets.rake:12:in `ruby_rake_task'
/var/lib/gems/1.8/gems/actionpack-3.2.3/lib/sprockets/assets.rake:21:in `invoke_or_reboot_rake_task'
/var/lib/gems/1.8/gems/actionpack-3.2.3/lib/sprockets/assets.rake:29
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
root#ubuntu:~/myapp#
Can anyone tell me where the problem might be?
If this is an initial deploy, try to remove
load 'deploy/assets'
Then try to
cap deploy:setup
cap deploy:cold
In this case you app will be deployed but without any assets :)
Then you need to add this:
#to the top of your deploy.rb namespace :deploy block
namespace :assets do
task :precompile, :roles => :web do
from = source.next_revision(current_revision)
if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ lib/assets/ app/assets/ | wc -l").to_i > 0
run_locally("rake assets:clean && rake assets:precompile")
run_locally "cd public && tar -jcf assets.tar.bz2 assets"
top.upload "public/assets.tar.bz2", "#{shared_path}", :via => :scp
run "cd #{shared_path} && tar -jxf assets.tar.bz2 && rm assets.tar.bz2"
run_locally "rm public/assets.tar.bz2"
run_locally("rake assets:clean")
else
logger.info "Skipping asset precompilation because there were no asset changes"
end
end
task :symlink, roles: :web do
run ("rm -rf #{latest_release}/public/assets &&
mkdir -p #{latest_release}/public &&
mkdir -p #{shared_path}/assets &&
ln -s #{shared_path}/assets #{latest_release}/public/assets")
end
end
Then recheck if you have this in your production.rb
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
After all this work, change some thing in you assets, for example add new line in application.css, this will tell "task :precompile" to realy precomlile and upload new assets, because new assets was added..

Resources