Heroku create db with default data - ruby-on-rails

I'm noobie in rails and I have a problem with db. My database.yml:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
And I succeeded in filling my db with this
namespace :db do
desc "Erase and fill database"
task :populate => :environment do
require 'populator'
require 'faker'
[Country, Region, City, Turbaza].each(&:delete_all)
ActiveRecord::Base.transaction do
Country.populate 5 do |country|
country.name = Faker::Address.country
Region.populate 1..2 do |region|
region.country_id = country.id
region.name = Faker::Address.state
City.populate 1..2 do |city|
city.region_id = region.id
city.name = Faker::Address.city
Turbaza.populate 1..2 do |turbaza|
turbaza.city_id = city.id
turbaza.name = Populator.words(1..3).titleize
end
end
end
end
end
end
end
So I've got my development db filled with data, but I cannot understand how I can do this to my production db and. Please, could anyone help me with this?

if you have already deployed you app into Heroku you can run you rake task via heroku-cli
https://devcenter.heroku.com/articles/heroku-command
heroku run rake db:populate
P.S Heroku provides only pg for free. So i you want to upload your up with pg i have to do
add gem 'pg' to Gemfile
bundle install
commit you changes
rebuild your project in heroku

Running heroku run rake db:populate should do the trick, have you tried? And (IMO) better solution would be just using seeds (http://railscasts.com/episodes/179-seed-data) and running heroku run rake db:seed

Related

ActiveRecord task executing in wrong environment with Whenever

I have to run a Cron task involving some data cleaning in my ActiveRecord database. I am using Whenever gem. Here is the code :
schedule.rb
every 1.hour do
rake 'notifications:clear'
end
notifications.rake
namespace :notifications do
task clear: :environment do
Rpush::Notification.delete_all
end
end
Running this gives me the following error:
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: role "user_prod" does not exist
I am in development environment. Here is my database.yml file :
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: development_database
test:
<<: *default
database: test_database
staging:
<<: *default
database: staging_database
username: user_staging
password: <%= ENV['DATABASE_PASSWORD'] %>
production:
<<: *default
database: production_database
username: user_prod
password: <%= ENV['DATABASE_PASSWORD'] %>
Any ideas on why my ActiveRecord instruction seems to connect to my production environment ? Thanks in advance !
Update Your code with below :-
In schedule.rb file :-
every 1.hour do
rake 'notifications:clear', :environment => "development"
end
And finally executed this command :-
whenever --update-crontab
OR
Clear existing cron jobs.
crontab -r
Update cronjob with the environment.
whenever --update-crontab --set environment='development'

Launch rspec tests with sqlite :memory: - "migrations are pending"

I want to run tests using rspec on my ruby on rails app using sqlite with the memory database. However, everytime I launch rspec it tells me that migrations are pending, event if I run the migrations before hand. Is there a way to do the migrations everytime before I run the tests ?
Here's my database configuration
test:
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
database: ":memory:"
You need to load schema in your tests instead of relying on migrations.
As advised in this blogpost replace
ActiveRecord::Migration.maintain_test_schema!
with
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.verbose = false
load "#{Rails.root.to_s}/db/schema.rb"

undefined method `geometry' for ActiveRecord::ConnectionAdapters::PostgreSQL

I have created a rails application using postgres database. I am using postgis extension for geo queries. The app is running successfully on my development(local) machine but after deploying my code on heroku server when I run heroku run rake db:migrate it is throwing an error, saying undefined method geometry for ActiveRecord ConnectionAdapters PostgreSQL.
I have geometry datatype in some migrations for storing latitude and longitude.
Note that I have also created PostGIS extension on heroku. And migrations that does not contain geometry datatype executed successfully.
My files are:
Gemfile
ruby "2.3.0"
gem 'rails', '>= 5.0.0.beta3', '< 5.1'
gem 'pg', '~> 0.18'
gem 'rgeo'
gem 'rgeo-activerecord', "~> 5.0.0.beta"
gem "activerecord-postgis-adapter", "~> 4.0.0.beta2"
psql --version is: 9.5.2 on heroku server
psql --version is: 9.4.7 on local server
database.yml
default: &default
adapter: postgis
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: ad_development
production:
<<: *default
database: ad_production
username: ad
password: <%= ENV['DATABASE_PASSWORD'] %>
create_cities migration
def change
create_table :cities do |t|
t.string :name
t.references :state, foreign_key: true
t.geometry :lat_lan
end
heroku run rake db:migrate stops here only.
I am totally confused whether I have used inappropriate gems or I have misconfigured something. Please help!
If using the DATABASE_URL environment variable to set the database connection string, ensure it has the postgis:// (not postgres://) prefix.
ie. postgis://username:password#db_server_url:5432/dbname
url: <%= ENV.fetch('DATABASE_URL', '').sub(/^postgres/, 'postgis') %>
EDIT:
It replaces the url scheme postgres://somewhere.com to postgis://somewhere.com.
It changes to the GIS "protocol", like changing http to https.
You have to replace postgresql with postgis in your database.yml file for the adapter option.
E.g.
default: &default
adapter: postgis
encoding: unicode
pool: <%= ENV["DB_POOL"] || ENV['MAX_THREADS'] || 5 %>
url: <%= ENV['DATABASE_URL'] %>
development:
<<: *default
database: yourapp_development

Using Sqlite3 test database and Postgres dev/production database in Rails

I currently have a rails project which I deploy to a production server which uses a postgres database. I develop my rails project in Windows, which means that if I want to test locally, I have to change all of the databases in my database.yml file from postgres over to sqlite3 (because setting up Windows to run a postgres server appears to be a pain).
What I would like to be able to do is format my database.yml something like this:
development:
adapter: postgresql
encoding: utf8
database: <%= begin IO.read("/home/www-data/.db/.dev_name") rescue "" end %>
pool: 5
username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: utf8
database: <%= begin IO.read("/home/www-data/.db/.prod_name") rescue "" end %>
pool: 5
username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>
That way I can run rails s -e test locally and test with an sqlite3 database, but when I deploy to my development and production servers I can use postgres.
The problem I am having is that, with the changes to my database.yml shown above, when I run rails s -e test locally I get an error saying that rails could not find the pg gem which seems to imply that it is still trying to use either the development or the production server.
With all the warnings acknowledged, the answer to the question would be to use group in your Gemfile like
gem 'pg', group: [:development, :production]
gem 'sqlite3', group: :test

Factory girl saving records in my development database

I have a very strange problem and I don't know where I should look to find it. I am developing a rails 3 app using rspec and factory girl for testing. For some reason, whenever I run any rails commands (eg to rake the db, start the dev server etc) one factory user is created and stored in my development database. The worst part is, it always has the same email, which I am validating the uniqueness of in my app, so the commands won't run until I go in manually delete the record.
I have looked all through my factories file and I don't think I am doing anything strange there, and suggestions where else I might for the code that is doing this?
EDIT: HERE IS MY database.yml
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_development
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_test
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_production
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
I figured it out. In my Gemfile, I had:
group :development, :test do
gem 'capybara'
gem "rspec-rails"
gem "guard-rspec"
gem "factory_girl_rails"
...
end
I moved factory girl out of this block onto its own line so it is in the test group only like this:
gem 'factory_girl_rails', :group => :test
No more problems
db/Seeds.rb maybe...but I think that only runs on db:reset and db:seed

Resources