RSpec testing database index

后端 未结 1 1850
醉梦人生
醉梦人生 2021-01-21 01:45

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

相关标签:
1条回答
  • 2021-01-21 02:14

    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"]}]
    
    0 讨论(0)
提交回复
热议问题