Connecting to existing Heroku Postgres DB for Sinatra app - ruby

I am using Salesforce for my CRM, but need to query data (multiple records, so no Zapier) for a Heroku based Sinatra app. Heroku provides a service that replicates Salesforce objects to Postgres tables regularly which gets my data out of Salesforce.
I've coded a Sinatra app before and hosted it on Heroku so I'm a bit familiar with the process of setting up a dev db locally then pushing it to production via the many tutorials. However, i'm running into an issue finding any information that will allow me to connect to the existing production Postgres DB that already is populated with the data I need while having no need to create and connect to a local dev db.
Heroku provides me with the following
How and where to I plug in these various credentials securely so my following files have the right information to connect to tables Venue__c and Spaces__c?
app.rb
/config
- database.yml
- environments.rb

Assuming that you have ActiveRecord, it should just be a case of configuring your database.yml correctly. For example, to directly connect your development environment, use something like:
# database.yml
development:
adapter: postgresql
encoding: unicode
database: your_database
username: your_user
password: your_password
host: your_host
port: your_port
# or alternatively, using the URI
development:
url: postgres://your_user:your_password#your_host/your_database

Heroku publishes your database credentials to you through a URL in an environment variable. You should be able to access it through ENV.fetch("DATABASE_URL"). Depending on your ORM you will use different ways to connect to your database, e.g. Sequel is able to use this value directly as a parameter to Sequel.connect(ENV.fetch("DATABASE_URL")).
As you seem to use ActiveRecord you could be using ActiveRecord::Base.establish_connection(...) as suggested in this post which also exemplifies how to parse the URL (which might not be necessary anymore). This does not translate directly to a database.yml, but since you do not plan to use Rails you will have to provide your own version of environment.rb or some such and it should be easy enough to do in that place.

Turns out Heroku has a great tutorial that not only connects to the replicated database, but is also written for Sinatra.
https://devcenter.heroku.com/articles/getting-started-with-heroku-and-connect-without-local-dev#introduction

Related

How to deploy a Flask REST API containing MySQLdb code on Heroku?

Most of the tutorials only show how to deploy a simple Flask hello world app on Heroku. But I have a Flask app which contains URLs with both GET and POST requests and they use MySQLdb library for fetching data from database.
How to set up such apps on Heroku? Currently I have a MySQL database on my local machine which is used by the code to fetch data. The Flask code contains many functions which are invoked by API calls. For example:
#app.route('/display_table', methods=['POST'])
def display_webstats():
db = MySQLdb.connect("localhost", "root", "root", "db_name")
cursor = db.cursor()
cursor.execute("select * from table_name")
ws = cursor.fetchall()
return jsonify(ws), 200
How to deploy such apps on Heroku?
Make sure your dependencies are listed in your requirements.txt or Pipfile and Pipfile.lock.
Select a MySQL addon and provision it, e.g.
heroku addons:create cleardb:ignite
Update your code to connect to the database provided by whatever environment variables the addon provides, e.g. CLEARDB_DATABASE_URL. You can use os.getenv() with a default argument to fall back in your development environment:
import os
database_url = os.getenv(
'CLEARDB_DATABASE_URL',
default='mysql://root:root#localhost/db_name', # For local development
)
It's probably also a good idea to centralize your database connection logic so it's not done in every controller. Something like Flask-SQLAlchemy might be helpful to simplify connecting and querying your database. It also provides an ORM if you want one of those.
Deploy.
Assuming you have them, run your migrations on Heroku via heroku run. If you're not using a migrations library I urge you to start. Flask-Migrate might be a good fit.
The alternative is manually creating and maintaining your schema across environments, and that's time-consuming, error prone, and frustrating.

How to define the database for ActiveRecord migrations when database is hosted

I am using a non-Rails Active Record migration tool, and a hosted Postgres database from Heroku. My dev environment is on Digital Ocean. The production code will be on Heroku.
According to the docs, it seems that the way to connect is through the Database_URL, however, I don't see that as an option for the config.yml file based on the documentation.
What do I put in the development config and production config given the above conditions?
When I create my own tables manually, without using the standalone-migration gem, I used:
ActiveRecord::Base.establish_connection(ENV['POSTGRES_URL'])
but I don't see a similar option to just provide a single URL.
Can I just do the following?
# config.yml
development:
adapter: pg
database: ENV['POSTGRES_URL']
production:
adapter: pg
database: ENV['POSTGRES_URL']

Datamapper, use yaml for configuration

I have a Sinatra app which uses DataMapper as it's ORM, it's deployed to Heroku, but I want to change the database details.
Right now before my Models, I'm connecting to my DB like this, but I want to use a yml file to manage the connections details.
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3::memory:")
If I have a file thats at config/database.yml, how can I change the DataMapper.setup to use the proper ENV? This way I can remove the need for sqlite3 as I'll be running Postgres locally too.
As far as I know, DataMapper doesn't parse .yml. You can look at https://github.com/merb/merb_datamapper/blob/master/lib/merb_datamapper/connection.rb to see how .yml config is consumed in Merb framework.

How manage multiple DB with MongoLab and Heroku?

I have a project hosted on Heroku using Mongolab for storing data (MongoDB).
I need to create 2 different DB instances: one for prod, one for dev.
Any idea how can I do and how I can tell my code which one to use?
Thanks
You should use a config variable (https://devcenter.heroku.com/articles/config-vars) to set the database url and then use the environment variable in your mongo initializer. Set one app to use the production url and a dev app to use the developer database url.

Heroku app with Xeround DB, how to configure?

I’m trying to setup a Heroku app but using a Xeround DB. The instructions I have found are a bit confusing:
http://xeround.com/developers/heroku-cloud-database-mysql/
I'm trying to follow the second way, creating the db directly on Xeround.
My doubts are:
Is the Xeround addon required for this? At least heroku addons:add xeround –app xxxx says: That add-on is only available to selected users
The Heroku DATABASE_URL needs to be set to: mysql://username:password#host:port/database
I've seen some posts with mysql2 and mysql (none has worked for me anyway). My gem file has mysql2, 0.2.7 (for Rails 3.0.x)
I can see the config var ok, but Heroku config --app xxx still shows SHARED_DATABASE_URL => postgres://pjyqfgjcbn:.... is that ok?
Then from the instructions the step #5 is really confusing, it is not clear if that part is optional or not (I expect it is). And if it was mandatory it doesn't tell to which file it has to be added ...
My objective is to create a fresh Heroku app, push an existing app I have, set it up to use the Xeround DB and then run Heroku rake db:schema:load but no matter what I try I keep getting:
rake aborted!
database configuration does not specify adapter
Any ideas how to set it up?
do the same with Rails 3.0.5 but having some problems and comments.
Is the adapter mysql or mysql2? The example on Xeround about the setup site is mysql://username:password#host:port/database
mysql2 is correct gem to use with Rails 3 (mysql 0.2.7 for 3.0.x and latest for 3.1) but the name in the url might be just a name they picked. I guess yours worked but still…
I imagine the Xeround addon of Heroku is not needed right?
heroku addons:add xeround –app xxxx says That add-on is only available to selected users
And finally with mysql or mysql2 in the name of the db url when I try to migrate I just get:
rake aborted!
database configuration does not specify adapter
Did ask Heroku but no answer yet…
I've been using Xeround for several of my apps hosted on Heroku.
I don't have specific experience with Rails. Sinatra only... But hopefully this helps you out.
I do not use the add-on. It's less expensive to setup an account through Xeround on your own.
Connect to it just like you would any MySQL Database....
Connection String will look just like you stated:
mysql://username:password#host:port/database
Host/Port are provided by Xeround in the admin database. Database name would be whatever you setup in phpMyAdmin. User/pass are NOT your Xeround account credentials. They are the credentials you setup for that database instance.
For example... if your ORM is Datamapper... do something like this:
DataMapper.setup(:default, ENV['XEROUND_CONN'])
Or if you're using Sequel:
DB = Sequel.connect(ENV['XEROUND_CONN'])
Then you need to set your XEROUND_CONN (or whatever you wish to call it) ENV variable.
Do this by using the Heroku command
heroku config:add XEROUND_CONN='mysql://username:password#host:port/database'
Warning: Datamapper has an issue with Xeround. Datamapper does not have support for setting your storage engine (myisam..etc..). Xeround uses their own custom Xeround storage engine. So... I've had trouble running auto_migrate due to differences in storage engine.

Resources