# Running 'rake' empties production database if RAILS\_ENV = 'p

**URL:** <https://www.ruby-forum.com/t/running-rake-empties-production-database-if-rails-env-p/47502>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/running-rake-empties-production-database-if-rails-env-p/47502 "2005-11-12T12:49:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/running-rake-empties-production-database-if-rails-env-p/47502/1 "2005-11-12T12:49:13Z")

</div>

I just ran rake on a development site on a server, which I typically  
don’t do, while trying to test what I thought to be a server-related  
issue. When rake finished (all tests passed) the _production_ database  
was wiped, leaving only fixture data. I subsequently noticed that in  
the dev site’s ‘environment.rb’ I had uncommented the line…

ENV[‘RAILS\_ENV’] = ‘production’

The site\_test database was unchanged. I believe the database.yml file  
is configured correctly (test environment points to my \_test  
database)…

test:  
adapter: mysql  
database: site\_test  
host: localhost  
username: jbaty  
password: mypassword

production:  
adapter: mysql  
database: site\_production  
host: localhost  
username: jbaty  
password: mypassword

It’s a tad disconcerting that typing “rake” in the wrong directory  
could have that sort of effect. Shouldn’t rake _always_ target the  
test environment database, regardless of environment rails is running?  
Or perhaps I just don’t understand something.

–  
Jack Baty  
Fusionary Media - [http://www.fusionary.com](http://www.fusionary.com)

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/running-rake-empties-production-database-if-rails-env-p/47502/2 "2005-11-12T12:49:13Z")

</div>

On Nov 7, 2005, at 12:59 PM, Jack Baty wrote:

> database)…  
> Argh! Sorry to hear that.

> It’s a tad disconcerting that typing “rake” in the wrong directory  
> could have that sort of effect. Shouldn’t rake _always_ target the  
> test environment database, regardless of environment rails is running?  
> Or perhaps I just don’t understand something.

Indeed. It does in the 1.0 release candidates to prevent this scenario.

RAILS\_ENV is specifiable as an environment variable so you don’t have to  
hardcode it in environment.rb – you may set it from outside your  
Rails app,  
such as in your FastCGI configuration.

See the -init-env option for Apache mod\_fastcgi and the bin-environment  
option for lighttpd. For example (lighttpd):

fastcgi.server = (  
“.fcgi” =\> (  
“myapp” =\> (  
“min-procs” =\> 2,  
“max-procs” =\> 2,  
“socket” =\> “/tmp/myapp\_fcgi\_1.sock”,  
“bin-path” =\> “public/dispatch.fcgi”,  
“bin-environment” =\> ( “RAILS\_ENV” =\> “production” )  
)  
)  
)

jeremy

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/running-rake-empties-production-database-if-rails-env-p/47502/3 "2005-11-12T12:49:14Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Nov 7, 2005, at 1:48 PM, Jack Baty wrote:

> > such as in your FastCGI configuration.
> > 
> > See the -init-env option for Apache mod\_fastcgi and the bin-  
> > environment  
> > option for lighttpd. For example (lighttpd): [snip]
> 
> That’s how I do it with lighttpd, but mod\_fastcgi has always been  
> difficult for me to get right, so I’ve been known to cheat and simply  
> change it in environment.rb. I’m trying to wean myself of that though.

Apache FastCGI configuration is notoriously aggravating.

We’re adding some further safeguards right now to keep this from ever  
happening.

jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDb83NAQHALep9HFYRAgOKAJ4oGbfKqUzvAbbCxIsdxY5M1w1GcgCdGRmN  
KntzLBRafVld6R/JsNcUjOA=  
=KkmS  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/running-rake-empties-production-database-if-rails-env-p/47502/4 "2005-11-12T12:49:13Z")

</div>

On 11/7/05, Jeremy K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Argh! Sorry to hear that.

Thanks. Fortunately, frequent backups have made this into only a minor  
inconvenience.

> RAILS\_ENV is specifiable as an environment variable so you don’t have to  
> hardcode it in environment.rb – you may set it from outside your  
> Rails app,  
> such as in your FastCGI configuration.
> 
> See the -init-env option for Apache mod\_fastcgi and the bin-environment  
> option for lighttpd. For example (lighttpd): [snip]

That’s how I do it with lighttpd, but mod\_fastcgi has always been  
difficult for me to get right, so I’ve been known to cheat and simply  
change it in environment.rb. I’m trying to wean myself of that though.

–  
Jack Baty  
Fusionary Media - [http://www.fusionary.com](http://www.fusionary.com)

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [December 27, 2005, 6:58pm UTC](https://www.ruby-forum.com/t/running-rake-empties-production-database-if-rails-env-p/47502/5 "2005-12-27T18:58:29Z")

</div>

Any suggestions if you’re running in a shared environment and don’t have  
access to the apache/lighttpd config? This problem is killing me.

Ha this been submitted as a bug?

Thanks.

–Ryan

jeremy wrote:

> -----BEGIN PGP SIGNED MESSAGE-----  
> Hash: SHA1
> 
> On Nov 7, 2005, at 1:48 PM, Jack Baty wrote:
> 
> > > such as in your FastCGI configuration.
> > > 
> > > See the -init-env option for Apache mod\_fastcgi and the bin-  
> > > environment  
> > > option for lighttpd. For example (lighttpd): [snip]
