Connect to sqlserver from heroku - ruby-on-rails

i have a remote SQLSERVER instance wich i want to connect from my rails app hosted on heroku.
My gemfile:
gem 'activerecord-sqlserver-adapter', '~> 3.2.12'
gem 'tiny_tds'
database.yml
production:
adapter: sqlserver
mode: dblib
dataserver: host.database.windows.net
database: items
username: username#host
password: password
azure: true
production.rb
dbconfig = YAML.load(ERB.new(File.read(File.join("config","database.yml"))).result)
ActiveRecord::Base.establish_connection dbconfig['production']
But i get the follwing error during the deploy process:
Writing config/database.yml to read from DATABASE_URL
Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
LoadError: Please install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` (sqlite3 is not part of the bundle. Add it to Gemfile.)
It seems that Active record require sqlite3 but if i have tiny_tds it should use sql server.
In development env all works fine.
Sure there is something i am missing.
UPDATE
I've already set up the custom buildpack
BUILDPACK_URL: https://github.com/firmhouse/heroku-buildpack-ruby-freetds.git
and the DATABASE_URL config var.
UPDATE 2
Making a pp of dbconfig var while deploing display this
{"production"=>
{"adapter"=>"sqlite3",
"database"=>"dbname",
"username"=>"user",
"password"=>"pass",
"host"=>"127.0.0.1"}}
It seems that heroku overwrite my database.yml file, any suggestions?
UPDATE 3
I have set DATABASE_URL=sqlserver//user:pass#host:1433/database is this wrong?

I've faced same issue and finally found the answer.
You have to put the suffix below:
?encoding=uft-8&azure=true
So, your database_url will be like this:
sqlserver://[user]:[password]#[server.database.windows.net]:1433/[database]?encoding=uft-8&azure=true
Hope it helps

Looks like you need to use a custom buildpack.
Using this FreeTDS buildpack on Heroku
To use this buildpack you can pass in an option when creating your Heroku app:
heroku create my_new_sqlserver_app --buildpack https://github.com/firmhouse/heroku-buildpack-ruby-freetds.git
Or for current apps:
heroku config:add BUILDPACK_URL=https://github.com/firmhouse/heroku-buildpack-ruby-freetds.git
Configuring your database connection
After creating your app or setting up your existing app to use the buildpack, you need to modify the DATABASE_URL config variable to point to your sqlserver instance. We currently use a SQL Server 2008 Express edition:
heroku config:add DATABASE_URL=sqlserver://username:password#sqlserver_host:1433/database_name

Related

Added to Rails app on Heroku. How to setup database in local environment?

I was recently added to a Rails application via Heroku. I'm able to clone the app to my local machine using:
heroku git:clone -a appname
However, I have issues running local tests and other tasks related to not having a local database. The production environment uses Postgres and the development environment uses sqlite3. What's the best way to get a local database setup?
Simply pull the production database from Heroku. Run the following once you have heroku_toolbelt and you have access to the app on Heroku:
heroku pg:pull DATABASE_URL app_development --app app_name_on_heroku_here
This will create a new database locally named "app_development" so rename it to your app. See config/database.yml for your development database name.
You will need to setup the databases required for development. This can be done using the built in rake db tasks:
rake db:create
rake db:migrate
This should see your local database created and migrated up to the latest migration.
As you have successfully clone rails project from Heroku and your production database is PostgreSQL which is different from your local database SQLite you should update some files from your local project
1. remove or comment "gem pg" and add gem 'sqlite3' and run bundle install
2. Make changes in your database.yml file
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
3 run rake db:create
4 run rake db:migrate
5 if you have data in seed file run rake db:seed

heroku deploy error in RoR(Github deploy) what should I do with postgresql error?

I'm deploying small app from C9 to github then to Heroku!
In heroku deploy dashboard, it says it's been deployed, but when I open the app, it just gives me error msg.
So I searched a lot of Stackoverflow answers and was following the direction from Heroku website(https://devcenter.heroku.com/articles/sqlite3) to remove sqlite3 and install the gem pg.
I did replace gem 'sqlite3' to gem 'pg' and did 'bundle install', and then
It says I need to convert my config/database.yml file, So I replaced it like this:
development:
adapter: postgresql
database: my_database_development
pool: 5
timeout: 5000
test:
adapter: postgresql
database: my_database_test
pool: 5
timeout: 5000
production:
adapter: postgresql
database: my_database_production
pool: 5
timeout: 5000
and then when I come back to c9 bash and type 'rake db:create',
it shows me
**rake aborted!
ActiveRecord::NoDatabaseError: FATAL: database "my_database_development" does not exist**
this error.
someone said 'bundle exec rake db:setup' would work, so I did, and then it shows
**Couldn't create database for {"adapter"=>"postgresql", "database"=>"my_database_test", "pool"=>5, "timeout"=>5000}
/home/ubuntu/workspace/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /home/ubuntu/workspace/config/application.rb to limit the frameworks that will be loaded.**
I don't know how to react to this msg.... when I type 'rake db:migrate' and it shows this again.
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: database "my_database_development" does not exist
I'm not using database, so maybe I can try something on 'config/application.rb' file, but how should I do??
It wasn't necessary to change your development and test databases, those could still have used sqlite3 and so your C9 instance would have continued to work.
(Although there are some advantages to using a consistent adapter across all environments)
If this is just a hobby exercise I would be tempted to keep sqlite3 for development and test.
Also, Heroku replaces the database.yml with its own version so you don't need a production stanza in database.yml
You do need the pg gem, but you can specify it is only loaded in production and you can specify the sqlite3 gem is only loaded in development and test. Do that in your Gemfile and then bundle.
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
end
If you want to use PostgreSQL in your C9 instance, you have to install PostgreSQL (the database, not just the adapter). It's not a simple process.
This wiki explains how you would install it.
https://wiki.postgresql.org/wiki/Detailed_installation_guides
one of the solution that you can create database manually this is easily solut

Rake assuming wrong database engine?

I have adapter set to "mysql2" in my database.yml, yet when I try to deploy to Heroku, I get an error telling me, that I don't have Postgres installed... What the heck?
Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded.
Heroku works ONLY WITH POSTGRESQL
you should add postgresql to envirement what you use on Heroku
UPDATED
but Heroku has huck for MySQL:
TAKE A LOOK

Rails 4 and Heroku not logging intentional logs

So there a couple of similar questions regarding this but I've tried all of the answers and still can not get my app to do simple logging.
I'm using Rails 4.2, Ruby 2.2, Puma on a Heroku free account; So far I've tried:
Adding config.logger = Logger.new(STDOUT), config.log_level = :debug, and config.logger.level = Logger::DEBUG to my production.rb file as specified here, here and here
Adding the rails_12factor gem as specified by Heroku here
Added ActiveRecord::Base.logger.level = Logger::DEBUG to my environment.rb file as outlined here
Added $stdout.sync = true to my config.ru file as outlined here
In my app I have the following (none of which I can see in production when using $ heroku logs --tail nor in papertrail)
Rails::logger.debug "Test of 'Rails::logger.debug' logging"
Rails.logger.debug "Test of 'Rails.logger.debug' logging"
puts "Test of 'puts' logging"
So the problem was actually that I accidently had two classes with the same name; Apparently ruby doesn't doesn't throw an error in that event and because config.eager_load is set to false in dev, it always worked. The app always ended up loading the class without logging in production first which is why I never saw any messages.
If you're on a local environment. This worked for me.
Create a Heroku account. Install Heroku Toolbelt.
From your terminal run heroku login, youll be be prompted qith your username and password, enter your credentials.
In Gemfile, add the pg gem to your Rails project. Change:
gem sqlite
to
gem 'sqlite3', group: :development
gem 'pg', '0.18.1', group: :production
In Gemfile, add the rails_12factor gem::
gem 'rails_12factor', group: :production
In the terminal, install the gems specified in the Gemfile:
$ bundle install
Ensure config/database.yml is using the postgresql adapter. Change:
production:
<<: *default
database: db/production.sqlite3
to
production:
<<: *default
adapter: postgresql
database: db/production.sqlite3
Commit your changes to git:
$ git add .
$ git commit -m "Heroku config"
In the terminal, create an app on Heroku:
$ heroku create
Push your code to Heroku:
$ git push heroku master
If you are using the database in your application, migrate the database by running:
$ heroku run rake db:migrate
It has worked well for me. I am on windows.

Rails 4 - how to use sqlite3 in development and PostgreSQL in production w/Heroku

I am trying to deploy to Heroku but can't because the default sqlite3 server is still in place.
Detected sqlite3 gem which is not supported on Heroku.
https://devcenter.heroku.com/articles/sqlite3
In another tutorial with Rails 3.2.13 I was able to use sqlite3 as the dev db and Postgres as the production db. The Gemfile looks different in Rails 4 but I have modified it to have this:
group :development do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
end
group :production do
gem 'pg'
end
I then changed my database.yml file so that the production section looked like this:
production:
adapter: postgresql
database: my_production_database
pool: 5
timeout: 5000
I then ran bundle install and rake db:create and rake db:migrate but am still unable to push to Heroku. So I tried rake db:drop as well as rake db:create and rake db:migrate but am still getting the same error message.
Am I missing something? What else do I need to do to make sure I'm getting Postgres as my production database and am able to use Heroku?
Don't do it. You are just going to run into problems down the road. Use the same database in production and development. There are a lot of resources available in documenting the switch from a sqlite to postgres database.
Take the time and switch.
Have a look at this Rails Cast.
http://railscasts.com/episodes/342-migrating-to-postgresql?view=asciicast
Try using this for your production DB
production:
adapter: postgresql
host: localhost
encoding: unicode
database: my_production_database
pool: 5
username:
password:
You can leave username and password blank

Resources