1
votes

I got a Ruby on Rails v5 application and I'm trying to run the following Rake task in development environment:

rake db:reset

Sadly, Rake runs the task in both development and test environment. Since I don't use the test environment, I don't wanna double my database. I have read somewhere that Rake run task in both environment when there is no RAILS_ENV defined. So I try to add the following line to my .bash_profile without any success:

export RAILS_ENV="development"

I also tried to add RAILS_ENV=development at the end of my task but it also did not worked.

Is there a way to use Rake in development only ?

Update #1

Thanks to Taryn East for the quick comment. I'm trying to update my post as quick as possible to make it easy for you to answer efficiently.

What do you actually observe when you run that command?

The command are simply executed twice. Since I have different database for my development and test environment, it does not show me any error at the moment. On the other hand, it does force me to use two database for no reason at all. I also tried to set the same database for the two environments, but then I was getting error since Rake tried to run the task twice on the database.

eg of output for rake db:drop

Dropped database 'db'
Database 'db' does not exist

It did drop my database, but as you can see Rake tried to run the command another time for the other environment.

Why aren't you using the test environment?

Because I don't see any advantage of it for my current project. I don't have time to create and update tests. I'm also not used to use both development and test environment at the same time.

If you want to run a rake task in just a single environment, you can also add RAILS_ENV to the command line.

As I said in my post, it does not work. Here's what I did:

RAILS_ENV=development rake db:drop

And here's the output:

Dropped database 'db'
Database 'db' does not exist

Update #2

Here's my database.yml configuration:

default: &default
  adapter:mysql2
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db
  username: something
  password: something
  host: localhost
  port: 3306

test:
  <<: *default
  database: db_test
  username: something
  password: something
  host: localhost
  port: 3306

I don't know if it can help, but my environment is development according to the rake about task.

1
How do you know that it's running in multiple environments? what do you actually observe when you run that command? what error message(s) are you getting? Secondly: why aren't you using the test environment? you definitely should have one of those set up! it won't double your database... the test database is structure-only without any data except for when your tests (which you should have!) are actually running. Thirdly: if you want to run a rake task in just a single environment, you can also add RAILS_ENV to the command line. eg you could run: RAILS_ENV=test rake db:reset - Taryn East
Thank you for you're answer. I answered you in the Update #1 section. - François Noël
" I don't have time to create and update tests." you don't have time to fix bugs that could have been caught early by those tests either... ;) - Taryn East
The problem does sound odd though - it sounds like rake is running simultaneously on two environments, which it shouldn't be. Can you show us your config/database.yml? (just delete out the username/password) ? - Taryn East
You're definitely right about the time lost. I just can't understand that I can't run my task in a single environment. It makes no sense. Anyway, I did another update in the post. - François Noël

1 Answers

0
votes

I have read somewhere that Rake run task in both environment when there is no RAILS_ENV defined.

This is not correct. If the environment is development, test is also added to current environments. See: https://github.com/rails/rails/blob/f507085fa2a7625324e6f4e3ecef9b27dabefb4f/activerecord/lib/active_record/tasks/database_tasks.rb#L339

So this is expected behaviour.

On the other hand what you're saying will be true for any other env. E.g. RAILS_ENV=test rails db:drop will only execute for test env.