Is there a way to test if a database index exists with RSpec?
I\'m using friendly_id
gem and often I forget to create the database index for slug field.
S
You can use index_exists? check unique
by options unique: true
:
=> ActiveRecord::Migration.index_exists?(:users, :user_id, unique: true)
=> -- index_exists?(:users, :user_id)
=> -> 0.0559s
=> false
or:
=> ActiveRecord::Base.connection.index_exists?(:users, :user_id)
=> false
or based on should-matchers:
=> ActiveRecord::Base.connection.indexes(:users).map(&:columns)
=> [["birthday"],
=> ["confirmation_token"],
=> ["email"],
=> ["facebook_id"],
=> ["reset_password_token"]]
with unique
:
=>ActiveRecord::Base.connection.indexes(:users).map { |x| Hash[:unique, x.unique, :column, x.columns ] }
=> [{:unique=>false, :column=>["birthday"]},
=> {:unique=>true, :column=>["confirmation_token"]},
=> {:unique=>true, :column=>["email"]},
=> {:unique=>true, :column=>["facebook_id"]},
=> {:unique=>true, :column=>["reset_password_token"]}]