Detect environment in migration

We’re using migrations for generating our test schemas (for a whole load
of reasons) and need to specifically run code when we are running our
rspec tests as opposed to running the actual migrations on the
production or development boxes.

Is there a way to detect the current environment that rspec is
connecting too?

I’ve tried looking in ENV[‘RAILS_ENV’] but it doesn’t appear to be set.
I’ve also tried looking in ActiveRecord::Base.connection to see if the
connection would give me a clue.

I want to do something like:

if RAILS_ENVIRONMENT == ‘TEST’

RUN THIS SQL

end

Alex M. wrote:

Is there a way to detect the current environment that rspec is
connecting too?

I’m not sure if this is helpful, but I use the following code to
generate development and test databases for a system that I report on
only in Rails (the production database is part of another application).
This works for me:

def self.up
if ENV[“RAILS_ENV”] and ENV[“RAILS_ENV”] == “production”
puts “Production db not altered by migration”
else
sql = “CREATE TABLE …” #creation SQL code.
execute sql
end
end