Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Ruby

Di chuyển liền mạch sang Mongoid 5: Cấu hình Driver cập nhật & Best Practices

Cấu hình Development

Một trong những điều đầu tiên khi khởi động Rails app sau khi nâng cấp Mongoid 5 là lỗi về config database không đúng.

Sửa dễ dàng, chỉ đổi sessions thành clients:

development: clients: default: database: appsignal_development hosts: - localhost:27017

Thay đổi Driver

Trong codebase chúng ta "drop down to the driver" nhiều để thực thi truy vấn trực tiếp trên moped/mongo-ruby-driver, thay vì dùng Mongoid, ví dụ tạo collection cho từng account. Ở đây cũng cần đổi session thành client. Thay đổi khác: read giờ mong đợi hash với key :mode, thay vì giá trị trực tiếp:

 def create_log_entry_collection Mongoid .client('default') # dùng to be `.session('default')` .with(:read => {:mode => :primary}) # dùng to be `read: => :primary` .database .command(:create => 'foo') end

Moped có phương thức insert chấp nhận single document hoặc array documents. Driver mongo-ruby-driver mới có hai phương thức riêng, chọn tùy số lượng document:

# Before Mongoid.client('default')['foo'].insert(document) Mongoid.client('default')['foo'].insert([document, document]) # After Mongoid.client('default')['foo'].insert_one(document) Mongoid.client('default')['foo'].insert_many([document, document])

Thiếu ordering

Một trong những thay đổi lớn nhất với driver mới là document không còn ordered theo _id mặc định.

First và last không còn thêm _id sort khi không có sorting options. Để đảm bảo document là first hoặc last, cần explicit sort.

Điều này nghĩa là bất cứ đâu dựa vào ordering (.first, .last) cần explicit order bằng _id:

# Before expect( User.first.name ).to eq 'bob' expect( User.last.name ).to eq 'kelso' # After expect( User.asc('_id').first.name ).to eq 'bob' expect( User.asc('_id').last.name ).to eq 'kelso' 

Để code hoạt động như cũ, tạo concern thêm default scope order by _id:

# concerns/ordered_by_id_asc.rb module OrderedByIdAsc extend ActiveSupport::Concern included do default_scope -> { asc('_id') } end end
# models/account.rb class Account include Mongoid::Document include Mongoid::Timestamps include OrderedByIdAsc end

FindAndModify

Find_and_modify đã bị xóa. Thay vào đó giờ có 3 phương thức chọn:

  • find_one_and_update
  • find_one_and_replace (Convenience method, gọi find_one_and_update)
  • find_one_and_delete

ExpireAfterSeconds

Một thay đổi ít người biết: cách tạo TTL index. Chúng ta dùng TTL index để tự động xóa dữ liệu khách hàng tùy plan (ví dụ sau 7 ngày, hoặc sau một tháng).

Option trên index trước gọi là expire_after_seconds, giờ đổi thành expire_after:

# Before collection.indexes.create_one( {:time => 1}, {:expire_after_seconds => ttl} ) # After: collection.indexes.create_one( {:time => 1}, {:expire_after => ttl} )

Thay đổi config Staging/Production

Trong development chỉ cần đổi sessions thành clients, nhưng config staging/production cần sửa nhiều hơn:

# Before staging: sessions: default: database: appsignal_main username: <%= ENV['MONGOID_USERNAME'] %> password: <%= ENV['MONGOID_PASSWORD'] %> hosts: - mongo1.staging:27017 - mongo2.staging:27017 - mongo3.staging:27017 options: read: :primary pool_size: {{ mongoid_pool_size }} ssl: ca_file: /etc/ssl/certs/root_ca.crt client_cert: /app/shared/config/mongodb_app.crt client_key: /app/shared/config/mongodb_app.key # After staging: clients: default: database: appsignal_main hosts: - mongo1.staging:27017 - mongo2.staging:27017 - mongo3.staging:27017 options: user: <%= ENV['MONGOID_USERNAME'] %> password: <%= ENV['MONGOID_PASSWORD'] %> read: mode: :primary max_pool_size: {{ mongoid_pool_size }} ssl: true ssl_ca_cert: /etc/ssl/certs/root_ca.crt ssl_cert: /app/shared/config/mongodb_app.crt ssl_key: /app/shared/config/mongodb_app.key replica_set: staging
  • username đổi thành user và chuyển vào options
  • password chuyển vào options
  • read giờ mong đợi nested key mode
  • SSL không còn nested hash, mà set dưới options
  • Config yêu cầu key replica_set nếu setup là replicaset

Tài liệu nâng cấp nói MongoDB 2.4 và 2.6 dùng :plain auth, nhưng chúng ta phải xóa key auth_mech hoàn toàn để setup chạy.

Kết luận

Mặc dù danh sách khá dài, chúng ta thấy nâng cấp tương đối nhẹ nhàng và driver mới cảm giác chắc chắn hơn driver Moped cũ.