Where is this SQL Debug output coming from? - ruby

My output when running my ruby cli file includes lines like:
D, [2018-11-17T15:33:29.481676 #45237] DEBUG -- : Patient Load (0.6ms) SELECT "patients".* FROM "patients"
I copied things from other sample projects for my environment, Gemfile, and Rakefile, and obviously something is set up to output this. How do I turn it off?
My gemfile:
source "https://rubygems.org"
gem 'pry'
gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'pry-nav'
gem 'activesupport'
gem 'nokogiri'
gem "activerecord"
gem "sinatra-activerecord"
gem "sinatra"
gem "sqlite3"
gem "rake"
gem "database_cleaner"
group :test do
gem 'poltergeist'
gem 'capybara'
gem 'rspec'
end
My rakefile:
# ENV['SINATRA_ENV'] ||= "development"
$LOADED_FEATURES << 'fake/active_support/core_ext/hash'
require_relative './config/environment'
require 'sinatra/activerecord/rake'
task :console do
Pry.start
end
My environment:
ENV['SINATRA_ENV'] ||= "development"
$LOADED_FEATURES << 'fake/active_support/core_ext/hash'
require 'bundler/setup'
Bundler.require
ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: "db/#{ENV['SINATRA_ENV']}.sqlite"
)
require 'active_record'
require 'rake'
Dir[File.join(File.dirname(__FILE__), "../app/models", "*.rb")].each {|f| require f}
Dir[File.join(File.dirname(__FILE__), "../lib", "*.rb")].each {|f| require f}

Active record logs sql output in development mode by default. To disable it,
try adding this line before the section after it like so
ActiveRecord::Base.logger = nil
ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: "db/#{ENV['SINATRA_ENV']}.sqlite"
)

Related

Getting an error when try to do Heroku run rake db:migrate

I am making small Sinatra app and trying to put it on Heroku server.
when I do Heroku run rake db:migrate, Heroku is giving error "Gem::LoadError: Specified 'sqlite3' for database adapter, but the gem is not loaded. Add gem 'sqlite3' to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)." .
While I have a sqlite3 gem in development group in my gem file.
source 'http://rubygems.org'
ruby '2.3.1'
gem 'sinatra'
gem 'activerecord', :require => 'active_record'
gem 'sinatra-activerecord', :require => 'sinatra/activerecord'
gem 'sqlite3', :group => :development
gem 'rake'
gem 'require_all'
gem 'thin'
gem 'shotgun', :group => :development
gem 'pry'
gem 'bcrypt'
gem "tux"
gem 'rack-flash3'
group :test do
gem 'rspec'
gem 'capybara'
gem 'rack-test'
gem 'database_cleaner', git: 'https://github.com/bmabey/database_cleaner.git'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
Also, please see below environment file
require 'bundler/setup'
require 'rack-flash'
Bundler.require
configure :development do
ENV['SINATRA_ENV'] ||= "development"
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "db/#{ENV['SINATRA_ENV']}.sqlite"
)
end
configure :production do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
end
require_all 'app'
I also did "bundle install --without production" and pushed everything on Github. Does anyone have any possible solution?
Thanks a lot!
The error comes from having group => development in the gem 'sqlite3' line of your gemfile.
What you need to understand is what environment your Heroku server is using.
To check, try running heroku run console -a your-app-name from the command line (you need the Heroku CLI installed).
Now run Sinatra::Base.development? or Sinatra::Base.production?
The results of running these commands should help you understand why including group => development is causing rake db:migrate to fail on Heroku and not your local dev environment.

Sinatra: NoMethodError undefined method `slim'

What am I missing? I've included slim in my Gemfile & 'required' it.
config.ru:
require './sinatra.rb'
run Sinatra::Application
Gemfile:
source 'https://rubygems.org'
gem 'rack'
gem 'sinatra'
gem 'slim'
sinatra.rb:
require 'sinatra'
require 'slim'
get '/' do
slim :index
end

Why do my rspec tests run slower in Ruby 1.9.2 than 1.8.7?

I'm using spork to test a Sinatra application and with Ruby 1.9.2 the tests run in about 3.5 seconds but in Ruby 1.8.7 they average 1.2 seconds. I did try Ruby 1.9.3 and even JRuby but they had some errors with the gems I'm using. Is there a way to bring Ruby 1.9.2's rspec performance up to 1.8.7's level?
My Gemfile:
source :rubygems
gem 'sinatra', '1.3.1'
gem 'thin', '1.3.1'
gem 'haml', '3.1.4'
gem 'datamapper', '1.2.0'
gem 'dm-postgres-adapter', '1.2.0'
gem 'carrierwave', '0.5.8'
gem 'carrierwave-datamapper', '0.2.0'
group :test do
gem "dm-sqlite-adapter"
gem "spork"
gem "rspec"
gem "rack-test"
end
spec_helper.rb:
require 'rubygems'
require 'spork'
require 'sinatra'
require 'rack/test'
require 'rspec'
require File.join(File.dirname(__FILE__), '..', 'app.rb')
require File.join(File.dirname(__FILE__), '..', 'model/model.rb')
Spork.prefork do
set :environment, :test
set :files, "test_files"
end
Spork.each_run do
RSpec.configure do |config|
config.before(:each) { DataMapper.auto_migrate! }
config.after(:all) do
FileUtils.rm_rf(Dir["#{settings.root}/public/test_files"])
end
end
end
thanks!
There was a problem in the way ruby 1.9.2 require'd files during startup:
http://rhnh.net/2011/05/28/speeding-up-rails-startup-time
1.9.3 has a partial fix for this IIRC.
Not according to rspec's own test: https://gist.github.com/939865. It is supposed to be faster. It could be something slower in your stack.

Setting the environment in Gemfile for bundling install/update based on a customize file

hi, all
I build a sinatra app, the main files for bundling as the following,
environment.rb
require 'sinatra'
require 'sequel'
ENV['RACK_ENV'] = 'development'
configure :production do
#do something
end
configure :development, :test do
#do something
end
Gemfile
gem 'sinatra'
gem 'sequel'
gem 'pg', :group => :production
gem 'sqlite3', :group => [:development, :test]
So, how to let the bundle install based on the ENV['RACK_ENV'] in my environment.rb file.
When doing a bundler require you can specify which groups to be required.
For example:
require 'rubygems'
require 'bundler'
if ENV['RACK_ENV'] == 'development'
Bundler.require(:default, :development)
else
Bundler.require(:default)
require 'sinatra'
More info on the bundler site gemfile specifications found here.

Sinatra and Bundler

I'm trying to get Bundler setup so I can deploy my Sinatra app to server with all the correct gems.
I've created my Gemfile
source :gemcutter
gem 'sinatra', '1.0'
gem "nokogiri", "1.4.2"
gem "rack", "1.1.0"
gem "dm-core", "1.0.0"
gem "dm-migrations", "1.0.0"
gem "dm-sqlite-adapter", "1.0.0"
gem "pony", "1.0"
Next I created a Config.ru
require 'rubygems'
require 'bundler'
Bundler.setup
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'
require 'open-uri'
require 'nokogiri'
require 'csv'
require 'pony'
require 'parsedate'
require 'digest/md5'
require 'MyApp'
run MyApp
So far so good, so next I ran bundle install and got 'Bundle Complete' so now all I need to do is just Rackup
Then I get:
config.ru:18: undefined local variable or method `MyApp' for #<Rack::Builder:0x1227350 #ins=[]> (NameError)
from /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/builder.rb:46:in `instance_eval'
from /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/builder.rb:46:in `initialize'
from config.ru:1:in `new'
from config.ru:1
Here's a simple MyApp.rb which will trigger the same error
get '/' do
erb :index
end
What's going wrong? :(
If you tell Rack to run MyApp, you need to define class MyApp first (which you're supposed to do inside MyApp.rb). Derive your class from Sinatra::Base to make it a Sinatra-Rack-App that can be run from config.ru:
require 'sinatra/base'
class MyApp < Sinatra::Base
get '/' do
erb :index
end
end
See also Sinatra's README about modular Sinatra apps (search for the paragraph named "Modular Apps" on http://github.com/sinatra/sinatra/)
Additionally you may have your my_app.rb as follows:
require 'rubygems'
require 'bundler'
Bundler.setup
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'
require 'open-uri'
require 'nokogiri'
require 'csv'
require 'pony'
require 'parsedate'
require 'digest/md5'
And your config.ru like this:
require './my_app'
run Rack::URLMap.new '/' => Sinatra::Application
Hope this helps.
Best Regards
ED
As an alternative to creating a modular app (wrapping your Sinatra methods in a class extending Sinatra::Base), you can use:
run Sinatra::Application
in the config.ru file in place of
run MyApp
This might be a better option if you want to keep the simple Sinatra code.
See the docs for more info.

Resources