I'm building a Rails Engine inside another rails app. The engine's rake tasks seem to get scoped to app:
# inside <app_root>/engines/engine_name
$ rake -T apartment
rake app:apartment:migrate # Migrate all tenants
Whereas from the app's root:
#inside <app_root>/
$ rake -T apartment
rake apartment:migrate # Migrate all tenants
My problem is when I run
# inside <app_root>/engines/engine_name
RAILS_ENV=test rake db:migrate
I get the following error:
rake aborted! Don't know how to build task 'apartment:migrate'
Seems like it should be calling the app:apartment:migrate task, but I'm not sure how to do this so I can test this engine on its own with RSpec
You should have a Rakefile for the engine here: <app_root>/engines/engine_name/Rakefile
Try adding this line to the bottom of it to load the rake tasks from the apartment gem:
load 'tasks/apartment.rake'
Related
I'm using the book "Agile Web Development with Rails 4" and am going through Chapter 7 which has to do with testing. The tutorial includes a Product model and has the reader modify the test file product_test.rb which apparently should be in test/models. The Railes Guide for tests also has this structure. Yet for some reason I don't have a models subdirectory in my test directory. My test directory looks like this:
[~/code/depot]: ls test
fixtures functional integration performance test_helper.rb unit
My product_test.rb file is in the "unit" folder.
This is causing problems when I try to run tests as demonstrated in the book. rake test works fine as does ruby -Itest test/unit/product_test.rb but rake test:models produces the following error:
[~/code/depot]: rake test:models
rake aborted!
Don't know how to build task 'test:models'
(See full trace by running task with --trace)
I don't really understand the rake commands but I'm assuming that it's looking for test/models when I run rake test:models? At any rate, it doesn't work, and I have no idea why my test directory structure is lacking the models folder and why my product_test.rb is in test/unit instead. The Rails Guide on testing also indicates that scaffold-generated models should have tests in test/models. Can anyone explain what may have happened or how I should go about fixing this?
EDIT
Output of rake -T:
[~/code/depot]: rake -T
rake about # List versions of all Rails frameworks and the environment
rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:crea...
rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load # Load fixtures into the current environment's database
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false)
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop ...
rake db:structure:dump # Dump the database structure to db/structure.sql
rake db:version # Retrieves the current schema version number
rake doc:app # Generate docs for the app -- also available doc:rails, doc:guides, doc:plugins (options: TEMPLATE=/...
rake log:clear # Truncates all *.log files in log/ to zero bytes
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, update:scripts...
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 coo...
rake stats # Report code statistics (KLOCs, etc) from the application
rake test # Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:...
rake test:recent # Run tests for {:recent=>"test:prepare"} / Test recent changes
rake test:single # Run tests for {:single=>"test:prepare"}
rake test:uncommitted # Run tests for {:uncommitted=>"test:prepare"} / Test changes since last checkin (only Subversion and...
rake time:zones:all # Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET para...
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tm...
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids
Do you have something like the following in your application.rb?
config.generators do |g|
g.fixture_replacement :factory_girl # if your using factory_girl
g.test_framework :test_unit, :spec => true, :fixture => false
end
I receive the following error, even after doing a rake db:test:prepare. I am running rails 4.0.
1) Core::PostsController GET index assigns all posts as #posts
Failure/Error: post = Post.create! valid_attributes
ActiveRecord::StatementInvalid:
Could not find table 'core_posts'
# ./spec/controllers/core/posts_controller_spec.rb:36:in `block (3 levels) in <module:Core>'
I am running this test inside an engine, so could it be something related? My test looks like this:
module Core
describe PostsController do
# This should return the minimal set of attributes required to create a valid
# Post. As you add validations to Post, be sure to
# adjust the attributes here as well.
let(:valid_attributes) { { } }
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# PostsController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe "GET index" do
it "assigns all posts as #posts" do
post = Post.create! valid_attributes
get :index, {}, valid_session
assigns(:posts).should eq([post])
end
end
end
end
Any ideas? Thanks!
cd into the engine dir & generate a dummy app for testing for your engine:
rails plugin new . --full --mountable --dummy-path spec/dummy
the above command will generate a full mountable engine with isolated namespace, meaning that all the controllers and models from this engine will be isolated within the namespace of the engine. For instance, the Post model later will be called Core::Post, and not simply Post. Since you have already generated the app -- incase of conflicts, you can skip the change.
Further, engine comes with a dummy application, located at spec/dummy because we told it to do that with the --dummy_path option. This dummy application is just a bare-bones Rails application that can be used to test the engine as if it was mounted inside a real application.
Then, you need to change rspec to use this dummy app, by making following changes:
inside spec/spec_helper.rb change this line
require File.expand_path("../../config/environment", __FILE__)
to this
require File.expand_path("../dummy/config/environment",__FILE__)
As config/environment.rb file doesn’t live two directories up, but rather inside spec/dummy.
Now, you can run the migration by following command.
RAILS_ENV=test bin/rake db:migrate
Why not db:test:prepare?
We cannot run rake db:test:prepare because it is unavailable. This
db:migrate task is especially altered for engines, and will run the migrations for the engine PLUS migrations within the spec/dummy/db folder.
Try to recreate your test DB
without rbenv
RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:test:prepare
with rbenv
RAILS_ENV=test bundle exec rake db:drop
RAILS_ENV=test bundle exec rake db:create
RAILS_ENV=test bundle exec rake db:test:prepare
We are working on rails engine on 3.2.9. Engine is generated with :
rails plugin new rails_engine -mountable --skip-test-unit --dummy-path=spec/dummy
After initial setup, we can generate a model just like what we do in regular rails app:
rails g model post name:string
There is a migration file created under db/migrate/. Next we did db:migrate under the/ rails_engine :
rake db:migrate
It causes error:
Don't know how to build task 'db:migrate'
List available rake tasks:
bundle exec rake --tasks
rake build # Build test_itemx-0.0.1.gem into the pkg directory
rake clobber_rdoc # Remove RDoc HTML files
rake install # Build and install test_itemx-0.0.1.gem into system gems
rake rdoc # Build RDoc HTML files
rake release # Create tag v0.0.1 and build and push test_itemx-0.0.1.gem to Rubygems
rake rerdoc # Rebuild RDoc HTML files
There is no db:migrate among them. The question is how to load db tasks back to rake.
This is the expected behavior. Remember that it is a mountable engine. It does not have an environment of it's own. You need to mount it to a Rails application (Not an engine but an actual app). Engine does not have any database. It uses the database defined in its parent app in which it is mounted. So obviously your db:migrate would not work.
Do the following in your parent app for an engine:
Mount your engine in the Gemfile of a rails application as just another engine using :path or :git option
gem 'engine_name', :path=>'path/to/engine/directory'
bundle install
rake engine_name:install:migrations
rake db:migrate
I've entered spec/dummy in my engine folder and just runned rake db:migrate plus the same for test Env, and db/schema.rb was added.
But I had to add spec/dummy/db folder manually before.
I'm updating a rails 2.x app to rails 3.0.12.
This app used the bootstrapper gem to "seed" the database. The original fork of the gem seems to have stopped in the rails 2.x branch, but I found this other fork which is (in theory) compatible with rails 3.2 (notice that I'm updating to 3.0.12, not 3.2).
So this is what my Gemfile has now:
gem 'bootstrapper', :git => 'git://github.com/vivrass/bootstrapper.git'
After running bundle update, this seems to work ... except that the gem is supposed to add a new rake task called rake db:bootstrap, and it doesn't appear when I execute rake -T db (which I have aliased to bundle exec rake -T db via oh-my-zsh).
rake db:create # Create the database from config/database.yml for the current Rails.env (use db:crea...
rake db:drop # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load # Load fixtures into the current environment's database.
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false).
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n).
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:res...
rake db:structure:dump # Dump the database structure to an SQL file
rake db:version # Retrieves the current schema version number
I've checked the repo, and according to this post it seems that the gem is doing the right things:
- It has a railtie.rb file which loads the rake file
- The railtie.rb file is included in the lib/bootstraper.rb file
And yet, the rake task doesn't appear.
Has the process of adding rake tasks to a gem changed from rails 3.0.x to 3.2.x in any significant way?
I'm running ruby 1.9.3.
Thanks.
EDIT:
I created a new empty rails project in rails 3.0.12, added the line on the Gemfile, and after bundling, the rake task appears. This might be related with the fact that I'm updating from rails 2.x instead of creating a brand new rails 3.0.12 project.
Ok, found the problem.
The old (rails 2.x) bootstrapper gem needed a file called lib/tasks/bootstrapper.rake . I sill had that file on my rails project, and it was messing around with the rake tasks. Removing the file solved the issue.
Why are some rake tasks not listed by rake -T? Like db:migrate:reset? I can execute it without a problem, but why is it not listed there? Is there a way to get a real full list of rake tasks?
% rake -T
(in /home/zeus/projects/my_project)
rake about # List versions of all Rails frameworks and the environment
rake db:create # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load # Load fixtures into the current environment's database.
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false).
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n).
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
rake db:structure:dump # Dump the database structure to an SQL file
rake db:version # Retrieves the current schema version number
rake doc:app # Generate docs for the app -- also availble doc:rails, doc:guides, doc:plugins (options: TEMPLATE=/rdoc-template.rb, TITLE="Custom Title")
rake erd # Generate an Entity-Relationship Diagram based on your models
rake log:clear # Truncates all *.log files in log/ to zero bytes
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
rake rails:update # Update both configs and public/javascripts from Rails (or use just update:javascripts or update:configs)
rake routes # Print out all defined routes in match order, with names.
rake secret # Generate a crytographically secure secret key (this is typically used to generate a secret for cookie sessions).
rake spec # Run all specs in spec directory (excluding plugin specs)
rake spec:acceptance # Run the code examples in spec/acceptance
rake spec:controllers # Run the code examples in spec/controllers
rake spec:helpers # Run the code examples in spec/helpers
rake spec:lib # Run the code examples in spec/lib
rake spec:mailers # Run the code examples in spec/mailers
rake spec:models # Run the code examples in spec/models
rake spec:rcov # Run all specs with rcov
rake spec:requests # Run the code examples in spec/requests
rake spec:routing # Run the code examples in spec/routing
rake spec:views # Run the code examples in spec/views
rake stats # Report code statistics (KLOCs, etc) from the application
rake test # Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profile, test:plugins)
rake test:recent # Run tests for recenttest:prepare / Test recent changes
rake test:uncommitted # Run tests for uncommittedtest:prepare / Test changes since last checkin (only Subversion and Git)
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
Tasks that don't have a description will not show up.
EDIT: Looks like DHH removed the description from Rails 3 for a few tasks to 'cut down on noise'. rake db:setup has a note about db:reset though.
http://github.com/rails/rails/commit/983815632cc1d316c7c803a47be28f1abe6698fb
You can also use rake db -T -A to show all tasks, even uncommented ones. For reference these switches are doing the following:
-T, --tasks [PATTERN] Display the tasks (matching optional PATTERN) with
descriptions, then exit.
-A, --all Show all tasks, even uncommented ones (in combination
with -T or -D)
You can use rake -P | grep rake to show the names of all Rake tasks.
As a follow up to Thomas Obermüller's answer, since the -T flag takes a positional argument, to get just the 'db' namespace tasks (at least with rake 10.0.3) you need to do: rake -T db -A
On this WIKI - Ruby on Rails/ActiveRecord/Migrations you can find some more information.
For windows user, you do this rake -P | find "rake"