RSpec 测试数据库索引 [英] RSpec testing database index

查看:49
本文介绍了RSpec 测试数据库索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用 RSpec 测试数据库索引是否存在?

Is there a way to test if a database index exists with RSpec?

我正在使用 friendly_id gem 并且经常忘记创建slug 字段的数据库索引.因此,我想将其包含在我的 RSpec 测试中.

I'm using friendly_id gem and often I forget to create the database index for slug field. So, I would like to include this in my RSpec tests.

推荐答案

您可以使用 index_exists? 通过选项检查 unique unique: true:

=> ActiveRecord::Migration.index_exists?(:users, :user_id, unique: true)
=> -- index_exists?(:users, :user_id)
=>   -> 0.0559s
=> false

或:

=> ActiveRecord::Base.connection.index_exists?(:users, :user_id)
=> false

基于应该匹配器:

=> ActiveRecord::Base.connection.indexes(:users).map(&:columns)
=> [["birthday"],
=> ["confirmation_token"],
=> ["email"],
=> ["facebook_id"],
=> ["reset_password_token"]]

具有唯一:

=>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"]}]

这篇关于RSpec 测试数据库索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆