Change Ruby Environment in Hudson - ruby-on-rails

I'm trying to point the Hudson app at the test environment in my MySQL database with Rails. Do I need to change the environment variable RUBY_ENV? If so, where do I implement this?

I believe you're referring to RAILS_ENV variable? If you're writing the commands to hudson then something like this might work:
$ rake test RAILS_ENV=test
Although rake test implicitly uses the test environment. If it's needed for other commands just pass RAILS_ENV=test to them.

Related

Build all tables within testing database from schema.rb

I am trying to implement testing and realized I need to create a testing database.
I successfully created the schema.rb file from the database by running rake db:schema:dump.
I also successfully created the new testing database.
Now what I need to do (I think) is do something like a rake db:schema:load, but I want to specify that I am doing this for the test database (not the development database or the production database. I don't want to delete any of the data there!)
I attempted rake db:test:schema:load but that was not working.
Searches online advise using commands that appear to be deprecated for rails 4.1 or later.
Answer based on feedback:
RAILS_ENV=test rake db:schema:load
If you want to specify that you are creating the "test" database:
RAILS_ENV=test rake db:create
The RAILS_ENV environmental variable will allow you to specify which environment you wish to use. These environments will be defined in your "config/database.yml". Ensure that you have a test environment setup and a database specified under it. This can be done to any task that you would like to affect a particular environment.
RAILS_ENV=test rake any:task:here

How do I create a ruby script that runs script/console and then runs commands using that environment?

I've noticed I've been having to do:
bundle exec script/console
<wait for console to load>
require migration
generate some data
a lot... and I was wondering if there is a way to have this all in a bash script or something. so i could just do ./generatedata and have it run the above commands.
I've found that custom rake tasks are an awesome tool for when you have work which requires running code in the rails environment. Check out this railscast http://railscasts.com/episodes/66-custom-rake-tasks
If you want to run a one-off command in the console, you can use the rails runner command. So if you had a ./generatedata.rb script which performs the ruby commands you want to execute in the console, you can just call rails runner ./generatedata.rb and it will run your ruby script in the rails environment against the database. Alternatively, you could add the shebang line to the ./generatedata.rb script: #!/usr/bin/env rails runner. Then you only need to execute the ./generatedata.rb script and it will use rails runner automatically.

How to write ruby script that uses the production environment instead of development?

I have writen a ruby script that requires config/environment.rb so I can use all the models of my rails application in the script but the problem is that I want to use the production environment instead of the develoment environment which seems to be the default behavior.
Im using Rails 3.1.1 and Ruby 1.9.2
How can I run the script with the production environment?
Your script will take the environment variable RAILS_ENV as the environment to use.
I'd be very wary of overriding that in the script as it may cause much confusion if you try and run your script in another environment - e.g. staging - and it starts trying to access production databases etc.
So do either:
RAILS_ENV=production ./script/my-awesome-script
or
export RAILS_ENV=production
./script/my-awesome-script
Generally speaking; when I log into a production Rails environment I'd be changing the environment straight away if I haven't configured it to be "production" by default.
I think you want either Rails.env = 'production' or ENV['RAILS_ENV'] = 'production' in your script.
#davidb, I am not sure what u want also not as good on rails yet but may be you can use the seed.rb (i.e seed functionality) for achieving what you want if this script is run only once or sometimes as we can specify the environment while running seed
rake db:seed RAILS_ENV=production

Running custom SQL to prepare Rails integration test

I am trying to run some custom SQL during the setup of my Rails integration tests to prepare a legacy database (e.g., to create its tables, create any required views, etc.), which is not part of my schema.rb (nor do any migrations for it exist).
Are there any best practices for doing so? Googling has not been very enlightening so far ;-)
The reason why there are not any migrations is that in the development and production RAILS_ENV the database already exists for legacy reasons. If there is a way to run these migrations only for RAILS_ENV=='test', that would maybe also help.
You can pass the Rails environment to Rake on the command line. For example, to only run migrations for the test environment, do:
rake RAILS_ENV=test db:migrate

How to run Rails console in the test environment and load test_helper.rb?

The background: I'm having some problems with Thoughtbot's "Factory Girl" gem, with is used to create objects to use in unit and other tests. I'd like to go to the console and run different Factory Girl calls to check out what's happening. For example, I'd like to go in there are do...
>> Factory(:user).inspect
I know that you can run the console in different environments...
$ script/console RAILS_ENV=test
But when I do that, Factory class is not available. It looks as though test_helper.rb is not getting loaded.
I tried various require calls including one with the absolute path to test_helper.rb but they fail similarly to this:
$ script/console RAILS_ENV=test
>> require '/Users/ethan/project/contactdb/test/test_helper.rb'
Errno::ENOENT: No such file or directory -
/Users/ethan/project/contactdb/config/environments/RAILS_ENV=test.rb
Grr. Argh.
For Rails < 3.0
Run script/console --help. You'll notice that the syntax is script/console [environment], which in your case is script/console test.
I'm not sure if you have to require the test helper or if the test environment does that for you, but with that command you should at least be able to boot successfully into the test env.
As a sidenote: It is indeed kind of odd that the various binaries in script/ has different ways of setting the rails environment.
For Rails 3 and 4
Run rails c test. Prepend bundle exec if you need this for the current app environment.
For Rails 5 and 6
Run rails console -e test.
In Rails 3, just do rails console test or rails console production or rails console development (which is the default).
For Rails 5.2.0: "Passing the environment's name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -e option instead."
rails c -e test
script/console test
Should be all you need.
You can specify the environment in which the console command should operate.
rails c [environment]
Examples
1) For Staging
rails c staging
2) For Production
rails c production
For source & detailed description: The Rails Command Line
David Smith is correct, just do
script/console test
The help command will show why this works:
$ script/console -h
Usage: console [environment] [options]
-s, --sandbox Rollback database modifications on exit.
--irb=[irb] Invoke a different irb.
--debugger Enable ruby-debugging for the console.
It's the [environment] bit.
I share the asker's pain. There are really three separate questions here, some of which are addressed, some not:
How do you start the console in the test environment?
For recent Rails versions, bundle exec rails c test, or alternative syntaxes for that.
How do you ensure that test/test_helper.rb is loaded into that console session?
Something like require './test/test_helper' ought to do it.
For me, this returns true, indicating that it was not already loaded when I started the console. If that statement returns false, then you just wasted a few keystrokes, but you're still good to go.
Once test_helper is loaded, how do you call the methods defined in it?
In a typical test_helper, the custom methods are typically defined as instance methods of ActiveSupport::TestCase. So if you want to call one of them, you need an instance of that class. By trial and error, ActiveSupport::TestCase.new has one required parameter, so...pass it something.
If your test_helper has a method called create_user, you could invoke it this way:
ActiveSupport::TestCase.new("no idea what this is for").create_user
Make sure you installed the GEM and you added the following line either in your environment.rb or test.rb file.
config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com"
Test Env
rails console test # or just rails c test
Developement Env
rails console # or just rails c
Command to run rails console test environment is
rails c -e test
or
RAILS_ENV=test rails c
if you are facing problem something like
ActiveRecord::StatementInvalid:
Mysql2::Error: Table 'DB_test.users' doesn't exist: SHOW FULL FIELDS FROM `users`
then you should first prepare your test DB by running
bundle exec rake db:test:prepare

Resources