90

I was doing this kind of thing in my migrations:

add_column :statuses, :hold_reason, :string rescue puts "column already added"

but it turns out that, while this works for SQLite, it does not work for PostgreSQL. It seems like if the add_column blows up, even if the Exception is caught, the transaction is dead and so the Migration can't do any additional work.

Is there any non-DB sepecific ways to check if a column or table already exist? Failing that, is there any way to get my rescue block to really work?

2
  • It need to be mentioned, that conditional migration leads to problems with rollback due to the fact that at the rollback stage it is not known what conditions were during the forward migration
    – oklas
    Feb 13, 2020 at 8:48
  • Only do the non optional part in rollback Feb 13, 2020 at 23:36

5 Answers 5

193

As of Rails 3.0 and later, you can use column_exists? to check for the existance of a column.

unless column_exists? :statuses, :hold_reason
  add_column :statuses, :hold_reason, :string
end

There's also a table_exists? function, which goes as far back as Rails 2.1.

3
  • Is it considered best practice to check if a column/table exists before add/create it? (I know of course it depends on the problem in hands) Apr 25, 2014 at 11:11
  • 4
    Does this work with rollbacks if I define it in change method?
    – dardub
    Sep 18, 2015 at 17:57
  • 2
    Yeah rollback would be a issue... we're not sure whether we should remove the column or not.. since we're not recording the previous state.
    – songyy
    Jul 12, 2016 at 9:26
13

Rails 6.1+:

add_column :statuses, :hold_reason, :string, if_not_exists: true

https://github.com/rails/rails/pull/38352/files

Rails < 6.1:

add_column :statuses, :hold_reason, :string unless column_exists?(:statuses, :hold_reason)
10

Or even shorter

add_column :statuses, :hold_reason, :string unless column_exists? :statuses, :hold_reason
1
  • this would be a comment on the other answer, not an answer. Thanks. Apr 20, 2013 at 15:20
4

For Rails 2.X, you can check the existence of columns with the following:

columns("[table-name]").index {|col| col.name == "[column-name]"}

If it returns nil, no such column exists. If it returns a Fixnum, then the column does exist. Naturally, you can put more selective parameters between the {...} if you want to identify a column by more than just its name, for example:

{ |col| col.name == "foo" and col.sql_type == "tinyint(1)" and col.primary == nil }

(this answer first posted on How to write conditional migrations in rails?)

0

add_column :statuses, :hold_reason, :string unless Status.column_names.include?("hold_reason")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.