Can't generate schema.rb file with ActiveRecord - ruby

When I run 'rake db:migrate' it won't generate the schema.rb file. I ran almost every rake command already but it didn't change anything yet. Anyone, please? I'm still pretty new at this.
Here are some files that may be helpful:
My Gemfile:
source "http://rubygems.org"
gem "sinatra"
gem "activerecord", :require => "active_record"
gem "sinatra-activerecord", :require => "sinatra/activerecord"
gem "rake"
gem "require_all"
gem "sqlite3"
gem "thin"
gem "shotgun"
gem "pry"
gem "bcrypt"
gem "tux"
group :test do
gem "rspec"
gem "capybara"
gem "rack-test"
gem "database_cleaner", git: "https://github.com/bmabey/database_cleaner.git"
end
One of my created migrations:
class CreateUsers < ActiveRecord::Migration
def change
t.string :name
t.string :email
t.string :password_digest
end
end
environment.rb:
ENV['SINATRA_ENV'] ||= "development"
require 'bundler/setup'
Bundler.require(:default, ENV['SINATRA_ENV'])
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "db/#{ENV['SINATRA_ENV']}.sqlite"
)
require './app/controllers/application_controller'
require_all 'app'

Try specifying your ActiveRecord to version 5.2 on your Gemfile, since you're using that Ruby version. Also, make sure you include it on your generated migrations.
So on your case:
class CreateUsers < ActiveRecord::Migration[5.2]

Related

Where is this SQL Debug output coming from?

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"
)

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.

Resources