My Photo

Google Ads

  • Google Ads

« XUL Description Tag: The bain of my existance | Main | Date, Time, DateTime formatting in ruby. »

October 10, 2007

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e54ede0214883300e54f0907c98834

Listed below are links to weblogs that reference Rails Migration Table Check:

Comments

cory

You can also do it like this, which is a little cleaner

def self.table_exists?(name)
ActiveRecord::Base.connection.tables.include?(name)
end

Eliot Sykes

To make it reusable you can create a module or class in your lib directory, e.g. database_util.rb:

class DatabaseUtil
def self.table_exists?(table_name)
ActiveRecord::Base.connection.tables.include? table_name.to_s
end
end

Daniel Rosenstark

Don't bother.

drop_table :events rescue puts("events doesn't exist, no worries")

neil

For me the rescue approach leaves my transaction trashed, and nothing works afterwards. So I have to do a check first, rather than letting it fail.

But I we have some custom transaction stuff to make sure our migrations roll back and don't leave everything trashed if they fail part way.

Raf

Here is my approach to solve the problem:

http://github.com/rafmagana/database_utils

after using that code you can do this in a migration:

:users.table_exists?
"users".table_exists?
:users.column_exists? :name

see you

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment