I am building a rails 4 app that does not use any database. I have successfully disabled ActiveRecord on my development machine by following a few guides online by deleting database.yml and replacing
require 'rails/all'
with
require "action_controller/railtie"
require "action_mailer/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"
It works locally but when I try to deploy it on the server running unicorn, I get this on the err logs
ERROR -- : ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
/home/rtb/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
the app worked fine on the production unicorn server when I had a database.yml on and activerecord enabled. Is there something I am missing?
The ConnectionManagement middleware from ActiveRecord is probably still active. This middleware manages the connection pool for each request. It should not be active if you haven't loaded ActiveRecord.
You can manually remove the middleware with the following line in your Rails configuration:
config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement"
Related
I have one rails API application (App1), which doesn't have any database (no schema.rb file), but i am using some other application's database without adding any heroku postgres addon so when i push this application to heroku, i set the environment variable as DATABASE_URL = [ Database url of App2 ], also i have added some test cases with mini-test and enabled CI in heroku, it will run the test before the deployment but that time i am getting this error
and this is my application.rb
require_relative 'boot'
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
# require "sprockets/railtie"
require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module DemoGroup
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
end
end
is there any way i can run this test in heroku
The correct way to do this is by attaching the Postgres addon from app2 to app1.
Lets say you set DATABASE_URL on app1 to point to the attachment on app2. Heroku then needs to do maintenance - which changes the database url as its shifted to another server. Heroku is nice enough to automatically update DATABASE_URL on app2 - but app1 will be pointing to an invalid url.
This assumes you have the Heroku CLI client installed.
First get a list of the attachments:
$ heroku addons
Remove the default Postgres addon
$ heroku addons:remove heroku-postgresql:dev --app app1
Replace app2 with the name of your application on heroku.
Attach the addon from the other application.
Then attach the Postgres add-on from app2 to app1:
$ heroku addons:attach attachment_name -a app1
Schema.rb
schema.rb is required for ActiveRecord to work properly. If app1 does actually create migrations you can just commit an "empty" schema.rb:
ActiveRecord::Schema.define(version: 0) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
end
I'm missing something, just not sure what. Sidekiq is up and running fine, I can see it in my terminal.
I have this worker, defined in app/workers/sqs_worker.rb
class SqsWorker
include Sidekiq::Worker
require 'aws-sdk'
def perform
#do something
end
end
And then in just a test file at app/test.rb I have the very simple code:
require 'sidekiq'
SqsWorker.new.perform_async
When I run the test.rb file I get this error: uninitialized constant SqsWorker (NameError)
Where did I go astray? I'm running Sidekiq (4.1.4)
I tried killing the running processes and restarting both Sidekiq and Redis to no luck.
uninitialized constant SqsWorker (NameError) indicates that your script in test.rb is not able to locate class SqsWorker
All you need to do is replace require 'sidekiq' with require_relative 'workers/sqs_worker' to make your script aware about location of SqsWorker class.
Probably, you ran the test.rb from outside of the scope of the application with something like that:
ruby app/test.rb
But for this purpose, You need to add to your test something like this:
require 'rubygems'
require 'bundler/setup'
require File.expand_path('../config/environment', __FILE__)
SqsWorker.new.perform_async
And run as this:
bundle exec ruby app/test.rb
Why do you need this? Because nowadays, the bundler manages your dependencies added to your app and therefore, you need to load the rails environment too and the last will load all the things under app/ basically.
I've been trying to get initial Capybara-tests working with my current project that uses graph database (neo4j) as the database. I tried to initialize the tests following the steps mentioned here https://github.com/neo4jrb/neo4j/wiki/How-To-Test . My neo4j test server is running in port :7475 When I run
rspec spec/features/users_sign_up.rb
I'm getting this error:
ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/connection_adapters/connection_specification.rb:257:in `resolve_symbol_connection': 'test' database is not configured. Available: [] (ActiveRecord::AdapterNotSpecified)
ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/connection_adapters/connection_specification.rb:224:in `resolve_connection'
... more stack
spec/features/users_sign_up.rb:4:in `<top (required)>'
So it's trying to use the database mentioned in database.yml. What other configuration do I need to set or disable besides adding the
config.neo4j.session_type = :server_db
config.neo4j.session_path = 'http://localhost:7475'
in test.rb?
I've attached the config/test-files to a pastebin-document http://pastebin.com/qXwyrK9k
Even though you're not using ActiveRecord, your app is still loading it and RSpec is angry that it doesn't know where to find its database. There are at least two possible solutions.
Quick and dirty, insert the following into config/database.yml:
test:
<<: *default
database: db/test.sqlite3
Ensure you have gem 'sqlite3' in your Gemfile.
Alternatively, if you know you're not going to use ActiveRecord and don't even want to load it in your app, you can change application.rb so it looks like this:
# require 'rails/all'
require "active_model/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
require 'neo4j/railtie'
And then comment out the following lines in your rails_helper.rb file:
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
I have application on Rails 3. Everything works just fine, but as we know best performance should be on Production mode.
When I set environment as Production in Justhost control panel and in environment.rb file
with
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Darbs::Application.initialize!
ENV['RAILS_ENV'] ||= 'production'
And touch tmp/restart.txt
It gives me error:
Ruby (Rack) application could not be started
Error message:
Admin is not a class
And when I check current environment with Rail.env.production? it returns false.
I tried several restarts and so on without success.
in production log file I get Connecting to database specified by database.yml
It means some problem with connecting with specified database?
Any suggestions ?
Thanks.
Does anyone know what the contents of config.ru should be for a Rails 2.3.18 app in production to run on Passenger/Unicorn/Puma?
So far I've got:
# Require your environment file to bootstrap Rails
require ::File.dirname(__FILE__) + '/config/environment'
# Dispatch the request
run ActionController::Dispatcher.new
but it's loading development instead of the correct production environment.
It turns out this is a perfect config.ru.
The real problem is that Unicorn's -E parameter sets RACK_ENV and Rails 2.3.18 needs RAILS_ENV in order to correctly detect the environment.
So, at the top of config/environment.rb, I've set ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] and this is working just great.