Rails 6 while running sidekiq database configuration does not specify adapter - ruby-on-rails

Recently we started migrating our codebase from rails 5 to rails 6. Everything seems to work fine except sidekiq. Whenever we tried to run sidekiq in production mode it always throws an error database configuration does not specify adapter. I am pretty sure that we have mentioned an adapter in database.yml. Can someone please help to resolve this issue?
For reference
Rails 6.0.3.4
Sidekiq 6.1.2
Ruby 2.7.1p83
databsae.yml file
default: &default
adapter: postgresql
pool: <%= ENV['DATABASE_POOL'] %>
timeout: 5000
database: anajmandi
development:
primary:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
multidb:
fallback: true
follower:
<<: *default
url: <%= ENV['DATABASE_SLAVE_URL'] %>
replica: true
multidb:
fallback: true
test:
<<: *default
database: db/test.sqlite3
production:
primary:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
multidb:
fallback: true
follower:
<<: *default
url: <%= ENV['DATABASE_SLAVE_URL'] %>
replica: true
multidb:
fallback: true
and sidekiq.rb file
# typed: strict
if Rails.env.production?
# three unicorns = 3 connections
Sidekiq.configure_client do |config|
config.redis = { :size => 1 }
end
Sidekiq.configure_server do |config|
config.redis = { url: ENV['REDIS_URL'], size: 22 }
Rails.application.config.after_initialize do
Rails.logger.info("DB Connection Pool size for Sidekiq Server before disconnect is: #{ActiveRecord::Base.connection.pool.instance_variable_get('#size')}")
ActiveRecord::Base.connection_pool.disconnect!
ActiveSupport.on_load(:active_record) do
config = Rails.application.config.database_configuration[Rails.env]
config['reaping_frequency'] = ENV['DATABASE_REAP_FREQ'] || 10 # seconds
config['pool'] = ENV['WORKER_DB_POOL_SIZE'] || Sidekiq.options[:concurrency]
ActiveRecord::Base.establish_connection(config)
Rails.logger.info("DB Connection Pool size for Sidekiq Server is now: #{ActiveRecord::Base.connection.pool.instance_variable_get('#size')}")
end
end
end
end
and this is the error stack trace on running command bundle exec sidekiq -e production -C config/sidekiq.yml
DB Connection Pool size for Sidekiq Server before disconnect is: 5
database configuration does not specify adapter
/Users/yadusingla/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/connection_specification.rb:161:in `spec'
/Users/yadusingla/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1052:in `establish_connection'
/Users/yadusingla/.rvm/gems/ruby-2.7.1/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:51:in `establish_connection'
/Users/yadusingla/.rvm/gems/ruby-2.7.1/gems/activerecord-import-1.0.6/lib/activerecord-import/import.rb:250:in `establish_connection'
/Users/yadusingla/spars/procol-backend/config/initializers/sidekiq.rb:19:in `block (3 levels) in <top (required)>'
/Users/yadusingla/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:71:in `class_eval'
/Users/yadusingla/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook'
/Users/yadusingla/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:61:in `with_execution_control'
/Users/yadusingla/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:66:in `execute_hook'
/Users/yadusingla/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:43:in `block in on_load'
/Users/yadusingla/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:42:in `each'
/Users/yadusingla/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:42:in `on_load'

Here I am using a 3-tier configuration, so for 3 tier configuration initializer isn't correctly defined. If we select configurations by environment variable then we'll get two, both primary and follower. Rails don't know which one to access so the app has to select the right one.
Change config = Rails.application.config.database_configuration[Rails.env] to
self.configurations = Rails.application.config.database_configuration
config = configurations.configs_for(env_name: Rails.env, spec_name: "primary").config
Refer to this link https://github.com/rails/rails/issues/40640

Related

rake db:create -> stack level too deep, couldn't create database, please check your configuration

I have an old app dating all the way back to Rails 2. We are currently upgrading it.
The database is refusing to create with the settings below. What could be going on?
When I run rake db:create:
Calling `DidYouMean::SPELL_CHECKERS.merge!(error_name => spell_checker)' has been deprecated. Please call `DidYouMean.correct_error(error_name, spell_checker)' instead.
Running cron.rake
Running roles.rake
begin Rakefile
development
stack level too deep
Couldn't create 'project_dev' database. Please check your configuration.
rake aborted!
SystemStackError: stack level too deep
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/instance_variables.rb:15:in `block in instance_values'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/instance_variables.rb:15:in `map'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/instance_variables.rb:15:in `instance_values'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:63:in `as_json'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:180:in `block in as_json'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:179:in `each'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:179:in `as_json'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:63:in `as_json'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:180:in `block in as_json'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:179:in `each'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:179:in `as_json'
... repeats many times ...
database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: <%= Rails.application.credentials.database[:username] %>
password: <%= Rails.application.credentials.database[:password] %>
development:
<<: *default
database: project_dev
host: localhost
test:
<<: *default
database: project_test
psql postgres:
postgres=# \du
Role name | Attributes | Member of
--------------+------------------------------------------------------------+-----------
applyco | Superuser | {}
postgres=# \list
(List does not include project_dev or project_test).
Remove the gem 'meta_request' to fix the issue.
Source: https://github.com/rails/rails/issues/40781#issuecomment-742807721

Rails Rspec test fails: Failure/Error: FATAL: database "jmschp" does not exist

I have a project with Rails 5.2.4.5. Where I am using Rspec for testing, and Postgres as database.
RSpec 3.10
- rspec-core 3.10.1
- rspec-expectations 3.10.1
- rspec-mocks 3.10.2
- rspec-support 3.10.2
When I run rails spec:helpers I am getting the following error:
An error occurred while loading ./spec/helpers/pluralize_without_count_helper_spec.rb.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!
ActiveRecord::NoDatabaseError:
FATAL: database "jmschp" does not exist
# ./spec/rails_helper.rb:30:in `<top (required)>'
# ./spec/helpers/pluralize_without_count_helper_spec.rb:1:in `<top (required)>'
# ------------------
# --- Caused by: ---
# PG::ConnectionBad:
# FATAL: database "jmschp" does not exist
# ./spec/rails_helper.rb:30:in `<top (required)>'
No examples found.
I have droped the data base and recreated it again.
In my database.yml I have:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 6 } %>
development:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
database: store_development
test:
<<: *default
url: <%= ENV['TEST_DATABASE_URL'] %>
In development work fine.
How can I fix this?
I don't see the name of the test database setup in database.yml you can try below
test:
<<: *default
database: <%= ENV['TEST_DATABASE_URL'] || 'store_development_test' %>
and run this to make sure test database has all the migrations applied.
bin/rails db:test:prepare

rails credentials not fetching

I created rails config/master.key and config/credentials.yml.enc via rails credentials:edit command
on rails console i'm able to fetch values but when i run rake db:migrate, i get this error:
invalid port number: "Rails.application.credentials.development[:port]"
Couldn't create 'Rails.application.credentials.development[:database]' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: invalid port number: "Rails.application.credentials.development[:port]"
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/pg-1.1.4/lib/pg.rb:56:in `initialize'
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/pg-1.1.4/lib/pg.rb:56:in `new'
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/pg-1.1.4/lib/pg.rb:56:in `connect'
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
#config/environments/development.rb
config.require_master_key = true
#config/credentials.yml.enc
development:
database: xxxxxxxx
username: yyyyyyyy
password: zzzzzzzz
host: aaaaaaa.com
port: 1234
# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: 1234567890
#config/database.yml
default: &default
adapter: postgresql
encoding: utf8
database: Rails.application.credentials.development[:database]
username: Rails.application.credentials.development[:username]
password: Rails.application.credentials.development[:password]
host: Rails.application.credentials.development[:host]
port: Rails.application.credentials.development[:port]
development:
<<: *default
It looks that problem in syntax
You need to use interpolation like this:
database: <%= Rails.application.credentials.development[:database] %>
username: <%= Rails.application.credentials.development[:username] %>
password: <%= Rails.application.credentials.development[:password] %>
host: <%= Rails.application.credentials.development[:host] %>
port: <%=Rails.application.credentials.development[:port] %>

carrierwave-dropbox authorization

I have created an app on dropbox and I am following the https://github.com/robin850/carrierwave-dropbox steps but cannot get the access token.
It has 400 bad request although I am providing correct key and app secret but cannot get the link to allow my app.
Here is the error and I am creating a ruby-on-rails app:
rake aborted!
DropboxAuthError: Error getting request token. Is your app key and secret correctly set? Server returned 400: Bad Request.
/var/lib/gems/2.3.0/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:277:in `get_token'
/var/lib/gems/2.3.0/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:293:in `get_request_token'
/var/lib/gems/2.3.0/gems/dropbox-sdk-1.6.5/lib/dropbox_sdk.rb:299:in `get_authorize_url'
/var/lib/gems/2.3.0/gems/carrierwave-dropbox-1.2.1/lib/carrierwave/dropbox/rake.rb:13:in `authorize'
/var/lib/gems/2.3.0/gems/carrierwave-dropbox-1.2.1/lib/carrierwave/dropbox/authorize.rake:11:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.3.0/gems/rake-12.1.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => dropbox:authorize
(See full trace by running task with --trace)
Have you configured carrierwave
CarrierWave.configure do |config|
config.dropbox_app_key = ENV["APP_KEY"]
config.dropbox_app_secret = ENV["APP_SECRET"]
config.dropbox_access_token = ENV["ACCESS_TOKEN"]
config.dropbox_access_token_secret = ENV["ACCESS_TOKEN_SECRET"]
config.dropbox_user_id = ENV["USER_ID"]
config.dropbox_access_type = "dropbox"
end
Here is an example hope it can work:
1- create a file config/config.yml and provide here your keys:
default: &default
development:
<<: *default
app_key: "your_app_key"
production: &production
<<: *default
app_key: "your_app_key"
staging: &staging
<<: *default
app_key: "your_app_key"
2 - in environment.rb file
# Load the Rails application.
require_relative 'application'
APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
# Initialize the Rails application.
Rails.application.initialize!
3 - then you can use in in carrierwave.rb file
CarrierWave.configure do |config|
config.dropbox_app_key = APP_CONFIG['app_key']
//and your another configuration so on...
}
end
if you have right api keys permission then it should work. thanks

Elastic Beanstalk Deploying Issue - Production Database Is Not Configured

I am having issues deploying my Rails API to Elastic Beanstalk. I cannot seem to get rid of the following error: ActiveRecord::AdapterNotSpecified: 'production' database is not configured. Available: ["development", "test"]
Please help me figure this out. I am attaching details below.
/config/database.yml
default: &defaults
adapter: postgresql
encoding: unicode
min_messages: warning
pool: 5
timeout: 10000
username: postgres
host: localhost
production:
<<: *defaults
adapter: postgresql
encoding: unicode
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
pool: 5
/config/environments/production.rb
config.assets.compile = false
config.assets.initialize_on_precompile = false
EB Logs
[Instance: i-13bf48db] Command failed on instance. Return code: 1 Output: (TRUNCATED)...ib/rails/application.rb:389:in `block in run_tasks_blocks' /home/rof/cache/bundler/ruby/2.1.0/gems/sprockets-rails-2.3.1/lib/sprockets/rails/task.rb:64:in `block (2 levels) in define' Tasks: TOP => environment (See full trace by running task with --trace). Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/11_asset_compilation.sh failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.
and
+++ RUBY_VERSION=2.1.5
+++ export GEM_ROOT=/opt/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0
+++ GEM_ROOT=/opt/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0
++ (( 0 != 0 ))
+ cd /var/app/ondeck
+ su -s /bin/bash -c 'bundle exec /opt/elasticbeanstalk/support/scripts/check-for-rake-task.rb assets:precompile' webapp
+ '[' false == true ']'
+ su -s /bin/bash -c 'bundle exec rake assets:precompile' webapp
rake aborted!
ActiveRecord::AdapterNotSpecified: 'production' database is not configured. Available: ["development", "test"]
/home/rof/cache/bundler/ruby/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/connection_specification.rb:257:in `resolve_symbol_connection'
If it is not in .gitignore, please give the credentials in your database.yml for the environment that you want to do.

Resources