I have a rake task in my rails app that has a different absolute filepath compared between prod and dev. I have a different database username/pass that differs between prod and dev. I can't seem to get the RAILS_ENV variable to be set within RVM in order to tell each server what their role is. Is there a way to do this?
My workaround has been to do the following for all rake tasks:
RAILS_ENV=development rake routes
RAILS_ENV=production rake db:migrate
Been having similar problems (not with RVM though). One way we did it was to assign ENV["RAILS_ENV"] through envrionment.rb
It's not the actual answer, but it will give you some ideas:
#config/environment.rb
ENV["RAILS_ENV"] = "staging"
You may wish to look at assigning environment vars in RVM here: http://matthew.mceachen.us/blog/howto-make-system-wide-rvm-installations-work-with-cron-monit-delayed_job-and-passenger-1021.html
I am trying to setup a unique environment (but effectively a new development environment so that various global parameters can be different). I've followed lots of examples to create a new environment (I used my development config as the starting point).
My new environment is singleserverintegration.
a new environment.rb
added new logic to initializers/additional.rb (elsif Rails.env.eql?("singleserverintegration"))
added entries to database.yml
BUT when-ever i attempt to setup the environment
RAILS_ENV="singleserverintegration" && rake db:drop && rake db:create && rake db:migrate
I get a
rake aborted!
uninitialized constant Capybara
why is it pulling out test configuration (which is where capybara is used as part of rspec) [i've noticed that additional.rb has capybara config reguardless of the environment, but it never complains when i run rake / db commands for my dev environment. why would it complain now?]
what am i missing - guidance appreciated for a relative newbie...
thanks
Ben
bottom line of additional.rb is
Capybara.server_port = 8066
but, this does not cause an issue when i setup development environments!?
try adding your new env to the capybara gem as well
Ex:
group :test, :development, :singleserverintegration do
gem 'capybara'
end
I am creating a brand new full gem plugin using Rails 3.1.0 on ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]. I am having trouble with the schema and fixtures not being loaded for running rake test. Following are the steps I am taking:
Create the plugin:
rails plugin new core --full
From the plugin, generate a new scaffold:
rails g scaffold user
Run the db create and migrate:
rake db:create
rake db:migrate
Run the tests:
rake test
When running the functional tests, I am receiving a set of error like the following from the controller tests:
1) Error:
test_should_create_user(UsersControllerTest):
NoMethodError: undefined method `users' for #<UsersControllerTest:0x00000003babca8>
This error seems to stem from the fact that the test doesn't seem to understand the fixture call. It doesn't look like the test db is getting the schema loaded or having the fixtures loaded. Is this expected behavior? Is there something I'm missing in this scenario? All the fixtures are there. Is there some process I need to follow to get these tests to run correctly?
OK, I think I've made some progress. It looks like I can resolve it with the following changes.
I've added the following to test_helper:
#Run any available migration
ActiveRecord::Migrator.migrate 'up'
# Set fixtures root
ActiveSupport::TestCase.fixture_path=(File.expand_path("../fixtures", __FILE__))
ActiveSupport::TestCase.fixtures :all
This allows the test environment to find my fixtures outside of the dummy application.
After this change, I was still having an issue where the test database was not being prepared for the test. I resolved this by dropping into the test/dummy directory and running:
rake db:test:prepare
After this, I am able to run "rake test" successfully. It's somewhat a pain to have to drop in to the dummy app to prepare the db. There has to be a way around this. Any suggestions?
I have a Rails 2.2 app that I'm supporting and one thing that needs to be done before the app start is a small rake task to initialize and make sure the database is properly set. How can I get this task run right after rspec initializes the database. I run rspec using the rails spec command.
You could put a simple system call to your spec_helper.rb file.
An Example could look like this
# run rake task
`rake your_task RAILS_ENV=test`
RSpec.configure do |config|
...
end
I'm developing a Rails application with Rspec for unit testing.
Weeks ago, Rspec used to migrate the database to the last version automatically when executing 'rake spec', but now it doesn't do it automatically, I have to implement everything for myself.
This happens in test environment, because my development data doesn't desappear.
Is my fault? I didn't change anything, I think :)
Typically what I do is use an alias command that runs both migrate and prepares the test database.
rake db:migrate && rake db:test:prepare
In your .bashrc just create an alias command like so and then run migrate_databases whenever you need to.
alias migrate_databases='rake db:migrate && rake db:test:prepare'
My solution for Rails 4:
in spec/spec_helper.rb or anywhere in testing environment initialization code:
# Automigrate if needs migration
if ActiveRecord::Migrator.needs_migration?
ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db/migrate'))
end
UPD: As Dorian kindly pointed out in comments, you don't need to check separately if there are any pending migrations, because ActiveRecord::Migrator.migrate already does this behind the scenes. So you can effectively use just this one line:
ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db/migrate'))
Rails 4.1 forward you can use:
ActiveRecord::Migration.maintain_test_schema!
Add at the top of your spec_helper.rb or rails_helper.rb and you're good to go. More info here.
Here's my workaround:
Rakefile:
require File.expand_path('../config/application', __FILE__)
require 'rake'
require "rspec/core/rake_task"
MyApp::Application.load_tasks
desc "Run specs"
RSpec::Core::RakeTask.new(:spec)
task :run_specs => ['db:test:clone', :spec] do
end
task :default => :run_specs
Then I run $ rake run_specs
for some reason default task doesn't default to run_specs
See if you have the following in your spec_helper.rb? Everytime you run specs, RSpec checks if there are pending migrations.
#Checks for pending migrations before tests are run.
#If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
This works even when Rails is not loaded and only does one SQL query most of the time.
if defined?(ActiveRecord::Migrator)
ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db', 'migrate'))
end