# Detecting rake db:migrate?

**URL:** <https://www.ruby-forum.com/t/detecting-rake-db-migrate/159566>\
**Category:** Rails\
**Created:** [February 5, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/detecting-rake-db-migrate/159566 "2009-02-05T21:33:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [February 5, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/detecting-rake-db-migrate/159566/1 "2009-02-05T21:33:54Z")

</div>

Hi group,

I have a line of code in environment.rb that cannot be run during a  
rake db:migrate, as it depends on some tables being present.

I need to find a way to enclose this line in a check for db:migrate. I  
know how to check if we’re in a rake task with:

unless defined?(Rake)

# do stuff, but skip when run from rake tasks

end

How can I take this further to make check if I’m running the rake  
db:migrate task?

Thanks,

-H

---

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [February 5, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/detecting-rake-db-migrate/159566/2 "2009-02-05T21:55:04Z")

</div>

Excuse me, the code I need to wrap around the check is on  
ApplicationController. Still, any ideas?

---

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [February 5, 2009, 10:23pm UTC](https://www.ruby-forum.com/t/detecting-rake-db-migrate/159566/3 "2009-02-05T22:23:27Z")

</div>

There is a model class method like table\_exists? That you can use to  
check ifthe table exists and then only run the code if it does

---

<div class="post-metadata">

**Author:** ![Harold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8491ac/32.png) [@Harold](https://www.ruby-forum.com/u/Harold)\
**Post date:** [February 5, 2009, 10:26pm UTC](https://www.ruby-forum.com/t/detecting-rake-db-migrate/159566/4 "2009-02-05T22:26:24Z")

</div>

Thanks! That would work, for now.

I still have the itch though. If anyone knows, do tell.

Thanks again,  
-H
