模块:Mongoid::Tasks::Database
Overview
用于管理数据库集合、索引、分片等的实用程序模块。从 Rake 任务中调用。
实例方法摘要折叠
-
# create_collections (models =::Mongoid.models, force: false) ⇒ 对象
在给定提供的 glob 且未嵌入类的情况下,为每个模型创建集合。
-
# create_indexes (models =::Mongoid.models) ⇒ Array<Class>
在给定提供的 glob 且未嵌入类的情况下,为每个模型创建索引。
-
# create_search_indexes (models =::Mongoid.models, wait: true) ⇒ 对象
提交创建搜索索引的请求。
-
# remove_indexes (models =::Mongoid.models) ⇒ Array<Class>
在给定提供的 glob 且未嵌入类的情况下,删除每个模型的索引。
-
# remove_search_indexes (models =::Mongoid.models) ⇒ 对象
从给定模型中删除所有搜索索引。
-
# remove_undefined_indexes (models =::Mongoid.models) ⇒ Hash{Class => Array(Hash)}
删除数据库中存在但未在模型上指定的索引。
-
# shard_collections (models =::Mongoid.models) ⇒ Array<Class>
声明分片键的模型的分片集合。
-
# undefined_indexes (models =::Mongoid.models) ⇒ Array<Hash>
按模型返回数据库中存在但未在模型上指定的索引列表。
实例方法详细信息
# create_collections (models =::Mongoid.models, force: false) ⇒对象
在给定提供的 glob 且未嵌入类的情况下,为每个模型创建集合。
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mongoid/tasks/ 数据库.rb', line 21 def create_collections(模型 = ::Mongoid.模型, 强制: false) 模型.每 do |模型| if !模型. || 模型.循环? 模型.create_collection(强制: force) 记录器.信息(" MONGOID: Created 集合 for #{ model } : ") else 记录器.信息(“ MONGOID:在#{ model }上忽略了集合选项,请在根模型中定义。 ”) end 救援 例外 记录器.错误 “ 为 #{ model} 创建集合时出错” 提高 end end |
# create_indexes (models =::Mongoid.models) ⇒ Array<Class>
在给定提供的 glob 且未嵌入类的情况下,为每个模型创建索引。
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mongoid/tasks/ 数据库.rb', line 42 def create_indexes(模型 = ::Mongoid.模型) 模型.每 do |模型| 来年 if 模型.index_specifications.空? if !模型. || 模型.循环? 模型.create_indexes 记录器.信息(" MONGOID: Created indexes on #{ model } : ") 模型.index_specifications.每 do |spec| 记录器.信息(" MONGOID: Index: #{ spec . key } , Options: #{ spec . } ") end 模型 else 记录器.信息(“ MONGOID:索引已忽略: #{ model } ,请在根模型中定义。 ”) nil end end.compact end |
# create_search_indexes (models =::Mongoid.models, wait: true) ⇒对象
提交创建搜索索引的请求。 这将异步发生。 如果“wait”为 true,则该方法将在等待创建索引时区块。
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mongoid/tasks/ 数据库.rb', line 67 def create_search_indexes(模型 = ::Mongoid.模型, 等待: true) 可搜索 = 模型.SELECT { |m| m.search_index_specs.任何? } # 对搜索索引创建请求进行排队 index_names_by_model = 可搜索.each_with_object({}) do |模型, obj| 记录器.信息(" MONGOID:在#{ model }上创建搜索索引... ") obj[模型] = 模型.create_search_indexes end wait_for_search_indexes(index_names_by_model) if wait end |
# remove_indexes (models =::Mongoid.models) ⇒ Array<Class>
在给定提供的 glob 且未嵌入类的情况下,删除每个模型的索引。
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/mongoid/tasks/ 数据库.rb', line 139 def remove_indexes(模型 = ::Mongoid.模型) 模型.每 do |模型| 来年 if 模型. 开始 模型.remove_indexes 救援 mongo::错误::OperationFailure 来年 end 模型 end.compact end |
# remove_search_indexes (models =::Mongoid.models) ⇒对象
从给定模型中删除所有搜索索引。
155 156 157 158 159 160 |
# File 'lib/mongoid/tasks/ 数据库.rb', line 155 def remove_search_indexes(模型 = ::Mongoid.模型) 模型.每 do |模型| 来年 if 模型. 模型.remove_search_indexes end end |
# remove_undefined_indexes (models =::Mongoid.models) ⇒ Hash{Class => Array(Hash)}
删除数据库中存在但未在模型上指定的索引。
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/mongoid/tasks/ 数据库.rb', line 118 def remove_undefined_indexes(模型 = ::Mongoid.模型) undefined_indexes(模型).每 do |模型, 索引| 索引.每 do |索引(index)| key = 索引(index)['key'].symbolize_keys 集合 = 模型.集合 集合.索引(会话: 模型.发送(:_session)).drop_one(key) 记录器.信息( " MONGOID: Removed index ' #{ index [ ' name ' ] } ' on collection " + " ' #{ 集合 . name } ' in 数据库 ' #{ 集合 . 数据库 . name } '. " ) end end end |
# shard_collections (models =::Mongoid.models) ⇒ Array<Class>
声明分片键的模型的分片集合。
返回已对其集合进行分片的模型分片的,包括在调用此方法之前其集合已进行分片的的模型类。
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/mongoid/tasks/ 数据库.rb', line 172 def shard_collections(模型 = ::Mongoid.模型) 模型.map do |模型| 来年 if 模型.shard_config.nil? if 模型. && !模型.循环? 记录器.WARN(" MONGOID: #{ model } has 分片 config 但 is embedded ") 来年 end 除非 模型.集合.集群.分片的? 记录器.WARN(” ) 来年 end # 集合的数据库必须存在才能运行 collStats。 # 根据服务器版本,集合本身还必须 # exists. # MongoDB 没有用于创建数据库的命令;最好的 # 它的近似值是创建我们想要的集合。 # 在较旧的服务器上,创建已存在的集合是 # 一个错误。 # 此外, 3.6和可能更旧的服务器不提供 # 当要求它们 collStats 不存在时的错误代码 # 集合 (https://jira.mongodb.org/browse/SERVER-{0 50070 ). 开始 stats = 模型.集合.database.命令(collStats: 模型.集合.名称).first 救援 mongo::错误::OperationFailure => 排除 # 代码26表示数据库不存在。 # 截至4.0 ,代码8是集合不存在。 # 在3.6及更早版本中,匹配异常消息的文本。 if 排除.代码 == 26 || 排除.代码 == 8 || 排除.代码.nil? && 排除. =~ / not find / then 模型.集合.创建 stats = 模型.集合.database.命令(collStats: 模型.集合.名称).first else 提高 end end stats = 模型.集合.database.命令(collStats: 模型.集合.名称).first if stats[:sharded] 记录器.信息(" MONGOID: #{ 命名空间} 分片的为 #{ model} 进行分 片 ") 来年 模型 end admin_db = 模型.集合.客户端.请使用(: admin).database 开始 admin_db.命令(enableSharding: 模型.集合.database.名称) 救援 mongo::错误::OperationFailure => 排除 # 如果已启用分片,则MongoDB Server 2.6 会失败 if 排除.代码 == 23 || 排除.代码.nil? && 排除. =~ /已启用/ # 无 else 提高 end end 开始 admin_db.命令(shardCollection: 模型.集合.namespace, **模型.shard_config) 救援 mongo::错误::OperationFailure => e 记录器.错误(" 分片 :集合 对 #{ model}: #{ e. class}: #{ e } 的集合 #{ model. 集合 命名空间} } 进行 分片 : #{ e} ") 来年 end 记录器.信息(" MONGOID : #{ model} 的 分 片 集合 #{ model. 集合 命名空间} ") 模型 end.compact end |
# undefined_indexes (models =::Mongoid.models) ⇒数组<Hash>
按模型返回数据库中存在但未在模型上指定的索引列表。
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mongoid/tasks/ 数据库.rb', line 86 def undefined_indexes(模型 = ::Mongoid.模型) undefined_by_model = {} 模型.每 do |模型| 除非 模型. 开始 模型.集合.索引(会话: 模型.发送(:_session)).每 do |索引(index)| # ignore default index 除非 索引(index)[' name '] == ' _id_ ' key = 索引(index)['key'].symbolize_keys spec = 模型.index_说明(key, 索引(index)[' name ']) 除非 spec # 未指定索引 undefined_by_model[模型] ||= [] undefined_by_model[模型] << 索引(index) end end end 救援 mongo::错误::OperationFailure; end end end undefined_by_model end |