I just made a new rails 5.0.0 app. Everything has been working hunky dory until I decided to precompile assets for Heroku:
RAILS_ENV=production bundle exec rake assets:precompile
I then see this error. I do not know why it wants the role to be my App's Name? I've been precompiling up to this point. Only two migration files added (listed below).
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: role "MYAPPNAME" does not exist
/Users/james/MyApps/MYAPPNAME/app/models/model.rb:6:in `get_percent_change'
/Users/james/MyApps/MYAPPNAME/lib/tasks/trade_wave_tasks.rake:3:in `block in <top (required)>'
/Users/james/MyApps/MYAPPNAME/config/application.rb:12:in `<class:Application>'
/Users/james/MyApps/MYAPPNAME/config/application.rb:10:in `<module:MYAPPNAME>'
/Users/james/MyApps/MYAPPNAME/config/application.rb:9:in `<top (required)>'
/Users/james/MyApps/MYAPPNAME/Rakefile:4:in `require_relative'
/Users/james/MyApps/MYAPPNAME/Rakefile:4:in `<top (required)>'
/Users/james/.rbenv/versions/2.3.1/bin/bundle:23:in `load'
/Users/james/.rbenv/versions/2.3.1/bin/bundle:23:in `<main>'
PG::ConnectionBad: FATAL: role "MYAPPNAME" does not exist
/Users/james/MyApps/MYAPPNAME/app/models/model.rb:6:in `get_percent_change'
/Users/james/MyApps/MYAPPNAME/lib/tasks/trade_wave_tasks.rake:3:in `block in <top (required)>'
/Users/james/MyApps/MYAPPNAME/config/application.rb:12:in `<class:Application>'
/Users/james/MyApps/MYAPPNAME/config/application.rb:10:in `<module:MYAPPNAME>'
/Users/james/MyApps/MYAPPNAME/config/application.rb:9:in `<top (required)>'
/Users/james/MyApps/MYAPPNAME/Rakefile:4:in `require_relative'
/Users/james/MyApps/MYAPPNAME/Rakefile:4:in `<top (required)>'
/Users/james/.rbenv/versions/2.3.1/bin/bundle:23:in `load'
/Users/james/.rbenv/versions/2.3.1/bin/bundle:23:in `<main>'
Tasks: TOP => get_percent_change
(See full trace by running task with --trace)
Initially, any rake command gave me an issue. I fixed those issues and running all rake commands works--except this one! (ie, rake db:reset) I also created the postgresql USER and ROLE, but it still says the same error. I restart psql, and rake db:reset.
Here are the migration files:
Created the model:
class CreateMyModel < ActiveRecord::Migration[5.0]
def change
create_my_model :models do |t|
t.timestamps
end
end
end
Added a Column:
class AddPercentChangeToMyModelTable < ActiveRecord::Migration[5.0]
def change
remove_column :my_models, :percent_change, :string
add_column :my_models, :percent_change, :string
end
end
Changed it to a float:
class ChangePercentChangeToFloatOnMyModelTable < ActiveRecord::Migration[5.0]
def change
add_column :my_models, :percent_change, :float, precision: 8
end
end
I'm also using whenever gem so I have a rake job (located in tasks/my_rake_job.rake)
desc 'get latest percent change'
task get_percent_change: :environment do
MyModel.get_percent_change
end
And here is my schema:
create_table "bots", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "mymodels", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "percent_change"
end
create_table "tweets", force: :cascade do |t|
t.string "message"
t.string "string"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Config/database
# PostgreSQL. Versions 9.1 and up are supported.
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: MYAPP_development
test:
<<: *default
database: MYAPP_test
production:
<<: *default
database: MYAPP_production
username: MYAPP
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
And my gem file:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
gem 'twitter'
gem 'figaro'
gem 'randumb'
gem 'whenever', :require => false
gem "stock_quote"
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console'
gem 'listen', '~> 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
After dropping the role and re making it my psql \du table looks like this:
ccap | Superuser, Create DB | {}
Please let me know what other files are necessary to debug.
This issue is with the method I am calling in my rake
desc 'get latest percent change'
task get_percent_change: :environment do
MyModel.get_percent_change
end
To elaborate more on this, this rake task was calling a model method that was trying to create something. Instead to create something the rake called a model method, that method returned a float and then the rake job did something like this:
current_percent = Model.get_percent_change
new_data = Model.new
new_data.percent_change = current_percent
new_data.save
When pushing to heroku, because I attempted to change a column name, this solution is very helpful: How do I change column type in Heroku?
class ModifyContacts < ActiveRecord::Migration
def self.up
rename_column :contacts, :date_entered, :date_entered_string
add_column :contacts, :date_entered, :date
Contact.reset_column_information
Contact.find(:all).each { |contact| contact.update_attribute(:date_entered, contact.date_entered_string) }
remove_column :contacts, :date_entered_string
end
end