How to use annotate with Rails 6.1 multiple databases feature?
Opened this issue · 1 comments
https://guides.rubyonrails.org/active_record_multiple_databases.html
With Rails 6 multiple databases feature, I can't annotate models because they are connected to multiple databases via connects_to
block.
Commands
class GlobalRecord < ActiveRecord::Base
self.abstract_class = true
connects_to shards: {
db1: { writing: :primary, reading: :primary },
db2: { writing: :db2, reading: :db2 }
}
end
Errors
Unable to annotate app/models/club.rb: No connection pool for 'GlobalRecord' found.
Unable to annotate app/models/member.rb: No connection pool for 'ShardRecord' found.
Unable to annotate app/models/user.rb: No connection pool for 'GlobalRecord' found.
Version
- annotate version:
latest
- rails version:
latest
- ruby version:
latest
Hi Regedarek,
After running into problems with annotate
myself – in my case, it was not the multi database setup, but our application's many different directories for models – I have developed a gem which fixes the issues. This gem, datagaze
, is heavily inspired by annotate
, but simply tries to fix the problem you are experiencing. The gem is to be found at: https://github.com/jurriaanschrofer/datagaze.
Basically, annotate's lookup system is often rigid and based on very strict path conventions. Instead, I have found a way of annotating more dynamically, through the use of ruby's const_source_location
method and rails' ApplicationRecord.descendants
.
Would you, if you choose to try the gem out, please reach out to me with feedback? I would love to hear about your experience.