unable to rake db:migrate - ruby

everyone one I am working on a Sinatra app. I am trying to use Active Record as my database option. This is my first time of using Active Record with Sinatra.
I have successfully created my table, but I am getting an error whenever I try to "rake db:migrate".
I have gone through likely issues I found on Stack overflow, but not getting any headway. Any help would be appreciated.
Error message for "rake db:migrate
rake aborted!
ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Below is my Rakefile
require_relative './app/controllers/recipe_controller'
require 'sinatra/activerecord/rake'
task :console do
Pry.start
end
Below is my config/environment file
require 'bundler'
require 'bundler/setup'
Bundler.require
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'db/development.sqlite'
)
require './app/controllers/recipe_controller'
Below is config.ru file
require "sinatra"
require './app/controllers/recipe_controller'
run RecipeController
my create_recipes file
class CreateRecipes < ActiveRecord::Migration[7.0]
def change
create_table :recipes do |t|
t.string :name
t.text :description
t.boolean :completed
t.timestamps
end
end

Related

How to run migrations for test suite.

I have a ruby app that uses ActiveRecord and sqllite. I am trying to write tests but I get this error:
Failure/Error: user = described_class.create(name: name)
ActiveRecord::StatementInvalid:
Could not find table 'users'
This is my gemfile:
source "https://rubygems.org"
gem "sinatra-activerecord"
gem "sqlite3"
group :test do
gem 'database_cleaner'
end
group :test, :development do
gem "rspec-rails", ">= 2.1.0"
gem "pry"
end
I have a spec_helper that looks like this:
RSpec.configure do |config|
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/test.db')
end
What can I do to create a test database and run the migrations for my sqllite tests?
bin/rails RAILS_ENV=test db:migrate and/or bin/rails db:test:prepare will update your test db. You also might want to check out the Rails Testing Guides too - lots of helpful info there. Or if you want to avoid fixtures, FactoryBot is great for generating test data.
Ginnie's answer can work, but if like Sean mentionned you need an ActiveRecord only solution instead of using Rails consider this :
I recently developed a gem to bundle some ActiveRecord models without using Rails.
Here is what I did to test my models with rspec:
spec/spec_helper.rb:
ActiveRecord::Base.establish_connection(adapter: 'sqlite3',
database: ':memory:')
ActiveRecord::Schema.define do
require_relative '../lib/db/migrate/create_models'
end
lib/db/migrate/create_models.rb: (to make this file I copy/pasted the actual schema.rb generated by migrations)
class CreateModels < ActiveRecord::Migration[5.1]
create_table "users", force: :cascade do |t|
t.string "uid", default: "", null: false
t.string "email", null: false
// other attributes
end
// other tables
end
This will create your needed tables for your tests to run.
Don't forget to require spec_helper in your *_spec.rb file and you're good to go.

Sinatra - ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base

I've only been able to find rails answers to this question. Whenever I run rake db:migrate I am getting the aforementioned error. As far as I am aware, I have setup everything correctly so have no idea what's wrong.
config/environment.rb
ENV['SINATRA_ENV'] ||= "development"
require 'bundler/setup'
Bundler.require(:default, ENV['SINATRA_ENV'])
configure :develpoment do
set :database, 'sqlite3:db/database.db'
end
require './app'
Rakefile
require "./config/environment"
require "sinatra/activerecord/rake"

Guard warning "warning: loading in progress, circular require considered harmful"

I have a simple Ruby test envinvorment set up with:
minitest, guard, guard-minitest, and terminal-notifier-guard.
I'm using the following Rakefile so my tests are run by default because that's what Travis CI does by default.
require 'rake/testtask'
task :default => [:test]
Rake::TestTask.new do |t|
t.libs << 'test'
t.pattern = "test/test_*"
end
The tests do run and pass but I get multiple screens worth of warnings. I found an answer and another answer.
But it seems like those solutions are specific to rails and rspec.
Why am I getting these warnings?
You can find the full project on GitHub and the full error output in this gist
If you just want to turn off the warnings, you can do so in the rake test task setup:
require 'rake/testtask'
task :default => [:test]
Rake::TestTask.new do |t|
t.libs << 'test'
t.pattern = "test/test_*"
t.warning = false
end

Why am I getting this error when I use 'rackup' but not when I use 'ruby'

I am new to Ruby and Sinatra, and I am attempting to finish this tutorial: http://www.sitepoint.com/just-do-it-learn-sinatra-iv/
At the end of the tutorial, before deployment, the tutorial says "Before we move on to deploying the app, we should just check that everything is running fine on our local machine. This is done in a different way now that we have a rackup file." It then instructs the student to enter "rackup" at the command line. When I do so, then go to the 9292 local port specified in my browser I get the error:
DataObjects::ConnectionError at / file is encrypted or is not a database
But when I run my application by typing "ruby main.rb" in the same directory, the app works fine in my browser. Is the tutorial wrong? Am I fine to just deploy my app to Heroku as is? Or do I need to get it working using the rackup command?
I am using sqlite3 1.3.9 and ruby 2.0.0
here is my 'main.rb' file:
require 'sinatra'
require 'data_mapper'
require 'slim'
require 'sass'
DataMapper.setup(:default, 'sqlite3::memory:')
class Task
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
property :completed_at, DateTime
belongs_to :list
end
class List
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
has n, :tasks, :constraint => :destroy
end
Task.auto_upgrade!
List.auto_upgrade!
#checks integrty of models
DataMapper.finalize
#For using SASS
get('/styles.css'){ content_type 'text/css', :charset => 'utf-8' ; scss :styles }
get '/' do
#lists = List.all(:order => [:name])
slim :index
end
post '/:id' do
List.get(params[:id]).tasks.create params['task']
redirect to('/')
end
delete '/task/:id' do
Task.get(params[:id]).destroy
redirect to('/')
end
#this adds a button that toggles a checkmark on and off
#so you can check something off your list
put '/task/:id' do
task = Task.get params[:id]
task.completed_at = task.completed_at.nil? ? Time.now : nil
task.save
redirect to('/')
end
post '/new/list' do
List.create params['list']
redirect to('/')
end
delete '/list/:id' do
List.get(params[:id]).destroy
redirect to('/')
end
my config.ru file:
require 'bundler'
Bundler.require
require './main'
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")
run Sinatra::Application
and my Gemfile
source :rubygems
gem "sinatra"
gem "datamapper"
gem "slim"
gem "sass"
gem "dm-postgres-adapter", :group => :production
gem "dm-sqlite-adapter", :group => :development
gem 'tilt', '~> 1.4.1'
I also have a config file in a .bundle folder that says
---
BUNDLE_WITHOUT: production
I'm not sure where to start.
My code is already different in a few ways than the final code posted at:
https://github.com/daz4126/Just-Do-It
because I have already had some errors that I had to make workarounds for. Copy pasting the final code doesn't make it work.
Thanks in advance. I have other files I can post, just figured the relevant files were these.

I am trying to run a rake task involving datamapper on heroku using scheduler but it seems that heroku is trying to use sqlite?!

I have a rakefile.rb in which there is task called add_latest_apps. This task creates some tables in a database if they have not been create using datamapper, scrapes some info from an xml feed and then puts the data in the tables. The problem is that I can't seem to get my rake task to run.
If I use the following command heroku run rake add_latest_apps then I get: rake aborted! no such file to load -- dm-sqlite-adapter which suggests to me that is trying to use the sqlite adapter even though it should be using the postgres adapter. What am I doing wrong?
rakefile.rb:
require 'open-uri'
require 'nokogiri'
require 'awesome_print'
require 'data_mapper'
require 'dm-postgres-adapter'
require 'pg'
task :add_latest_apps do
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/app.db")
class Company
include DataMapper::Resource
property :id, Serial
property :company_name, String
property :company_id, Text, :unique => true
has n, :apps
end
class App
include DataMapper::Resource
property :id, Serial
property :app_id, Integer
property :bundle_id, Text
property :app_name, Text
property :company_name, Text
property :created_at, DateTime
property :rank, Integer
belongs_to :company
end
DataMapper.finalize.auto_upgrade!
puts 'Database and tables created'
#Get App data from Apple
#Insert apps and companies into database
end
Gemfile:
source :rubygems
gem 'sinatra', '1.1.0'
gem 'thin'
gem 'nokogiri'
gem 'awesome_print'
gem 'data_mapper'
gem 'dm-postgres-adapter'
gem 'pg'
Please help!
You need to set the DATABASE_URL environment variable.
The problem is with the line:
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/app.db")
Here DataMapper looks for ENV['DATABASE_URL'], and if it isn’t set falls back to "sqlite3://#{Dir.pwd}/app.db", which causes it to try to load dm-sqlite-adapter.
Run heroku config to show your environment vars, it probaby won’t show DATABASE_URL. Check out the Heroku Postgres docs to determine what your URL should be. You may be able to do something like heroku pg:promote HEROKU_POSTGRESQL_GRAY_URL (although you’ll need to check what colour you should use).

Resources