问题
I have a remote read-only postgres database that is maintained from a docker instance of cardano-db-sync.
I managed to connect the development database to it, it was working fine. But since it is read-only I wanted to add another database for User and other modifiable tables.
this is the set up I prepared:
# config/database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
primary:
database: 'db_sync'
cexplorer:
database: 'cexplorer'
username: <%= ENV['HOST_PG_DATABASE_USERNAME'] %>
password: <%= ENV['HOST_PG_DATABASE_PASSWORD'] %>
host: <%= ENV['HOST_PG_DATABASE_IP'] %>
port: <%= ENV['HOST_PG_PORT'] %>
then the abstracted classes as in the doc instructions
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
connects_to database: { reading: :primary, write: :primary}
end
# app/models/cexplorer_record.rb
class CexplorerRecord < ApplicationRecord
self.abstract_class = true
connects_to database: { reading: :cexplorer }
end
then some tables to test:
# app/models/pool_hash.rb
class PoolHash < CexplorerRecord
self.table_name = 'pool_hash'
end
# app/models/pool.rb
class Pool < ApplicationRecord
end
The primary database seems to work fine with rails c
but once using PoolHash I get this error:
$ rails c
Running via Spring preloader in process 79267
Loading development environment (Rails 6.0.3.4)
2.6.1 :001 > PoolHash.count
Traceback (most recent call last):
4: from (irb):1
3: from app/models/pool_hash.rb:1:in `<main>'
2: from app/models/cexplorer_record.rb:1:in `<main>'
1: from app/models/cexplorer_record.rb:4:in `<class:CexplorerRecord>'
ActiveRecord::AdapterNotSpecified (The `cexplorer` database is not configured for the `development` environment.)
Available databases configurations are:
default
development
2.6.1 :002 > Pool.count
(0.5ms) SELECT COUNT(*) FROM "pools"
=> 0
2.6.1 :003 >
I don't get why it says cexplorer is not configured for development when is instead right there in database.yml
in the same config that worked when it was without the :primary
UPDATE
it seems that by moving the <<: *default
things improved:
development:
primary:
<<: *default
database: 'swan_db_sync'
cexplorer:
<<: *default
database: 'cexplorer'
username: <%= ENV['HOST_PG_DATABASE_USERNAME'] %>
password: <%= ENV['HOST_PG_DATABASE_PASSWORD'] %>
host: <%= ENV['HOST_PG_DATABASE_IP'] %>
port: <%= ENV['HOST_PG_PORT'] %>
but I now get a different problem:
rails c
Running via Spring preloader in process 79798
Loading development environment (Rails 6.0.3.4)
2.6.1 :001 > Pool.count
(4.2ms) SELECT COUNT(*) FROM "pools"
=> 0
2.6.1 :002 > PoolHash.count
Traceback (most recent call last):
1: from (irb):2
ActiveRecord::ConnectionNotEstablished (No connection pool with 'CexplorerRecord' found.)
UPDATE 2
I have now tried a setup as in this article managing multiple databases in a single rails application link from 2017.
It works fine. It also makes more sense for my goal than what is in the rails documentation. I hope being an old article won't give me surprises down the line.
来源:https://stackoverflow.com/questions/65541744/multiple-databases-with-rails-not-working-for-remote-database